3

I am trying to devise a code that opens specific URL and at specific point, I encountered OPEN dialog that is related to that website .. It is like that enter image description here

Now what I am stuck at is that I need to paste specific path like that "C:\Users\User\Desktop\Pics(423).jpg" to the file name field then click on the Open button

And I am also stuck at how to copy the path in memory using clipboard .. Thanks advanced for help

2
  • Looks like an upload? If there is an input field of type "file" you can use sendKeys method to send the path of the file.... then submit the form. Commented Dec 19, 2019 at 19:09
  • Thanks a lot. I am trying to avoid using sendKeys .. I am searching for a way to interact the open dialog if possible. Commented Dec 19, 2019 at 19:17

1 Answer 1

1

If you want to avoid the use of the SendKeys method, you could use the UIAutomationClient library or the SendMessage function from user32.dll. These would allow you to use the Windows API to interact with the dialog window.

There is not a lot of online resources on the UIAutomationClient library in VBA, but this video (and part 2) should be good enough to get you started.

EDIT: However, if you decide to use SendMessage, here's an example of how to implement it:

1) Put the following at the top of the code module

Option Explicit

Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, ByVal lParam As String) As LongPtr
Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr

Private Const BM_CLICK = &HF5
Private Const WM_SETTEXT = &HC

2) Add the following sub procedure and change the value of FileName to the desired value

Sub FillFileNameAndPressOpenButton()

    Dim FileName As Variant
    FileName = "(423)" 'Change this to the appropriate file name

    Dim WindowCaption As String
    WindowCaption = "Open"

    Dim OpenDialogHandle As LongPtr
    OpenDialogHandle = FindWindow(vbNullString, WindowCaption)

    If OpenDialogHandle = 0 Then
        MsgBox "Couldn't find the Window"
        Exit Sub
    End If

    Dim TextBoxHandle As LongPtr, tempHandle As LongPtr
    tempHandle = FindWindowEx(OpenDialogHandle, 0, "ComboBoxEx32", vbNullString)
    tempHandle = FindWindowEx(tempHandle, 0, "ComboBox", vbNullString)
    TextBoxHandle = FindWindowEx(tempHandle, 0, "Edit", vbNullString)

    If TextBoxHandle = 0 Then
        MsgBox "Couldn't find the textbox"
        Exit Sub
    End If

    Dim Rslt As LongPtr
    Rslt = SendMessage(TextBoxHandle, WM_SETTEXT, 0, FileName)
    DoEvents

    Dim ButtonHandle As LongPtr, ButtonCaption As String
    ButtonCaption = "&Open"
    ButtonHandle = FindWindowEx(OpenDialogHandle, 0, "Button", ButtonCaption)

    If ButtonHandle = 0 Then
        MsgBox " Couldn't find the button"
        Exit Sub
    End If

    Rslt = SendMessage(ButtonHandle, BM_CLICK, 0, 0)
    DoEvents

End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. I have watched the videos and in fact this is something total new to me. I tried to make use of it but it seems advanced for me at this point.
@YasserKhalil - I've added an example using the SendMessage function. Let me know if that is more helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.