Microsoft Office Tutorials and References
In Depth Information
‘Open the Word document
Set wddoc = WdApp.Documents.Open(Filename:=”C:\Your\File\Path\myWordDoc.docx”)
‘Copy paragraph 2
‘Activate your workbook and go to the paste destination of Sheet1 cell A1.
‘Paste paragraph 2 from the Word document.
‘Close the Word document, no need to save changes.
‘Quit the Word application.
‘Set the Object variables to Nothing to release system memory.
Set wddoc = Nothing
Set WdApp = Nothing
In this lesson, you write a macro that asks for the name of a Word document and opens that Word
document if it exists in a particular folder.
For this lesson, you write a macro that uses an InputBox to ask for the name of a Word document
to be opened from a predetermined folder path. If the Word document exists, it is opened, but if it
does not exist, the user is advised of that. To get the sample database files you can download Lesson
From any worksheet in your Excel workbook, press Alt+F11 to go to the Visual Basic Editor.
From the VBE menu, click Insert ➪ Module.
In the module you just created, type Sub OpenRequestedWordDoc and press Enter. VBA will
automatically place a pair of empty parentheses at the end of the Sub line, followed by an
empty line, and the End Sub line below that. Your macro will look like this so far:
Declare a String type variable for the predetermined folder path:
Dim myPath As String
Define the String type variable for the example folder path:
myPath = “C:\Your\File\Path\”