0

I Have create VBA code which open website and click upload button but after executing upload button its still running same line but it should run next line of my API program for fill the popup upload form but its not running.

Below is my VBA code:

IE.Navigate "https://XXX.my.XXXX.com/home/home.jsp"
Set filee = mydoc.getElementById("file")
filee.Click 'here only paused
call uploadAPI

My API upload program:

Public Declare PtrSafe Function SendMessageByString Lib "user32" Alias "SendMessageA" ( _
ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long

Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Declare Function GetWindowTextLength Lib "user32" Alias _
"GetWindowTextLengthA" (ByVal hwnd As Long) As Long


Dim strBuff As String, ButCap As String
Public Const WM_SETTEXT = &HC
Public Const BM_CLICK = &HF5

Sub uploadAPI()

    hw = FindWindow(vbNullString, "Choose File to Upload")
    op = FindWindowEx(hw, 0&, "Button", vbNullString)

    strBuff = String(GetWindowTextLength(op) + 1, Chr$(0))
    GetWindowText op, strBuff, Len(strBuff)
    ButCap = strBuff

    Do While op <> 0
        If InStr(1, ButCap, "Open") Then
            OpenRet = op
            Exit Do
        End If
    Loop

    hw1 = FindWindowEx(hw, 0&, "ComboBoxEx32", vbNullString)
    hw2 = FindWindowEx(hw1, 0&, "ComboBox", vbNullString)
    hw3 = FindWindowEx(hw2, 0&, "Edit", vbNullString)

    Call SendMessageByString(hw3, WM_SETTEXT, 0, _
                             "C:\Users\kk\Documents\ka\H\2015\MAY\410.pdf")
    Call SendMessage(OpenRet, BM_CLICK, 0, 0)

End Sub

I have tried like this also

filee.Click : call uploadAPI

Kindly advice me to run my Upload API program after click upload Popup link.

Kathir vtv
  • 305
  • 2
  • 7
  • 19
  • 2
    This is the wrong way to perform automated uploading. – David Heffernan Jan 19 '16 at 15:34
  • 1
    @DavidHeffernan Kindly Provide me right one to perform automated uploading – Kathir vtv Jan 19 '16 at 15:43
  • 1
    It probably depends on what the server expects, how the webservice is designed and so on. Faking input to a browser is rarely the right way to do anything. – David Heffernan Jan 19 '16 at 15:53
  • Where does the code fail? If you don't know, you need to implement error handling (and use a debugger). – IInspectable Jan 19 '16 at 15:54
  • You also need to show us your API declarations with the conditional compilation code if required. – SierraOscar Jan 19 '16 at 16:16
  • @IInspectable I think coding fine but after upload popup windows open vba pause the current line if i fill the upload form manually and close my program start executing. – Kathir vtv Jan 19 '16 at 16:36
  • @IInspectable my program paused after executing "filee.Click" this line after my form filling API is not running – Kathir vtv Jan 19 '16 at 16:41
  • @MacroMan i have update my API declaration for your requirement. – Kathir vtv Jan 19 '16 at 16:42
  • Is it really paused? Code can't just pause itself - either it's waiting for a response from something or your API piece is running and not returning anything. You need to do some basic debugging and then give us more information. – SierraOscar Jan 19 '16 at 17:07
  • Very likely, your call to `filee.Click` is synchronous. I.e. it won't return, until the operation it invokes runs to completion. Once again, you are using the wrong tool for the job. Either upload the document using a custom implementation, or use [UI Automation](https://msdn.microsoft.com/en-us/library/ms747327.aspx) if you believe that automating a UI is your best option (it probably isn't). – IInspectable Jan 19 '16 at 17:28
  • @MacroMan yes ,its waiting for response only – Kathir vtv Jan 20 '16 at 01:18
  • @MacroMan . But API program only need fill that popup form without executing API sub program it wont get response. Is there any way do that ? – Kathir vtv Jan 20 '16 at 01:26
  • Is there any command to avoid waiting response from Popup file upload window like ie.FileUpload(Find.ById("FilUpload")).ClickNoWait(); and ie.FileUpload(Find.ById("profile_file")).Text = "C:/Desktop/image.jpg"; – Kathir vtv Jan 21 '16 at 06:30

1 Answers1

0

I fixed this issue by running external VBScript which contain file path to set it on 'Choose File to Upload' pop up window using SendKeys method after that I send Enter Key to close this pop up, and this run successfully because the external VBScript will run on another thread so it will not stuck on VBA code.

Notes: 1- I dynamically create the external VBScript from VBA code and save it on Temp folder after that I run this script using WScript.Shell.Run to excutet it on another thread 1- At the beginning of the external VBScript I set 1 sec delay to be sure the 'Choose File to Upload' pop up window already opened from VBA.

And here is the complete code:

....
....

IE.Navigate "https://XXX.my.XXXX.com/home/home.jsp"
Set filee = mydoc.getElementById("file")

    CompleteUploadThread MyFilePath
    filee.Foucs
    filee.Click

....
....

Private Sub CompleteUploadThread(ByVal fName As String)
    Dim strScript As String, sFileName As String, wsh As Object
    Set wsh = VBA.CreateObject("WScript.Shell")
    '---Create VBscript String---
    strScript = "WScript.Sleep 1000" & vbCrLf & _
                "Dim wsh" & vbCrLf & _
                "Set wsh = CreateObject(""WScript.Shell"")" & vbCrLf & _
                "wsh.SendKeys """ & fName & """" & vbCrLf & _
                "wsh.SendKeys ""{ENTER}""" & vbCrLf & _
                "Set wsh = Nothing"
    '---Save the VBscript String to file---
    sFileName = wsh.ExpandEnvironmentStrings("%Temp%") & "\zz_automation.vbs"
    Open sFileName For Output As #1
    Print #1, strScript
    Close #1
    '---Execute the VBscript file asynchronously---
    wsh.Run """" & sFileName & """"
    Set wsh = Nothing
End Sub
Ezzat Elbadrawy
  • 142
  • 1
  • 3