Results 1 to 4 of 4

Thread: VB Script To Run Macro from Word

  1. #1
    Join Date
    Aug 2011
    Posts
    3

    unsure VB Script To Run Macro from Word

    Hello friends,

    I've been asked to convert about 700+ word files into Word HTML. As you know, the word document with HTML as its extension. Yes, you guessed it right, Save As HTML.

    I created a macro which saves a word document as HTML with the current file name and then closes it. Got this after some online research. This works great with a single open document. I need to automate it for all the 700 docs. I planned to open the word docs and call the macro using a batch or vbscript.

    Tried batch first. The batch i created opens just one word document, saves it as html and closes it. Then it opens a new word document, and not the files in the directory. Failed! I have the batch script at office, if you need it for reference, pls tell me. I will post it.

    Tried a vb script, but dont know how to add a variable to run it on all word docs in a directory structure, one by one. Here is the code:

    Code:
    Dim objSaveAs 
    
    Set objSaveAs = CreateObject("Word.Application") 
    
    on error resume next 
    
    With objSaveAs 
    
    .documents.Open (“D:\Test\Test.doc”) 
    
    .Run "Macro1" 
    
    .Quit 
    
    End With 
    
    Set objSaveAs = Nothing
    This will run the macro in word for Test.doc. I have many folders and subfolders, inside the folder 'Test', and many word docs in each. I want the batch or script to open each word doc, and run the macro, one by one. The macro will close each doc.

    Please help. I know we have many smart techies here. Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Posts
    605

    Re: VB Script To Run Macro from Word

    Check the below code to run a Macro of word document in VB Script:

    Code:
    Dim objWordApp As Word.Application
    Dim objWordDoc As Word.Document
    Set objWordApp = CreateObject("Word.Application")
    Set objWordDoc = WordApp.Documents.Open("C:\WordDoc.doc")
    objWordApp.Visible = True
    objWordApp.Run "Macro"
    objWordApp.Quit SaveChanges:=wdDoNotSaveChanges
    Set objWordApp = Nothing

  3. #3
    Join Date
    Aug 2011
    Posts
    3

    Re: VB Script To Run Macro from Word

    Quote Originally Posted by janos™ View Post
    Check the below code to run a Macro of word document in VB Script:

    Code:
    Dim objWordApp As Word.Application
    Dim objWordDoc As Word.Document
    Set objWordApp = CreateObject("Word.Application")
    Set objWordDoc = WordApp.Documents.Open("C:\WordDoc.doc")
    objWordApp.Visible = True
    objWordApp.Run "Macro"
    objWordApp.Quit SaveChanges:=wdDoNotSaveChanges
    Set objWordApp = Nothing
    Thank you very much for the reply. Will this work with every word doc in a given folder structure? As I see in the code, this applies to the file "C:\WordDoc.doc" alone right? I need it to work on every word document inside a folder tree. I will try this out tomorrow at office and let you know the results. Thanks again.

  4. #4
    Alpensopath Guest

    Re: VB Script To Run Macro from Word

    Click the Microsoft Office Button and then click Word Options.
    Click Popular.
    Under Top options for working with Word, select the Show Developer tab in the Ribbon check box.
    On the Developer tab, in the Code group, click Record Macro.
    Do one of the following:
    Begin recording To begin recording the macro without assigning it to a button on the Quick Access Toolbar or to a shortcut key, click OK.
    Create a button To assign the macro to a button on the Quick Access Toolbar, do the following:
    Click Button.
    Under Customize Quick Access Toolbar, select the document (or all documents) for which you want to add the macro to the Quick Access Toolbar.
    To make your macro available in all documents, be sure to clickNormal.dotm.
    Under Choose commands from dialog box, click the macro that you are recording, and then click Add.
    To customize the button, click Modify.
    Under Symbol, click the symbol that you want to use for your button.
    In the Display name box, type the macro name that you want to display.
    Click OK twice to begin recording the macro.
    The symbol that you choose is displayed in the Quick Access Toolbar. The name that you type is displayed when you point to the symbol.
    Assign a keyboard shortcut To assign the macro to a keyboard shortcut, do the following:
    Click Keyboard.
    In the Commands box, click the macro that you are recording.
    In the Press new shortcut key box, type the key sequence that you want, and then click Assign.
    Click Close to begin recording the macro.
    Perform the actions that you want to include in the macro.
    When you record a macro, you can use the mouse to click commands and options, but not to select text. You must use the keyboard to select text. For more information about selecting text by using the keyboard, see Select text.
    To stop recording your actions, click Stop Recording in the Code group.
    Click the Microsoft Office Button and then click Word Options.
    Click Customize.
    Next to Keyboard shortcuts, click Customize.
    In the Categories list, click Macros.
    In the Macros list, click the macro that you want to change.
    In the Press new shortcut key box, type the key combination that you want to choose.
    Check the Current keys box to make sure that you aren't assigning a key combination that you already use to perform a different task.
    In the Save changes in list, click the option that matches where you want to run your macro.
    To make your macro available in all documents, be sure to clickNormal.dotm.
    Click Close.
    On the Developer tab, in the Code group, click Macros.
    In the list under Macro name, click the macro that you want to run.
    Click Run.
    Write a macro from scratch
    On the Developer tab, in the Code group, click Macros.
    In the Macro name box, type a name for the macro.
    If you give a new macro the same name as a built-in macro in Office Word 2007, the new macro actions will replace the built-in macro. To view a list of built-in macros, click Word Commands in the Macros in list.
    In the Macros in list, click the template or document in which you want to store the macro.
    To make your macro available in all documents, be sure to click Normal.dotm.
    Click Create to open the Visual Basic Editor.
    After you open the Visual Basic Editor, you may want more information about working with Visual Basic for Applications. For more information, click Microsoft Visual Basic Help on the Help menu or press F1.

Similar Threads

  1. VB Script or batch file to run Excel Macro 2010
    By Fakhry in forum Software Development
    Replies: 2
    Last Post: 19-06-2012, 12:23 PM
  2. Word Macro Won't Run
    By IanGreen in forum Windows Software
    Replies: 1
    Last Post: 24-02-2012, 03:34 AM
  3. Excel Macro to loop a script
    By JFN in forum Software Development
    Replies: 1
    Last Post: 27-04-2010, 10:06 AM
  4. Macro for hyperlink on Word/Excel
    By Mindstorm in forum Windows Software
    Replies: 3
    Last Post: 07-05-2009, 03:13 PM
  5. Word 2008 + bibfuse: no script in script menu
    By deval4u in forum Software Development
    Replies: 5
    Last Post: 06-04-2009, 12:53 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,017,599.80725 seconds with 17 queries