Microsoft Office Tutorials and References
In Depth Information
Try It
‘Open the Word document
Set wddoc = WdApp.Documents.Open(Filename:=”C:\Your\File\Path\myWordDoc.docx”)
‘Copy paragraph 2
wddoc.Paragraphs(2).Range.Copy
‘Activate your workbook and go to the paste destination of Sheet1 cell A1.
Application.GotoThisWorkbook.Worksheets(“Sheet1”).Range(“A1”)
‘Paste paragraph 2 from the Word document.
ActiveSheet.Paste
‘Close the Word document, no need to save changes.
wddoc.Close Savechanges:=False
‘Quit the Word application.
WdApp.Quit
‘Set the Object variables to Nothing to release system memory.
Set wddoc = Nothing
Set WdApp = Nothing
End Sub
Try iT
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.
lesson requirements
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
27 from the book’s website at www.wrox.com .
step-by-step
1.
From any worksheet in your Excel workbook, press Alt+F11 to go to the Visual Basic Editor.
2.
From the VBE menu, click Insert Module.
3.
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:
Sub OpenRequestedWordDoc()
End Sub
4.
Declare a String type variable for the predetermined folder path:
Dim myPath As String
5.
Define the String type variable for the example folder path:
myPath = “C:\Your\File\Path\”
Search JabSto ::




Custom Search