How to get the process ID of the current Excel instance, through VBA, without using the caption?

前端 未结 3 2155
无人共我
无人共我 2021-02-20 03:38

How can I get the process ID of the current Excel instance that my VBA code is running in? I don\'t want to asking for it by the name in the caption, which causes problems when

3条回答
  •  故里飘歌
    2021-02-20 04:19

    As a vba n00b, some other things I did not know

    1. The Declare statement goes at the top. VBA will complain if the declare statement is inserted after a sub declaration

      For example, this will work

      Declare Function GetCurrentProcessId Lib "kernel32" () As Long
      
      Sub Update
        ...
        ...
      End Sub
      

      But this will not work

      Sub Update
        ...
        ...
      End Sub
      
      Declare Function GetCurrentProcessId Lib "kernel32" () As Long
      
    2. Here is how we display the PID in a messagebox in vbscript

      Set app = CreateObject("Excel.Application")
      MsgBox("Excel PID is " + CStr(app.Run("GetCurrentProcessId")))
      

    Hope this helps someone

提交回复
热议问题