Adjusting VBA to find a window in a Virtual Machine

anichols

Board Regular
Joined
Mar 11, 2021
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I have a macro that looks for a "Save As" window to popup. Once the window pops up, the copied value of the cell will be pasted and then saved. My issue, is that I am now having to run the report program that creates the save as window in a Virtual Machine. as such, my code doesn't seem to notice when the save as box pops up anymore. Does anybody have any advice on how to make my macro look inside my VM?
Here's the code I am using below:
VBA Code:
Private Declare Function FindWindow Lib "user32" _
                                    Alias "FindWindowA" _
                                    (ByVal lpClassName As String, _
                                     ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Sub try_saveAs() 

    Const MAX_WAIT_SECS As Long = 10800 '3 hrs = 10800
    Const WAIT_SECS As Long = 5

    Dim hWnd As Long
    Dim ans As VbMsgBoxResult
    Dim endTime As Single
  
    endTime = Timer + MAX_WAIT_SECS

    Sheets("DAILY02").Select
    Range("D24").Select
    Selection.Copy
    Do
        hWnd = FindWindow(vbNullString, "Save As")
        If hWnd > 0 Then
            SetForegroundWindow hWnd
            'hWnd.WindowState = xlMaximized
            Application.SendKeys ("^v")
            Application.SendKeys ("~")
            Exit Do
        End If
        If Timer > endTime Then
            ans = MsgBox("Window not found, try again?", vbQuestion + vbYesNo)
            If ans = vbYes Then
                endTime = Timer + MAX_WAIT_SECS
            Else
                Exit Do
            End If
        Else
            PauseMacro WAIT_SECS
        End If
    Loop

End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi. My understanding of VM's is such that you're not really supposed to be able to reach into/out off a VM (or at least on an automated basis). I did a bit checking and it appears that someone else has asked a similar kind question (FindWindows - target is in VM) on a VB Forum about a decade ago: link. I think it's useful and I can understand the logic behind the approach they took, but the end conclusion is: you can't do it through normal WinAPIs. The suggestion there would be to check see whether the VM software you're using has equivalent APIs available.

Of course, I could be completely wrong and someone else might (hopefully) pipe up and give you the solution, but I thought it was an interesting question, and wanted to share what I managed to find.
 
Upvote 0

Forum statistics

Threads
1,223,244
Messages
6,170,976
Members
452,372
Latest member
Natalie18

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top