Microsoft Office Tutorials and References
In Depth Information
Try It
6.
Declare a String type variable for the anticipated InputBox entry:
Dim myFileName As String
7.
Show the InputBox to ask the user for the name of the Word document to be opened from
the predetermined folder path:
myFileName = InputBox _
(“Enter the full Word document name to be opened” & Chr(10) & _
“from the folder path “ & myPath & “:”, _
“What file name with extension do you wish to open?”, _
“YourDocumentName.docx”)
8.
Exit the macro if nothing is entered or if the Cancel button is clicked:
If myFileName = “” Then Exit Sub
9.
Declare a String type variable for the combined folder path and document name:
Dim myDocName As String
10.
Define the String type variable for the combined folder path and document name:
myDocName = myPath & myFileName
11.
Check to see if the Word document name exists in the folder path. If it does not, advise the
user and exit the macro.
If Dir(myDocName) = “” Then
MsgBox “The file “ & myFileName & vbCrLf & _
“was not found in the folder path” & vbCrLf & _
myPath & “.”, _
vbExclamation, _
“No such animal.”
Exit Sub
End If
12.
At this point, the Word document is determined to exist in the folder. Declare Object
variables for the Word application and the Word document:
Dim appWord As Object, wdDoc As Object
13.
Using late binding in this example, create a Word application:
Set appWord = CreateObject(“Word.Application”)
14.
Make the created Word application visible:
appWord.Visible = True
15.
Open the requested Word document name:
Set wdDoc = appWord.Documents.Open(myDocName)
Search JabSto ::




Custom Search