Microsoft Office Tutorials and References
In Depth Information
Try It
Release the reserved memory in VBA for the declared Object type variables now that they
have served their purpose and are no longer needed:
Set wdDoc = Nothing
Set appWord = Nothing
Go ahead and test your macro, which will look like this in its entirety:
Sub OpenRequestedWordDoc()
‘Declare a String variable for the predetermined folder path.
Dim myPath As String
‘Define the String variable with the example folder path.
myPath = “C:\Your\File\Path\”
‘Declare a String variable for the anticipated InputBox entry.
Dim myFileName As String
‘Show the InputBox to ask the user for the name of the Word
‘document they want to open 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?”, _
‘Exit the macro if nothing is entered or the Cancel button is clicked.
If myFileName = “” Then Exit Sub
‘Declare a String variable for the combined folder path
‘and document name.
Dim myDocName As String
‘Define the String variable for the combined folder path
‘and document name.
myDocName = myPath & myFileName
‘Check to see if the Word document name actually exists
‘in the folder path.
‘If it does not, then 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
‘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
Search JabSto ::

Custom Search