use the shell command to open a word file in My Documents

M

Margaret Bartley

This works:
strCommand = "winword.exe C:\test.doc /mDoThis"
varMyDoc = Shell(strCommand, vbMaximizedFocus)

When I execute this, I get a file not found error:
strCommand = "winword.exe C:\Documents and Settings\mbartley\test.doc"
varMyDoc = Shell(strCommand, vbMaximizedFocus)

Same thing for this:
strCommand = "winword.exe 'C:\Documents and
Settings\mbartley\test.doc'"
varMyDoc = Shell(strCommand, vbMaximizedFocus)


My assumption is it's the spaces in the directory that are flummoxing the
compiler.

I'm modifying someone else's complicated code. This is a utility procedure,
and I don't want to change the overall logic by implementing a different
approach, I want to keep the Shell command logic, but don't know how to
implement it when I've got a space in the file name.

Any help?
 
J

Jay Freedman

This works:
strCommand = "winword.exe C:\test.doc /mDoThis"
varMyDoc = Shell(strCommand, vbMaximizedFocus)

When I execute this, I get a file not found error:
strCommand = "winword.exe C:\Documents and Settings\mbartley\test.doc"
varMyDoc = Shell(strCommand, vbMaximizedFocus)

Same thing for this:
strCommand = "winword.exe 'C:\Documents and
Settings\mbartley\test.doc'"
varMyDoc = Shell(strCommand, vbMaximizedFocus)


My assumption is it's the spaces in the directory that are flummoxing the
compiler.

I'm modifying someone else's complicated code. This is a utility procedure,
and I don't want to change the overall logic by implementing a different
approach, I want to keep the Shell command logic, but don't know how to
implement it when I've got a space in the file name.

Any help?

You need to embed double quotes in the string to surround the path:

Dim qt As String
qt = Chr$(34)
strCommand = "winword.exe " & qt & _
"C:\Documents and Settings\mbartley\test.doc" & qt

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
T

Thomas Lindberg

Jay Freedman said:
You need to embed double quotes in the string to surround the path:

Dim qt As String
qt = Chr$(34)
strCommand = "winword.exe " & qt & _
"C:\Documents and Settings\mbartley\test.doc" & qt

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

The need for double quotes in macros pops up now and then but I have not yet
found a so simple way to realize it!

Thanks, Jay!

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top