0% found this document useful (0 votes)
17 views

How Do You Switch Between Excel and A Third Party Application (E.g. Chrome) Using VBA - Excel

Uploaded by

phuockcn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

How Do You Switch Between Excel and A Third Party Application (E.g. Chrome) Using VBA - Excel

Uploaded by

phuockcn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

7/27/2019 How do you switch between Excel and a third party application (e.g. Chrome) using VBA?

: excel

Search r/excel

 3   Hsolved CLOSE

Posted by u/De_Noir 9 months ago 

How do you switch between Excel and a third


party application (e.g. Chrome) using VBA?
solved

Hi all,

I currently have this code:

Dim PROGRAM As Variant

PROGRAM = Shell("C:\PROGRAM\PROGRAM.exe", vbNormalFocus)

On Error Resume Next

AppActivate PROGRAM

what this code does is call the PROGRAM and opens it. Yet assuming
that this PROGRAM is already open, how would I first define the
PROGRAM so VBA would recognize it and secondly how would I
switch between Excel and the PROGRAM? For comparison, in order to
switch between various workbooks, one would use the command
".activate". Does something similar exist to switch between Excel and
third party applications?

Thanks for any help!

 6 Comments  Share  100% Upvoted

This thread is archived



New comments cannot be posted and votes cannot be cast

SORT BY TOP (SUGGESTED)

ViperSRT3g 516 3 points · 9 months ago


To do something like this, you would have to make use of
the Windows API (WINAPI) to interact with various
programs on your machine. Here's some code to show you
how it works:

Option Explicit

Public Declare Function FindWindow Lib "user32" A


Public Declare Function SetForegroundWindow Lib "

https://www.reddit.com/r/excel/comments/9kh7p1/how_do_you_switch_between_excel_and_a_third_party/ 1/3
7/27/2019 How do you switch between Excel and a third party application (e.g. Chrome) using VBA? : excel

Public Declare Function SetActiveWindow Lib "user


Search r/excel
Dim NotepadHwnd As Long

 3 Dim
 ExcelHwnd
 Hsolved As Long CLOSE
Dim Flip As Boolean

Public Sub GetWindows()


NotepadHwnd = FindWindow(vbNullString, "Untit
If NotepadHwnd > 0 Then MsgBox "Notepad Windo

ExcelHwnd = FindWindow(vbNullString, "Book1 -


If ExcelHwnd > 0 Then MsgBox "Excel Window Fo
End Sub

Public Sub Toggle()


If Flip Then
Call SetForegroundWindow(NotepadHwnd)
Call SetActiveWindow(NotepadHwnd)
Else
Call SetForegroundWindow(ExcelHwnd)
Call SetActiveWindow(ExcelHwnd)
End If
Flip = Not Flip
End Sub

The GetWindows finds the windows by their captions, the


main titles of said windows. It's what their names are in the
taskbar. The API requests a class name (We don't
necessarily need to pass one if we don't know it) and the
window name. So we pass the window name, and get a
number back. This number, is how windows keeps track of
that particular program's window. It's basically a number in
memory that points to the particular window it represents,
other programming languages call these things pointers.
Using this pointer, we can use it in other WINAPIs to do
other things like activate the windows and bring them into
focus. So once we have our two window IDs, we can then
test that we have them, and that the API works by running
the Toggle subroutine. This will utilize the other API
functions to manipulate the windows as you described in
your post.

De_Noir  2 points · 9 months ago


Solution verified!
Continue this thread

De_Noir  1 point · 9 months ago


I awarded you the solution as I was not aware of the
commands in question and the fact that this kind of

https://www.reddit.com/r/excel/comments/9kh7p1/how_do_you_switch_between_excel_and_a_third_party/ 2/3
7/27/2019 How do you switch between Excel and a third party application (e.g. Chrome) using VBA? : excel

manipulation was possible. Thank you a lot!


Search r/excel

dougiek 149 1 point · 9 months ago


 3   Hsolved CLOSE
You might have more control if you use IE instead of
Chrome. I've never done anything like that but I know you
can web scrape and stuff through IE using VBA.

De_Noir  1 point · 9 months ago


To clarify, I mentioned Chrome to emphasize that the
program in question is not from MS. The program in
question is not a browser. Notepad could be a good
example.

https://www.reddit.com/r/excel/comments/9kh7p1/how_do_you_switch_between_excel_and_a_third_party/ 3/3

You might also like