Sparky1157
Programmer
I was running an Excel 2010 macro on a Gen-Y VDI system running Windows 7 and everything worked just fine. Now, however, we upgraded to Excel 2013 with an AppSphere VDI system running Server 2008. The macro now behaves very erratic - sometimes it does nothing, and sometimes it will get part way through and then just quit.
Please see code below:
Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hWnd As Integer) As Integer
Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Declare Function IsIconic Lib "user32.dll" (ByVal hWnd As Integer) As Boolean
Declare Function ShowWindow Lib "user32.dll" (ByVal hWnd As Integer, ByVal nCmdShow As Integer) As Integer
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Const SW_RESTORE As Integer = 9
Const SW_SHOW As Integer = 5
Sub Autpen()
Dim WshShell As Object
Dim hWnd As Integer
Dim Temp As Integer 'Dummy variable - see below
hWnd = FindWindow(vbNullString, "Inbox - ... Outlook")
If hWnd > 0 Then
SetForegroundWindow (hWnd)
If IsIconic(hWnd) Then 'Restore if minimized
Temp = ShowWindow(hWnd, SW_RESTORE) 'Temp is meaningless, but compiler wanted "=" sign
Else
Temp = ShowWindow(hWnd, SW_SHOW)
End If
SendKeys ("{Tab}")
Sleep 200 ' Pause for 1/5 of a second.
SendKeys ("{Tab}")
Sleep 200 ' Pause for 1/5 of a second.
SendKeys ("{Tab}")
Sleep 200 ' Pause for 1/5 of a second.
SendKeys ("{Tab}")
Sleep 200 ' Pause for 1/5 of a second.
SendKeys ("{Down}")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("{Down}")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("{Down}")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("{Down}")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("{Down}")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("{Enter}")
Sleep 1000 ' Pause for 1 second.
SendKeys ("{Enter}")
Sleep 1000 ' Pause for 1 second.
SendKeys ("{Tab}")
Sleep 200 ' Pause for 1/5 of a second.
SendKeys ("{Tab}")
Sleep 200 ' Pause for 1/5 of a second.
SendKeys ("{Tab}")
Sleep 200 ' Pause for 1/5 of a second.
SendKeys ("{Tab}")
Sleep 200 ' Pause for 1/5 of a second.
SendKeys ("^a")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("^c")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("^n")
Sleep 1000 ' Pause for 1 second.
SendKeys ("{Tab}")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("{Tab}")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("{Tab}")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("{Tab}")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("^a")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("^v")
End If
'
' Quit Excel
'
Application.Quit
End Sub
Please see code below:
Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hWnd As Integer) As Integer
Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Declare Function IsIconic Lib "user32.dll" (ByVal hWnd As Integer) As Boolean
Declare Function ShowWindow Lib "user32.dll" (ByVal hWnd As Integer, ByVal nCmdShow As Integer) As Integer
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Const SW_RESTORE As Integer = 9
Const SW_SHOW As Integer = 5
Sub Autpen()
Dim WshShell As Object
Dim hWnd As Integer
Dim Temp As Integer 'Dummy variable - see below
hWnd = FindWindow(vbNullString, "Inbox - ... Outlook")
If hWnd > 0 Then
SetForegroundWindow (hWnd)
If IsIconic(hWnd) Then 'Restore if minimized
Temp = ShowWindow(hWnd, SW_RESTORE) 'Temp is meaningless, but compiler wanted "=" sign
Else
Temp = ShowWindow(hWnd, SW_SHOW)
End If
SendKeys ("{Tab}")
Sleep 200 ' Pause for 1/5 of a second.
SendKeys ("{Tab}")
Sleep 200 ' Pause for 1/5 of a second.
SendKeys ("{Tab}")
Sleep 200 ' Pause for 1/5 of a second.
SendKeys ("{Tab}")
Sleep 200 ' Pause for 1/5 of a second.
SendKeys ("{Down}")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("{Down}")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("{Down}")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("{Down}")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("{Down}")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("{Enter}")
Sleep 1000 ' Pause for 1 second.
SendKeys ("{Enter}")
Sleep 1000 ' Pause for 1 second.
SendKeys ("{Tab}")
Sleep 200 ' Pause for 1/5 of a second.
SendKeys ("{Tab}")
Sleep 200 ' Pause for 1/5 of a second.
SendKeys ("{Tab}")
Sleep 200 ' Pause for 1/5 of a second.
SendKeys ("{Tab}")
Sleep 200 ' Pause for 1/5 of a second.
SendKeys ("^a")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("^c")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("^n")
Sleep 1000 ' Pause for 1 second.
SendKeys ("{Tab}")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("{Tab}")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("{Tab}")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("{Tab}")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("^a")
Sleep 100 ' Pause for 1/10 of a second.
SendKeys ("^v")
End If
'
' Quit Excel
'
Application.Quit
End Sub