Consulting

Results 1 to 5 of 5

Thread: Solved: Cannot "SaveAs' text using Word from Windows Script Hosting

  1. #1

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

    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.

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi and welcome to VBAX!

    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) [vba]
    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"
    [/vba]

    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!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    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!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  4. #4
    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/de...7e17ef0e05.asp

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    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!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •