PDA

View Full Version : Solved: Cannot "SaveAs' text using Word from Windows Script Hosting



SparceMatrix
02-15-2006, 01:36 PM
I am tryng to use Word to convert some documents from Word/HTML to text using Windows Script Hosting. I find that using this code in a Module in Word's VBA functionality works fine:


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 does not work:


' 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 I open MyTextFile.txt, I see a lot of gibberish -- not text and if I open the document in Word, it looks just like the original.

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

MOS MASTER
02-15-2006, 02:37 PM
Hi and welcome to VBAX! :hi:

The problem is Early binding verses late binding.
You're using Word VBA to do WSH that is not possible. (not in this form that is...)

WSH uses late binding which in this case means you have to clear out all the Word constants like "wdFormatText". (These are constants that you can only use in Word itself or by automating Word via early binding. (ref to Word library)

To get simple here:
In the VBE press F2 and paste "wdFormatText" in the search box and press Enter.

Below the objectbrowser you now see something like:
Const wdFormatText = 2
It's the 2 where after here cause in late binding we get the constant by its numerical equivalent in this case its 2. (So remember using late binding use the objectbrowser to find the numbers of the constants)

Also in late binding you can't use the "named arguments" like: ReadOnly:= (The prefix before your constant)

We just provide the values op the optional parameters of the function or method to access it directly (separated by commas)

So your sub could look like: (Paste below code in a txt file and rename extension to test.vbs)
Option Explicit
Dim oWord
Dim oDoc
Set oWord = CreateObject("Word.Application")
Set oDoc = oWord.Documents.Open("C:\test.doc")

With oDoc
.SaveAs "C:\test.txt", 2
.Close
End With

oWord.Quit

Set oDoc = Nothing
Set oWord = Nothing
msgbox "succes"


Now make sure a "C:\test.doc" exists and double click the test.vbs file.

Now all should be fine. I hope I've explained it ok for you cause my English isn't the best arround this site.

HTH! :whistle:

MOS MASTER
02-15-2006, 02:39 PM
Sorry when I reread your post I saw you also found out about the Named argument stuff. (missed that in the first read)

So the constant part should clean you up real nice.

Enjoy! :)

SparceMatrix
02-16-2006, 10:11 AM
That did it! Amazing. All I had to do was change wdFormatText to 2 and nothing else to fix my code. I had also tried "Word.FileConverters(2).SaveFormat" and that didn't work either. I was so close, I could have used the 2 and that would have done it. But, I never would have known why.

I tried some other approaches and got strange results like "type mismatch" for my objects that worked a moment before.

The "Option Explicit" was a mystery for me and I looked it up:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/29da309d-81ab-4bb4-ba4b-8c7e17ef0e05.asp

MOS MASTER
02-16-2006, 11:44 AM
Hi, :hi:

Glad I could help!

Option Explicit and dim instructions as well Set = Nothing instruction have to do with good programming habbits and will help you write better more readable code. (And code that runs faster with less errors)

HTH! :whistle: