Microsoft Office Tutorials and References
In Depth Information
Importing a Word Document to Excel
Dim WdApp As Object, wddoc As Object
‘Open Word
Set WdApp = CreateObject(“Word.Application”)
‘Open the Word document to be printed.
Set wddoc = WdApp.Documents.Open(Filename:=”C:\Your\File\Path\myWordDoc.docx”)
‘Print the entire Word document.
‘Give the print job 5 seconds to complete before closing Word.
Application.Wait Now + TimeSerial(0, 0, 5)
‘Close the Word document, no need to save changes.
wddoc.Close savechanges:=False
‘Quit the Word application.
‘Set the Object variables to Nothing to release system memory.
Set wddoc = Nothing
Set WdApp = Nothing
End Sub
You might have noticed that this macro, and a couple of others in this lesson, do
not include the statement to make the Word application visible. It’s easy to
forget that you have an open application if you cannot see it. The point to be made
is, do remember to include the Close and Quit statements in your macros when
opening applications. Otherwise, you’ll get read-only messages, and then error
messages when rerunning the macro, which to VBA will be interpreted as an
attempt to re-open a file that is already open.
If you want to print only a portion of the Word document, for example only page 2, then in the
preceding PrintWordDoc macro, substitute the statement
WdApp.ActiveDocument.PrintOut pages:=”2”
iMporTing A Word docuMEnT To ExcEl
There may be times when you want to import some text from Word into Excel. Admittedly this is
not a common task, because Excel cells are not meant to serve as word processing instruments for
extensive amounts of text. But because it’s possible, here’s a macro that opens a Word document,
copies the second paragraph, and pastes that text into cell A1 of Sheet1:
Sub ImportToExcelFromWord()
‘Declare object variables.
Dim WdApp As Object, wddoc As Object
‘Open Word
Set WdApp = CreateObject(“Word.Application”)
Search JabSto ::

Custom Search