如何检查哪个 Office 应用程序 VBA 代码正在运行?

发布于 2024-12-03 22:40:51 字数 795 浏览 2 评论 0原文

我目前正在使用 VBA 代码,该代码最初用于 Excel 文档。我可以只零售或在 Word 中使用的代码,但我认为扩展该应用程序可能会更好,以便它“可能”兼容任何 MS Office 应用程序。但我不完全确定该怎么做。

例如,基于 Excel 的代码获取文档的路径:

If libDir = "" Then libDir = ThisWorkbook.Path

要将其扩展到 Word,我只需将 ThisWorkbook 替换为 ActiveDocument。

但是否有可能找出哪个 Office 应用程序正在运行 VBA 代码,以便我可以保留两者。

 'IF VBA run from Word Then
     If libDir = "" Then libDir = ThisWorkbook.Path
 'ELSE VBA run from Excel then
     If libDir = "" Then libDir = ActiveDocument.path
 'End If

到目前为止,我可以看到我可以找出 word 或 excel 是否打开,但我不能假设因为它是打开的,所以它正在运行该特定模块。但这超出了我的 VBA 知识范围。

我也尝试过这个 currentproject.path 但这不起作用。

提前致谢

I am currently using VBA code which was originally intended for an excel document. I could just retailor the code for use in word but I think it might be better to extend the application so that it is compatible to 'potentially' any MS Office Application. But I'm not entirely sure how to do so.

For example the excel based code fetches the document's path:

If libDir = "" Then libDir = ThisWorkbook.Path

To extend this to word I just have to replace ThisWorkbook with ActiveDocument.

But would it be possible to find out which office application is running the VBA code so that I can keep both.

 'IF VBA run from Word Then
     If libDir = "" Then libDir = ThisWorkbook.Path
 'ELSE VBA run from Excel then
     If libDir = "" Then libDir = ActiveDocument.path
 'End If

So far I can see that I can find out if word or excel is open but I cannot assume that because it is open it is running that specific module. But that is beyond my VBA knowledge.

I've also tried this currentproject.path but that doesn't work.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒绊 2024-12-10 22:40:51

我自己发现了这种需要,并努力寻找任何类型的条件编译或其他方法来区分应用程序。

刚才,我凭直觉发现每个办公应用程序中的 Application 对象都有 .Name 和 .Version String 属性。

因此,您可以执行一些类似这样的代码来确定您正在运行哪个应用程序:

Private Sub TestApplication()
  Select Case Application.Name
    Case "Microsoft Word"
      MsgBox "Running in Word " + Application.Version
    Case "Microsoft Excel"
        MsgBox "Running in Excel " + Application.Version
    Case "Microsoft PowerPoint"
        MsgBox "Running in PowerPoint " + Application.Version
    Case "Microsoft Visio"
        MsgBox "Running in Visio " + Application.Version
    Case "Microsoft Access"
        MsgBox "Running in Access " + Application.Version
    Case "Outlook"
        MsgBox "Running in Outlook " + Application.Version
    Case Else
       MsgBox "Unknown Application " + Application.Name + " " + Application.Version
  End Select
End Sub

I found a need for this myself and struggled to locate any sort of conditional compilation or other means to distinguish the application.

Just now, on a hunch I discovered that the Application object in each of the office apps has .Name and .Version String properties.

So, you could do some code like so to determine which application you're running under:

Private Sub TestApplication()
  Select Case Application.Name
    Case "Microsoft Word"
      MsgBox "Running in Word " + Application.Version
    Case "Microsoft Excel"
        MsgBox "Running in Excel " + Application.Version
    Case "Microsoft PowerPoint"
        MsgBox "Running in PowerPoint " + Application.Version
    Case "Microsoft Visio"
        MsgBox "Running in Visio " + Application.Version
    Case "Microsoft Access"
        MsgBox "Running in Access " + Application.Version
    Case "Outlook"
        MsgBox "Running in Outlook " + Application.Version
    Case Else
       MsgBox "Unknown Application " + Application.Name + " " + Application.Version
  End Select
End Sub
浮光之海 2024-12-10 22:40:51

