Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

excel

vba

pid

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 I have two or more Excel instances with the same caption.

like image 407
Jason Avatar asked Jun 02 '09 21:06

Jason


People also ask

How do you find the PID in Excel?

To get the process ID, based on handler of the Microsoft Excel or Microsoft PowerPoint application instance, you need to import 'user32. dll' via DllImport and then use the Win32 API 'GetWindowThreadProcessId'.

How do you use .END in Excel VBA?

An End is a statement in VBA that has multiple forms in VBA applications. One can put a simple End statement anywhere in the code. It will automatically stop the execution of the code. One can use the End statement in many procedures like to end the sub procedure or to end any Loop function like “End If.”

How do I use Find function in Excel VBA?

Define the FIND function of VBA Excel. To search, a VBA code is written by entering some or all arguments of the FIND function. One can specify the direction of search, order of search, data to be searched, the format of the search value, and so on. The FIND function returns the cell containing the specified value.

What is VVA in Excel?

View More. VBA stands for Visual Basic Analysis. Excel VBA is Microsoft's programming language for Office applications such as MS-Excel, MS-Word, and MS-Access. Macros are what most people who write VBA code use.


3 Answers

My solution in Excel 2013: in a new module, I added the following code:

Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long

Public Sub Test()
    Debug.Print GetCurrentProcessId
End Sub
like image 154
Corey Avatar answered Nov 25 '22 16:11

Corey


You can use this method to get the current process id.

Declare Function GetCurrentProcessId Lib "kernel32" () As Long

This page has a good overview of exactly how you can do it in various versions of excel.

like image 23
Mitchel Sellers Avatar answered Nov 25 '22 16:11

Mitchel Sellers


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

like image 37
Shreyas Avatar answered Nov 25 '22 15:11

Shreyas