Use variable for application name

I have a macro that will switch between programs. I noticed that Excel 2010 and 2016 are named differently in the system (my semantics are wrong here, sorry!), And instead of changing the code depending on which Excel, I just thought I could use the variable to set , what.

Sub test() 'code here in Excel Dim myApp myApp = Application.Application AppActivate "Google Chrome" Call AppActivate("Google Chrome") ' do one or two things AppActivate myApp 'do more things in Excel End Sub 

Unfortunately, AppActivate myApp does not work. He throws

Runtime Error '5': Incorrect procedure call or argument

Is there a way to do what I'm trying to do? I see on this site that I could do something like:

 Public vPID As Variant vPID = Shell("C:\Windows\system32\notepad.exe", vbNormalFocus) AppActivate (vPID) 

Also, what if Excel is not in the same file path on the two computers on which it will be used?

Edit: it looks like I just need to set the Application Title to a variable (again, from this site):

Typically, the AppActivate operator is used to activate an existing application based on its name.

edit2: Going closer, I found that I can get the path to Excel.Exe this, excelPath = Application.Path & "\Excel.exe" , but I can’t figure out what to call it.

+5
source share
2 answers

To activate the window title of the instance that runs the code:

 AppActivate Application.Caption 

Or by process id (probably more reliable):

 AppActivate pid 

Where pid is the result of GetCurrentProcessId() .

+5
source

Here is a very simple example:

  • discovery of the word
  • activation

     Sub UsingWord() Dim wrdApp As Word.Application Dim wrdDoc As Word.Document Set wrdApp = CreateObject("Word.Application") wrdApp.Visible = True AppActivate wrdApp.Caption End Sub 
+2
source

All Articles