0

I am trying to attach a file to an email body (Well at least bring up the prompt, because i've read it's extremely difficult to interact with the pop up box, unless someone has a way to do that.) and I need to click the "attach" button, and then click the "Browse this computer" button. My code is being slowed down because it has to iterate through the 500+ "Buttons" on the sheet, and then there is 2 "attach" buttons. Neither of them have an ID associated with them that I can use. Can someone help me figure out how to make this code more efficient and to end the For Each loop as soon as it finds the FIRST "Attach" button?

Set attach = ie.Document.GetElementsByTagName("button")
For Each Button In attach
If Button.Name = "Attach" Then
Button.Click
End If
Next


Set attach = ie.Document.GetElementsByTagName("button")
For Each Button In attach
If Button.Name = "Browse this computer" Then
Button.Click
End If
Next
braX
  • 11,506
  • 5
  • 20
  • 33
Lzypenguin
  • 945
  • 1
  • 7
  • 18
  • 1
    `Exit For` will exit the loop if you put it after the `Click` line – Tim Williams Jan 26 '20 at 07:40
  • @TimWilliams This is perfect. I did not realize I could put an Exit for within the If statement. This will certainly speed up my code. Thanks – Lzypenguin Jan 26 '20 at 07:42
  • I'm not sure what email service you're using but when you click the "Attach" button, is that just unhiding the "Browse this computer button"? If so, try just clicking the second button, and ignoring the first. Long shot... – David Jan 26 '20 at 09:44
  • @David The "Browse this computer" button is not clickable until I click Attach. Using the Exit For has sped up my code dramatically. Now the problem I am facing is trying to interact with the dialog box that pops up. I have tried using sendkeys, but it wont send anything until I close the box. Can't find very much info about how to interact with the window either. – Lzypenguin Jan 26 '20 at 09:52
  • Everything is against you interacting with upload dialogs, sadly... you could try calling an external vbs straight after hitting the "Browse..." button... the external vbs could have a 1 second delay and then perform the sendkeys... test it with set text, and if it works you can look at programmatically generating/running/deleting the vbs. Not sure if it'll work though. – David Jan 26 '20 at 09:59
  • @TimWilliams Unless you had multiple with the name "Attach"! – JGFMK Jan 26 '20 at 10:02
  • @JGFMK - OP specifically asked how to exit after finding the first button. – Tim Williams Jan 26 '20 at 17:03
  • You'd likely need to use Windows API calls to navigate the browse dialog. – Tim Williams Jan 26 '20 at 17:04
  • @TimWilliams You are correct, i wanted to exit the loop after finding the FIRST instance, because there were multiple. Your solution worked perfectly. Also, can you give me a little more detail about how i would use the Windows API calls to navigate to and interact with the browse dialog box? – Lzypenguin Jan 27 '20 at 00:40
  • I've never used Win API to do that, so I can't help with that. Maybe look at https://stackoverflow.com/questions/34834857/vbawindows-api-how-to-assign-file-path-to-open-windows – Tim Williams Jan 27 '20 at 00:50

0 Answers0