Saving Word doc as XML from VBScript

J

Jason Reichenbach

I am new to VBScript, and automating Word, so I'm probably doing something
dumb.

I have a series of Word documents that I need to save in XML format. There
are hundreds, so I need to do it automatically. I have the following VBScript
(clearly doesn't handle the hundreds issue, but it won't work on one file
yet):

'==========================
' Script starts
dim inFile
dim outfile

' Define intput and output file paths
inFile = "D:\My Documents\TestData\Test.doc"
outfile = "D:\My Documents\TestData\Test.xml"

' Start Microsoft Word...
dim w
set w = createobject("word.application")

' Visibility will ultimately be false
w.visible = true

' Open a document as read only
dim d
set d = w.documents.open (inFile, False, True)

' Save it as XML
d.SaveAs outfile, wdFormatXML

w.Quit

' Script ends
'===================

The output file gets saved to the correct name, but not in XML format. What
I end up with is a Word document with an .xml extension. If I rename the file
to have a .doc extension it opens in word.

When I do the same thing manually by opening the file then selecting File |
SaveAs and selecting the XML Document type option, it works as expected and I
get an actual XML output file.

What am I doing wrong, here?
Many thanks in advance.
 
J

Jason Reichenbach

OK, it is a dumb mistake... The symbol wdFormatXML has no meaning in the
context where it is being used: the vb script. It only has meaning inside the
Word application object.

So now the question is, can I access this constant by name from outside the
word application, so I can pass its value in the script. Its value happens to
be 11, but I'd rather access it by name from Word if possible.

Any ideas on this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top