MDSS
asked on
VBA Shell can't Find Word document
I have an Access application that opens a Word document using this code:
x=Shell("C:\Program Files (x86)\Microsoft Office\root\Office16\WINWO RD.EXE C:\Test\Test.doc",1)
This works fine for all machines except 4 that are on a separate network ... It says it can't find the document ... but the document is present and opens manually.
What's going on?
x=Shell("C:\Program Files (x86)\Microsoft Office\root\Office16\WINWO
This works fine for all machines except 4 that are on a separate network ... It says it can't find the document ... but the document is present and opens manually.
What's going on?
Just a small thought, I often find it easier to add the needed double quotes with a small helper function rather than using embedded pairs of double quotes. I find it a little cleaner and typically avoid at least one or two mistakes in building the quoted substrings in a larger string field. But naturally personal taste comes into play here. I also like to build the full command line in a variable before passing it to the Shell command, makes it a little easier to read and debug. Again just thoughts to consider.
Command = Quote("C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE") & " " & Quote("C:\Test\Test.doc")
x = Shell(Command, 1)
Function Quote(s As String) As String
Quote = Chr(34) & s & Chr(34)
End Function
~bp
Is the file stored on the remote server?
ASKER
Neither of these suggestions made a difference .... it still opens Word and says it can't find the file C:\Test\Test.doc. ... (the file is located on the local C: drive by the way)
What's puzzling is that this syntax works on hundreds of other computers, just not these 4 ... I thought perhaps Office needed to be repaired, but it would be strange if all 4 had that problem. It's like something in their environment or a setting is keeping the file from being seen.
What's puzzling is that this syntax works on hundreds of other computers, just not these 4 ... I thought perhaps Office needed to be repaired, but it would be strange if all 4 had that problem. It's like something in their environment or a setting is keeping the file from being seen.
Will
? Dir("C:\Test\Test.doc", vbNormal)
return the filename?
/gustav
? Dir("C:\Test\Test.doc", vbNormal)
return the filename?
/gustav
Is the path to winword.exe the same on those four machines as it is on the other machines?
The default installation path is:
C:\Program Files (x86)\Microsoft Office\Office16\WINWORD.EX E
not
C:\Program Files (x86)\Microsoft Office\root\Office16\WINWO RD.EXE
Perhaps on them the default settings were used.
The default installation path is:
C:\Program Files (x86)\Microsoft Office\Office16\WINWORD.EX
not
C:\Program Files (x86)\Microsoft Office\root\Office16\WINWO
Perhaps on them the default settings were used.
It might be safer to determine the path to the Word EXE dynamically at runtime rather than hard coding, using something like this. Naturally you would want some error checking added to make sure Word was actually installed and you got a path to the EXE.
Dim objShell As Object
Dim strWordExe As String
Set objShell = CreateObject("WScript.Shell")
strWordExe = objShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Winword.exe\")
Shell (strWordExe & " " & """C:\Test\Test.doc""")
~bp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Problem solved ... based on the suggestion to go to DOS prompt and doing the directory listing I discovered the file name had been changed to C:\Test\test.doc.docx .... renaming the file solved the issue.
Thanks for the tip
Thanks for the tip
Well, that was my suggestion
https://www.experts-exchange.com/questions/28983194/VBA-Shell-can't-Find-Word-document.html?anchorAnswerId=41889306#a41889306
but never mind.
/gustav
https://www.experts-exchange.com/questions/28983194/VBA-Shell-can't-Find-Word-document.html?anchorAnswerId=41889306#a41889306
but never mind.
/gustav
When using paths with spaces, you should add quote characters around the respective strings, that is, the program path and the document path. You can create a single character quote inside a string by using two consecutive double quotes.
So try
Open in new window
That aside, to exclude an error that might be way too obvious: you made sure that the machines in question have the correct Office version (as specified in the program path) installed?