Can someone please tell me if it's possible, and if it is where I can find some sample VB code that would allow me, to programmatically switch between MS Word, MS Excel and the internet, and also pass data between them? Thanks.
Printable View
Can someone please tell me if it's possible, and if it is where I can find some sample VB code that would allow me, to programmatically switch between MS Word, MS Excel and the internet, and also pass data between them? Thanks.
Do you mean like emulating a user doing Copy, Alt Tab, Paste?
If so, this may help with the Alt Tab bit:
Finding the last application used:
That is, if the user is using an application with this code in it, what application would they switch to when they press ALT TAB.
Note: Within Windows 98 it is not possible to use the VB SendKeys function to send an ALT TAB sequence. Therefore this code needs to be used in order to activate the previously used application.
The code involves stepping through all of the open windows on the system, and identifying which windows:
· Don’t have parents (top level of an application)
· Have a title bar and caption (Windows can be identified and listed)
· Are visible (not hidden from view)
· Are not from this application.
The module code required is:
This routine is called using:VB Code:
Private Declare Function IsWindowVisible Lib "user32" _ (ByVal hWnd As Long) As Long Public Declare Function EnumWindows Lib "user32" _ (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long Public Declare Function GetWindowText Lib "user32" _ (ByVal hWnd As Long, ByVal lpString As String, _ ByVal cch As Long) As Long Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long Public Declare Function GetWindowLong Lib "user32" _ (ByVal hWnd As Long, ByVal nIndex As Long) As Long Declare Function GetActiveWindow Lib "user32" () As Long Public thisHwnd As Long Public FirstApp As Long Public FirstAppTitle As String Public Const GWL_STYLE = -16 Public Const WS_CAPTION = &HC00000 Public Const MAX_LEN = 260 Public Function _ EnumWinProc(ByVal hWnd As Long, ByVal lParam As Long) As Long Dim lRet, lWindowStyle, lWindowParent As Long Dim strBuffer As String ' Only keep windows that are visible, ' don't have parents, have a title bar and a caption, ' and are not this application, and are the first application found If hWnd <> thisHwnd Then ' Not this app If IsWindowVisible(hWnd) Then ' Window visible lWindowParent = GetParent(hWnd) If lWindowParent = 0 Then ' No parents lWindowStyle = GetWindowLong(hWnd, GWL_STYLE) If (lWindowStyle And WS_CAPTION) Then ' Has a caption strBuffer = Space(MAX_LEN) lRet = GetWindowText(hWnd, strBuffer, _ Len(strBuffer)) If lRet Then ' Has a title If FirstApp = 0 Then FirstApp = hWnd FirstAppTitle = strBuffer End If End If End If End If End If End If EnumWinProc = 1 End Function
The return from this routine checks for the Class name of the application, and looks for the following in particular (any others are reported as “Not useful”):VB Code:
thisHwnd = GetActiveWindow() FirstApp = 0 Call EnumWindows(AddressOf EnumWinProc, 0) MsgBox "The last application we were using was " & FirstApp & _ ", and has a Title of " & FirstAppTitle
The following line will activate the previous application:VB Code:
Select Case LastAppClass Case "OpusApp" WhoCalledUs = "Word" Case "XLMAIN" WhoCalledUs = "Excel" Case "rctrl_renwnd32" WhoCalledUs = "Outlook" Case "ExploreWClass" WhoCalledUs = "WinExp" Case "PP9FrameClass" WhoCalledUs = "PPT"
AppActivate FirstAppTitle
All the above code either works or it does not. If it does, then its likely that I "borrowed" it from elsewhere on this board. If it doesn't work, then its likely to be mine.
PLUS
The cut and paste bit can be done using the Clipboard functions.
yes, I am talking about moving between applications. But I'm most interested in being able to execute more VB code afeter I've switched to the new app. For instance, while executing code on an Excel WB, I'd like to switch to follow a hyperlink, retrieve some data (by executing somemore VB code), switch back to Excel, apply the retireved data, the switch to Word, execute somemore VB code, switch back to Excel, etc.
The previous code can help to identify which application you want to be active.
The AppActivate command will activate it for you. Once it is active, your code is no longer active.
However, what you can do is rather than activating the application, you can (as long as it has a callable interface like Word, Excel, Outlook etc.) send it commands....
E.g.:
which takes a copy of the current document, and saves it to the temporary folder as Document.DOCVB Code:
Set Obj = Word.ActiveDocument If Obj.Saved = False Then Obj.Save OrigFullFile = Obj.FullName tempFile = Environ("Temp") & "\Document.DOC" Obj.SaveAs tempFile Obj.SaveAs OrigFullFile
For Excel:
For PowerPoint:VB Code:
Set App = GetObject(, "Excel.Application") Set Obj = App.ActiveWorkbook OrigFullFile = Obj.FullName
For Outlook 2000:VB Code:
Set App = GetObject(, "PowerPoint.Application") With App.ActivePresentation OrigFullFile = .FullName tempFile = Environ("Temp") & "\PowerPoint.PPT" .SaveCopyAs tempFile
VB Code:
Set olExp = Outlook.ActiveExplorer Set olCurrentFolder = olExp.CurrentFolder Set olFolderItems = olCurrentFolder.Items Set myOlSel = olExp.Selection For x = 1 To myOlSel.Count On Error Resume Next gSubject = myOlSel.Item(x).Subject
Thanks a lot. I'll give it a try.
Hello. I still cannot get this to work. when I execute, I get error message from Excel:
"Run-time error 453.
Can't find DLL entyr point GetWindowLong in user32"
What gives? Thanks.
I looked at other examples posted here and found the use of ALIAS. Added Alias "GetWindowLongA" and Alias "GetWindowTextA" to my function declares and that fixed the problem. Thanks.