Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening a word doc in current folder as my vbs file

Status
Not open for further replies.

Mingas

Programmer
Jul 10, 2013
15
ZA
Please help

I would like to open a word doc that's in the same folder as my vbs...i don't want to have to specify the whole path. What I have is the following:

set word = createobject("word.application")
word.visible=true
Word.Documents.Open "C:\Whatever\Other\max.docm"


I have tried:

set word = createobject("word.application")
word.visible=true
Word.Documents.Open "max.docm" and "\max.docm" and ".\max.docm"

none work...please help
 
The Word.application object does not know where your script was run from, so you need to include a path. You can get the path the script is running from with the code below:
Code:
Set objShell = CreateObject("Wscript.Shell")
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile) 

Set word = createobject("word.application")
word.visible=true
Word.Documents.Open strFolder & "\" & "myfile.doc"
 
Another way to get the scripts current directory:
Code:
CurrentDirectory = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top