Consulting

Results 1 to 10 of 10

Thread: VBScript to create Word

  1. #1
    Administrator
    VP-Knowledge Base
    VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location

    Question VBScript to create Word

    Hi,

    I wonder if its possible to programmaticaly create a Word document with macros in it, using only WSH (Windows Script Hosting) and VBScript.

    I have created it already, but cant insert the macro. Sure I could do this by using a model with the macro in it, but my doubt is if its possible to create it all by code.

    My code is:
    [VBA]
    Dim objWord

    Set objWord = CreateObject ( "Word.Basic" )

    objWord.FileNew ( "Normal" )

    objWord.Insert "WORD TEST"

    objWord.FileSaveAs ( "c:\tstWord.doc" )

    objWord.FileClose

    [/VBA]
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  2. #2
    VBAX Regular macleanb's Avatar
    Joined
    Feb 2005
    Location
    Essex
    Posts
    23
    Location
    Hi

    I am fairly sure this must be possible (well it is in Excel so I am guessing it will be in Word too) Take a look at this article which takes you all round the VBE objects:


    http://www.cpearson.com/excel/vbe.htm

  3. #3
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Are you trying to do something like this?
    [vba]
    Dim objWord
    Dim Doc
    Dim Modul
    Dim MyText

    Set objWord = CreateObject ( "Word.Application" )
    Set Doc = Objword.Documents.Add
    Set Modul = Doc.VBProject.VBComponents.Add(1)

    MyText = "Line1" & vbNewLine & _
    "Line2" & vbNewLine & _
    "Line3" & vbNewLine & _
    "Line4" & vbNewLine

    Modul.CodeModule.AddFromString (MyText)

    Doc.SaveAs "c:\tstWord.doc"
    Doc.Close
    objword.Quit
    [/vba]

  4. #4
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    hi Jake,

    exactly but the VBComponents that works on Excel, dont work on WSH .
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  5. #5
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    It works with VBScript.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Hi Jake,

    not when using Windows Script Host (WSH). It generates the error 800A17B4: Access by programming to Visual Basic not trusted.

    There is nothing I can do to avoid it, not even lowering security to low. Try it by pasting it in a file and calling it word.vbs, then run it.
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  7. #7
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    It runs fine for me as a vbs file. You need to allow access to the Visual Basic Project in Word.

    • Tools

    • Macros

    • Security...

    • Trusted Publishers

    • Trust access to Visual Basic Project

  8. #8
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Hi Jake,

    I did that and got the same result.
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  9. #9
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Make sure the setting is saved. Open Word and allow access to the VB project. Save and Close. Re-open and make sure that the setting was saved. Then try to run the vbs file.

  10. #10
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Hi Jake,

    I did it and still getting the same result. Any idea on what may be my problem??
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

Posting Permissions

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