1

I am trying to automate file upload on chrome, getting error here :method run object iwshshell3 failed" please help:

Dim Customer_rates As String

Dim WshShell As Object

Customer_rates = "D:\FX Exch. Rates\2022-Feb-24 1707\MP_customer_exchange_rates_sample.xlsx"

Set WshShell = CreateObject("WScript.Shell")
  
    WshShell.Run "cmd.exe/c echo" & Customer_rates & "| clip", vbNormal, True
    WshShell.SendKeys "^{v}"
    Application.Wait DateAdd("S", 2, Now)
    WshShell.SendKeys "{ENTER}"
FunThomas
  • 23,043
  • 3
  • 18
  • 34
  • Why would you bend over backwards to copy the filename to the clipboard and `.SendKeys()` CTRL+V, when you could `.SendKeys()` the actual filename? – Tomalak Feb 25 '22 at 11:02
  • you mean like this : WshShell.SendKeys Customer_rates in place of CTR+V? didnt work – Stoyan Bozhkov Feb 25 '22 at 13:46
  • Yes, exactly. A couple of characters have special meaning for SendKeys - you must escape `+`, `[`, `]`, `^`, `%`, `~`, `{`, `}`, `(`, `)` as `{+}`, `{[}`, `{]}`, `{^}`, `{%}`, `{~}`, `{{}`, `{}}`, `{(}`, `{)}`, respectively - but other than that you can send the filename directly with `SendKeys` and skip the clipboard entirely. You could make a small helper function e.g. `Function MakeSafeForSendKeys(string)` that does these replacements and returns a safe value. Then you can call `WshShell.SendKeys MakeSafeForSendKeys(Customer_rates)` and it should work. – Tomalak Feb 25 '22 at 14:00
  • This function you could then re-use to directly type other values into other fields. – Tomalak Feb 25 '22 at 14:02

3 Answers3

0

Think about how this would appear in the console. The file path has spaces. So it will require quotes around it when you run it. Something like:

WshShell.Run "cmd.exe/c echo" & chr(34) & Customer_rates & chr(34) & "| clip", vbNormal, True
Sivakanesh
  • 817
  • 4
  • 13
  • 36
0

Thansk guys i did a workaround of cmd with this sub and it semms to work:

Sub StoreData() Dim varText As String Dim objCP As Object varText = "D:\FX Exch. Rates\2022-Feb-24 1707\MP_customer_exchange_rates_sample.xlsx" Set objCP = CreateObject("HtmlFile") objCP.ParentWindow.ClipboardData.SetData "text", varText End Sub

0

Try to always work with absolute paths (program and arguments). Be aware of quotes. I preferably use chr(13)

Athena
  • 1