如果总体意图是构建和重用可在所有 Office 应用程序中使用的 VBA 代码库,更好的方法可能是使用条件编译。上面 TestApplication 示例中的前两种情况将如下所示:(

Private Sub TestApplication()
    #If Word then
        MsgBox "Running in Word " + Application.Version
    #ElseIf Excel 
        MsgBox "Running in Excel " + Application.Version
    #Else
        MsgBox "Running in SomeOtherApplication " + Application.Version
    #End if    Case "Microsoft PowerPoint"
End Sub

所有其他应用程序均已使用 #Else 捕获。)

通过在包含以下内容的 VBProject 的属性中适当设置条件编译参数“Word”和“Excel”重用代码就可以确保只编译该实现所需的代码。例如,在 Word 中使用所需的条件编译参数为:


Word = -1 : Excel = 0


即 Word = True : Excel = False 是将代码编译到 Word 特定应用程序所需的设置。

最后一个一般提示:
设置条件编译参数的值时,始终使用 -1 表示 True。如果您稍后决定反转语句的意图,则可以通过添加 NOT 轻松更改意图。这是一个好主意,因为:

您可能知道,VBA 条件语句将条件语句中的非零值视为 true。例如,这将转到标签第 10 行:
如果 5 则转到 LINE10
这是可以的,直到您尝试更改此类语句的意图以不根据此类条件执行某些操作。该语句仍会转到第 10 行。
如果不是 5,则转到 LINE10
NOT 运算符不会将 -1 以外的任何数字解析为 False(即 0)。

If the overall intention is to build and re-use a VBA code base that can be used in all Office applications a better way may be to use conditional compilation. The first two cases in the TestApplication example above would then look like this:

Private Sub TestApplication()
    #If Word then
        MsgBox "Running in Word " + Application.Version
    #ElseIf Excel 
        MsgBox "Running in Excel " + Application.Version
    #Else
        MsgBox "Running in SomeOtherApplication " + Application.Version
    #End if    Case "Microsoft PowerPoint"
End Sub

(All other applications have been caught with an #Else.)

By setting conditional compilation arguments 'Word' and 'Excel' appropriate in the Properties of the VBProject that contains the re-used code it is then possible to ensure that only the code required for that implementation is compiled. e.g. the conditional compilation arguments needed for use in Word would be:


Word = -1 : Excel = 0


That is, Word = True : Excel = False, are the required settings for compilation of the code into a Word specific application.

One final general Tip:
Always use -1 for True when setting the values of the Conditional Compilation arguments. If you later decide to reverse the intent of a statement, it is then easy to change the intent by adding NOT. This is a good idea because:

As you probably know, VBA conditional statements treat non zero values in conditional statements as being true. e.g. this would GoTo Label Line10:
If 5 Then GoTo LINE10
This is OK until you try to change the intent of such a statement to NOT do something based on such a condition. This statement will still GoTo Line10.
If NOT 5 Then GoTo LINE10
The NOT operator will not resolve any number other than -1 to False (that is, 0).

渔村楼浪 2024-12-10 22:40:51

尝试改进代码以适应各种 Office 应用程序听起来确实很复杂。如果这段代码可以应用在word和excel中,那么它会做什么 - 也许是vbscript,然后自动化你想要使用的对象是一个选项

在上面的具体示例中,它实际上可以编译好(我已经在xl2003中进行了测试, xl2010 和 word2010),因此通过错误处理,代码将运行并跳过其他有问题的区域。

 On Error Resume Next
    If libDir = "" Then libDir = ThisWorkbook.Path
    If libDir = "" Then libDir = ActiveDocument.Path
 On Error GoTo 0

还有一点需要考虑,如果文件未保存,路径将不存在。

It does sound complex trying to tool up code to suit various Office APPs. And what does this code do if it can be applied in word and excel - perhaps vbscript and then automating the object you want to work with is an option

On your specific example above, it will actually compile okay (and I haved tested in xl2003, xl2010 and word2010), so with error handling the code will run and skip the otherwise problematic areas.

 On Error Resume Next
    If libDir = "" Then libDir = ThisWorkbook.Path
    If libDir = "" Then libDir = ActiveDocument.Path
 On Error GoTo 0

Something else to consider, the Path wont exist if the file is unsaved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文