//flex table opened by JP

Click to See Complete Forum and Search --> : Weird VBA behavior in Word 2003


Bazango
02-14-2006, 04:24 PM
I am trying to apply my Word 2003's scripting behavior to bulk convert some Word based HTML to text. I first started with Windows Script Hosting, had some problems and finally worked my way to Word's internal scripting capability and had the same problem.

Here is the subroutine as I run it in Word:

Sub OpenDoc2()

Documents.Open _
ReadOnly:=False, _
FileName:="C:\....\MyDocFile.doc", _
Format:=wdOpenFormatDocument

ActiveDocument.SaveAs _
FileName:="C:\...\MyTextFile.txt", _
FileFormat:=wdSaveFormatText

On Error GoTo errorHandler

ActiveDocument.Close

errorHandler:
If Err = 4198 Then MsgBox "Document was not closed"

End Sub

The important part is in red. Nothing happens. When I open MyTextFile.txt, I see a lot of gibberish. If I open the document in Word, the original formatting and font are intact. If I save the file as Plain Text from Word the usual way, I get plain text.

Here is how to duplcate this effort:

Open Word with a new document.

Open Word which will load a new document.
Use the "Tools" menu, go to Macro and choose Visual Basic Editor -- you might have to go back and install it.
From the VB Editor, select the Insert menu and choose Class Module.
Cut and paste the code I include here.
Create a test document in word and place the location and name in the correct C:/.../MyDocFile.doc location in the code.
Insert a location and name for the file you want the macro to create in the correct C:/.../MyTextFile.txt location in the code.
Save the document.
In the Run menu choose Run to run the Macro.


Any and all tips or clues would be appreciated.

Bazango
02-15-2006, 03:16 PM
A little more tinkering finds that the following code works as advertised in Word's Visual Basic for Applications capability:

Sub Works()

Set Doc = Documents.Open( _
ReadOnly:=False, _
FileName:=":="C:\....\MyDocFile.doc" _
)

Doc.SaveAs _
FileName:="C:\...\MyTextFile.txt", _
FileFormat:=wdFormatText

Doc.Close

End Sub

But now back to Windows Script Hosting and I find a few new problems. First, the named argument functionality does not work and I can't find any reference to the reason in MSDN's on line documentation. And also, this adaptation to a Windows Script Hosting script has the same problem as the original:

' VB Script Document

Sub Works()

Set Word = CreateObject("Word.Application")

Set Doc = Word.Documents.Open( _
"C:\....\MyDocFile.doc" _
)

Doc.SaveAs _
"C:\...\MyTextFile.txt", _
wdFormatText

Doc.Close

End Sub

Works()

If you have Windows Script Hosting installed, you should be to place your MyDocFile.doc in the correct place and see a similar lack of results.

Anybody have any idea what is going on? Any and all tips or clues would be appreciated.

Bazango
02-15-2006, 03:57 PM
There is a typographical error above. Omit the red code below:

Sub Works()

Set Doc = Documents.Open( _
ReadOnly:=False, _
FileName:=":="C:\....\MyDocFile.doc" _
)

Doc.SaveAs _
FileName:="C:\...\MyTextFile.txt", _
FileFormat:=wdFormatText

Doc.Close

End Sub