Open an Existing Word Document from Excel using VBA. You can open an existing DOC file (a word document), using the above methods. Instead of using documents.Add method, you’ll have to use documents.Open method. Simply tell the method, which file to open (give it a filename). Sep 18, 2013 My way of saying that I know very little about VBS. In this instance, I needed to create and call a script from within Excel VBA that will open a Word document on a server path, and then save all my Excel files and close them. The Word doc is a file update menu for my users and has several macros in it that require Excel to be completely closed.
VBScript Scripting Techniques > OLE Automation > MS Word
|
SaveAs Use MS Word to convert Word documents to HTML | |
---|---|
VBScript Code: | |
Option Explicit Doc2HTML 'C:Documents and SettingsMyUserIDMy Documentsresume.doc' Sub Doc2HTML( myFile ) ' This subroutine opens a Word document, ' then saves it as HTML, and closes Word. ' If the HTML file exists, it is overwritten. ' If Word was already active, the subroutine ' will leave the other document(s) alone and ' close only its 'own' document. ' ' Written by Rob van der Woude ' http://www.robvanderwoude.com ' Standard housekeeping Dim objDoc, objFile, objFSO, objWord, strFile, strHTML Const wdFormatDocument = 0 Const wdFormatDocument97 = 0 Const wdFormatDocumentDefault = 16 Const wdFormatDOSText = 4 Const wdFormatDOSTextLineBreaks = 5 Const wdFormatEncodedText = 7 Const wdFormatFilteredHTML = 10 Const wdFormatFlatXML = 19 Const wdFormatFlatXMLMacroEnabled = 20 Const wdFormatFlatXMLTemplate = 21 Const wdFormatFlatXMLTemplateMacroEnabled = 22 Const wdFormatHTML = 8 Const wdFormatPDF = 17 Const wdFormatRTF = 6 Const wdFormatTemplate = 1 Const wdFormatTemplate97 = 1 Const wdFormatText = 2 Const wdFormatTextLineBreaks = 3 Const wdFormatUnicodeText = 7 Const wdFormatWebArchive = 9 Const wdFormatXML = 11 Const wdFormatXMLDocument = 12 Const wdFormatXMLDocumentMacroEnabled = 13 Const wdFormatXMLTemplate = 14 Const wdFormatXMLTemplateMacroEnabled = 15 Const wdFormatXPS = 18 Const wdFormatOfficeDocumentTemplate = 23 (1) Const wdFormatMediaWiki = 24(1) (2) ' Create a File System object Set objFSO = CreateObject( 'Scripting.FileSystemObject' ) ' Create a Word object Set objWord = CreateObject( 'Word.Application' ) With objWord ' True: make Word visible; False: invisible .Visible = True ' Check if the Word document exists If objFSO.FileExists( myFile ) Then Set objFile = objFSO.GetFile( myFile ) strFile = objFile.Path Else WScript.Echo 'FILE OPEN ERROR: The file does not exist' & vbCrLf ' Close Word .Quit Exit Sub End If ' Build the fully qualified HTML file name strHTML = objFSO.BuildPath( objFile.ParentFolder, _ objFSO.GetBaseName( objFile ) & '.html' ) ' Open the Word document .Documents.Open strFile ' Make the opened file the active document Set objDoc = .ActiveDocument ' Save as HTML objDoc.SaveAs strHTML, wdFormatFilteredHTML ' Close the active document objDoc.Close ' Close Word .Quit End With End Sub | |
Requirements: | |
Windows version: | any |
Network: | any |
Client software: | MS Word |
Script Engine: | any |
Summarized: | Works in any Windows version, as long as MS Word is installed. |
[Back to the top of this page] | |
SaveAs Use MS Word to convert Word documents to PDF | |
VBScript Code: | |
Option Explicit Doc2PDF 'C:Documents and SettingsMyUserIDMy Documentsresume.doc' Sub Doc2PDF( myFile ) ' This subroutine opens a Word document, then saves it as PDF, and closes Word. ' If the PDF file exists, it is overwritten. ' If Word was already active, the subroutine will leave the other document(s) ' alone and close only its 'own' document. ' ' Requirements: ' This script requires the 'Microsoft Save as PDF or XPS Add-in for 2007 ' Microsoft Office programs', available at: ' http://www.microsoft.com/downloads/details.aspx? ' familyid=4D951911-3E7E-4AE6-B059-A2E79ED87041&displaylang=en ' ' Written by Rob van der Woude ' http://www.robvanderwoude.com ' Standard housekeeping Dim objDoc, objFile, objFSO, objWord, strFile, strPDF Const wdFormatDocument = 0 Const wdFormatDocument97 = 0 Const wdFormatDocumentDefault = 16 Const wdFormatDOSText = 4 Const wdFormatDOSTextLineBreaks = 5 Const wdFormatEncodedText = 7 Const wdFormatFilteredHTML = 10 Const wdFormatFlatXML = 19 Const wdFormatFlatXMLMacroEnabled = 20 Const wdFormatFlatXMLTemplate = 21 Const wdFormatFlatXMLTemplateMacroEnabled = 22 Const wdFormatHTML = 8 Const wdFormatPDF = 17 Const wdFormatRTF = 6 Const wdFormatTemplate = 1 Const wdFormatTemplate97 = 1 Const wdFormatText = 2 Const wdFormatTextLineBreaks = 3 Const wdFormatUnicodeText = 7 Const wdFormatWebArchive = 9 Const wdFormatXML = 11 Const wdFormatXMLDocument = 12 Const wdFormatXMLDocumentMacroEnabled = 13 Const wdFormatXMLTemplate = 14 Const wdFormatXMLTemplateMacroEnabled = 15 Const wdFormatXPS = 18 Const wdFormatOfficeDocumentTemplate = 23 (1) Const wdFormatMediaWiki = 24 (1) (2) ' Create a File System object Set objFSO = CreateObject( 'Scripting.FileSystemObject' ) ' Create a Word object Set objWord = CreateObject( 'Word.Application' ) With objWord ' True: make Word visible; False: invisible .Visible = True ' Check if the Word document exists If objFSO.FileExists( myFile ) Then Set objFile = objFSO.GetFile( myFile ) strFile = objFile.Path Else WScript.Echo 'FILE OPEN ERROR: The file does not exist' & vbCrLf ' Close Word .Quit Exit Sub End If ' Build the fully qualified HTML file name strPDF = objFSO.BuildPath( objFile.ParentFolder, _ objFSO.GetBaseName( objFile ) & '.pdf' ) ' Open the Word document .Documents.Open strFile ' Make the opened file the active document Set objDoc = .ActiveDocument ' Save as HTML objDoc.SaveAs strPDF, wdFormatPDF ' Close the active document objDoc.Close ' Close Word .Quit End With End Sub | |
Requirements: | |
Windows version: | any |
Network: | any |
Client software: | MS Word 2007 and the Microsoft Save as PDF or XPS Add-in for 2007 Microsoft Office programs |
Script Engine: | any |
Summarized: | Works in any Windows version, as long as MS Word 2007 and the Microsoft Save as PDF or XPS Add-in for 2007 Microsoft Office programs are installed. |
[Back to the top of this page] | |
SaveAs Use MS Word to convert Word documents to RTF | |
VBScript Code: | |
Option Explicit Doc2RTF 'C:Documents and SettingsMyUserIDMy Documentsresume.doc' Sub Doc2RTF( myFile ) ' This subroutine opens a Word document, then saves it as RTF, and closes Word. ' If the RTF file exists, it is overwritten. ' If Word was already active, the subroutine will leave the other document(s) ' alone and close only its 'own' document. ' ' Written by Rob van der Woude ' http://www.robvanderwoude.com ' Standard housekeeping Dim objDoc, objFile, objFSO, objWord, strFile, strRTF Const wdFormatDocument = 0 Const wdFormatDocument97 = 0 Const wdFormatDocumentDefault = 16 Const wdFormatDOSText = 4 Const wdFormatDOSTextLineBreaks = 5 Const wdFormatEncodedText = 7 Const wdFormatFilteredHTML = 10 Const wdFormatFlatXML = 19 Const wdFormatFlatXMLMacroEnabled = 20 Const wdFormatFlatXMLTemplate = 21 Const wdFormatFlatXMLTemplateMacroEnabled = 22 Const wdFormatHTML = 8 Const wdFormatPDF = 17 Const wdFormatRTF = 6 Const wdFormatTemplate = 1 Const wdFormatTemplate97 = 1 Const wdFormatText = 2 Const wdFormatTextLineBreaks = 3 Const wdFormatUnicodeText = 7 Const wdFormatWebArchive = 9 Const wdFormatXML = 11 Const wdFormatXMLDocument = 12 Const wdFormatXMLDocumentMacroEnabled = 13 Const wdFormatXMLTemplate = 14 Const wdFormatXMLTemplateMacroEnabled = 15 Const wdFormatXPS = 18 Const wdFormatOfficeDocumentTemplate = 23 (1) Const wdFormatMediaWiki = 24 (1) (2) ' Create a File System object Set objFSO = CreateObject( 'Scripting.FileSystemObject' ) ' Create a Word object Set objWord = CreateObject( 'Word.Application' ) With objWord ' True: make Word visible; False: invisible .Visible = True ' Check if the Word document exists If objFSO.FileExists( myFile ) Then Set objFile = objFSO.GetFile( myFile ) strFile = objFile.Path Else WScript.Echo 'FILE OPEN ERROR: The file does not exist' & vbCrLf ' Close Word .Quit Exit Sub End If ' Build the fully qualified HTML file name strRTF = objFSO.BuildPath( objFile.ParentFolder, _ objFSO.GetBaseName( objFile ) & '.rtf' ) ' Open the Word document .Documents.Open strFile ' Make the opened file the active document Set objDoc = .ActiveDocument ' Save as HTML objDoc.SaveAs strRTF, wdFormatRTF ' Close the active document objDoc.Close ' Close Word .Quit End With End Sub | |
Requirements: | |
Windows version: | any |
Network: | any |
Client software: | MS Word |
Script Engine: | any |
Summarized: | Works in any Windows version, as long as MS Word is installed. |
[Back to the top of this page] | |
SaveAs Use MS Word to convert Word documents to XPS (XML PaperSpecification) | |
VBScript Code: | |
Option Explicit Doc2XPS 'C:Documents and SettingsMyUserIDMy Documentsresume.doc' Sub Doc2XPS( myFile ) ' This subroutine opens a Word document, then saves it as XPS, and closes Word. ' If the XPS file exists, it is overwritten. ' If Word was already active, the subroutine will leave the other document(s) ' alone and close only its 'own' document. ' ' Requirements: ' This script requires the 'Microsoft Save as PDF or XPS Add-in for 2007 ' Microsoft Office programs', available at: ' http://www.microsoft.com/downloads/details.aspx? ' familyid=4D951911-3E7E-4AE6-B059-A2E79ED87041&displaylang=en ' ' Written by Rob van der Woude ' http://www.robvanderwoude.com ' Standard housekeeping Dim objDoc, objFile, objFSO, objWord, strFile, strXPS Const wdFormatDocument = 0 Const wdFormatDocument97 = 0 Const wdFormatDocumentDefault = 16 Const wdFormatDOSText = 4 Const wdFormatDOSTextLineBreaks = 5 Const wdFormatEncodedText = 7 Const wdFormatFilteredHTML = 10 Const wdFormatFlatXML = 19 Const wdFormatFlatXMLMacroEnabled = 20 Const wdFormatFlatXMLTemplate = 21 Const wdFormatFlatXMLTemplateMacroEnabled = 22 Const wdFormatHTML = 8 Const wdFormatPDF = 17 Const wdFormatRTF = 6 Const wdFormatTemplate = 1 Const wdFormatTemplate97 = 1 Const wdFormatText = 2 Const wdFormatTextLineBreaks = 3 Const wdFormatUnicodeText = 7 Const wdFormatWebArchive = 9 Const wdFormatXML = 11 Const wdFormatXMLDocument = 12 Const wdFormatXMLDocumentMacroEnabled = 13 Const wdFormatXMLTemplate = 14 Const wdFormatXMLTemplateMacroEnabled = 15 Const wdFormatXPS = 18 Const wdFormatOfficeDocumentTemplate = 23 (1) Const wdFormatMediaWiki = 24 (1) (2) ' Create a File System object Set objFSO = CreateObject( 'Scripting.FileSystemObject' ) ' Create a Word object Set objWord = CreateObject( 'Word.Application' ) With objWord ' True: make Word visible; False: invisible .Visible = True ' Check if the Word document exists If objFSO.FileExists( myFile ) Then Set objFile = objFSO.GetFile( myFile ) strFile = objFile.Path Else WScript.Echo 'FILE OPEN ERROR: The file does not exist' & vbCrLf ' Close Word .Quit Exit Sub End If ' Build the fully qualified XPS file name strXPS = objFSO.BuildPath( objFile.ParentFolder, _ objFSO.GetBaseName( objFile ) & '.xps' ) ' Open the Word document .Documents.Open strFile ' Make the opened file the active document Set objDoc = .ActiveDocument ' Save in XML Paper Specification (XPS) format objDoc.SaveAs strXPS, wdFormatXPS ' Close the active document objDoc.Close ' Close Word .Quit End With End Sub | |
Requirements: | |
Windows version: | any |
Network: | any |
Client software: | MS Word 2007 and the Microsoft Save as PDF or XPS Add-in for 2007 Microsoft Office programs |
Script Engine: | any |
Summarized: | Works in any Windows version, as long as MS Word 2007 and the Microsoft Save as PDF or XPS Add-in for 2007 Microsoft Office programs are installed. |
[Back to the top of this page] |
This demo script can be downloaded here.
The downloadable version also contains other file filter constants.
The downloadable version also contains other file filter constants.
Notes | 1: | Constants found with 'brute force' approach by Scott Ness |
2: | Requires Microsoft Office Word Add-in For MediaWiki |