+ Reply to Thread
Results 1 to 2 of 2

Performing ms word operations from ms Excel.

  1. #1
    andy
    Guest

    Performing ms word operations from ms Excel.

    Hi,

    I would like to code a macro in a Excel document that would perform a file
    conversion of a word doc to html format. I'm able to activate the word
    document using Application.ActivateMicrosoftApp xlMicrosoftWord.

    But how to I access to the word macro objects to perform the necessary
    automation within word?

  2. #2
    Dave Peterson
    Guest

    Re: Performing ms word operations from ms Excel.

    Most/All my MSWord coding comes from MSWord's macro recorder.

    I record what I need to do in MSWord, then try to modify it to work via excel.

    For instance, this prints a .doc file:

    Option Explicit
    Sub testme()

    'Dim WDApp As Word.Application
    'Dim WDDoc As Word.Document
    Dim WDApp As Object
    Dim WDDoc As Object
    Dim myDocName As String
    Dim WordWasRunning As Boolean
    Dim testStr As String

    myDocName = "C:\my documents\word\document1.doc"

    testStr = ""
    On Error Resume Next
    testStr = Dir(myDocName)
    On Error GoTo 0
    If testStr = "" Then
    MsgBox "Word file not found!"
    Exit Sub
    End If

    WordWasRunning = True
    On Error Resume Next
    Set WDApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then
    Set WDApp = CreateObject("Word.Application")
    WordWasRunning = False
    End If

    WDApp.Visible = True 'at least for testing!

    Set WDDoc = WDApp.documents.Open(Filename:=myDocName)
    WDDoc.PrintOut '.printPreview while testing???
    WDDoc.Close savechanges:=False

    If WordWasRunning Then
    'leave it running
    Else
    WDApp.Quit
    End If

    Set WDDoc = Nothing
    Set WDApp = Nothing

    End Sub

    There are a couple of things that can make your coding easier.

    'Dim WDApp As Word.Application
    'Dim WDDoc As Word.Document
    Dim WDApp As Object
    Dim WDDoc As Object

    Set a reference within the VBE (tools|references) to "microsoft word xx.0 Object
    library".

    Then you'll get the intellisense and autocomplete for those nicely declared
    objects.

    After you've tested it, remove that reference and use the generic "as object".
    And if you used any of the Word constants, change them to their value.

    If you record your macro in word and have trouble incorporating it in excel,
    post back with your efforts. Maybe someone will be able to help.

    andy wrote:
    >
    > Hi,
    >
    > I would like to code a macro in a Excel document that would perform a file
    > conversion of a word doc to html format. I'm able to activate the word
    > document using Application.ActivateMicrosoftApp xlMicrosoftWord.
    >
    > But how to I access to the word macro objects to perform the necessary
    > automation within word?


    --

    Dave Peterson

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1