Consulting

Results 1 to 4 of 4

Thread: DisableAutoMacros when opening Word in Excel VBA

  1. #1

    Exclamation DisableAutoMacros when opening Word in Excel VBA

    Hello

    When I open a word document from excel I need to disable AutoOpen and AutoClose events. However I cannot get "DisableAutoMacros" or anything else to work.

    I tried
    wrdApp.WordBasic.DisableAutoMacros 1 'gave an error that the member was not found, I assume that's the DisableAutoMacros


    I'm opening word like this

    Public wrdApp As Word.Application
    Public WordWasNotRunning As Boolean



    Set wrdApp = GetObject(, "Word.Application")
    If Err Then
    Set wrdApp = CreateObject("Word.Application")
    WordWasNotRunning = True
    End If

    ' somewhere I need to prevent the auto macros from running

    '......

    Set wrdDoc = wrdApp.Documents.Open(DocName)
    With wrdDoc

    ' I'm doing some work here

    .Save
    .AttachedTemplate.Saved = True
    .Close ' close the document
    End With


    If WordWasNotRunning Then
    wrdApp.Quit
    End If
    Set wrdApp = Nothing

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    http://office.microsoft.com/en-us/wo...640101033.aspx

    /a
    Starts Word and prevents add-ins and global templates (including the Normal template) from being loaded automatically. The /a switch also locks the setting files

    Maybe if you used Shell ( ) to run WinWord with the /a and /t switches, it would avoid the Autorun macros

    Paul

  3. #3
    Thought of that too, however I cannot get access to the "document" object and I need to do some work in the documents.

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Greetings Henry,

    Probably a better way, but for some reason I cannot recall how to successfully work w/the app first.

    Anyways, presuming you can get into the WORD document(s) and make a minor code change:

    In the WORD document:

    [VBA]Option Explicit
    Sub AutoOpen()
    If Application.UserControl Then

    MsgBox "HI from OnOpen"

    '...statements...
    End If
    End Sub[/VBA]

    OR if using on open:

    [VBA]Option Explicit

    Private Sub Document_Open()
    If Application.UserControl Then

    MsgBox "HI AGAIN - from Document_Open"

    '...statements...
    End If
    End Sub[/VBA]

    Back in the workbook:

    [VBA]Option Explicit
    Sub OpenWordEtc()
    Dim wrdDoc As Word.Document
    Set wrdDoc = GetObject("C:\Documents and Settings\stumpm\Desktop\TEMP\CVS\Doc1.doc")

    '// Optional. Since we got the doc open while UserControl was False, we can now //
    '// display the app if we want... //
    wrdDoc.Application.Visible = True

    With wrdDoc

    ' I'm doing some work here

    .Save
    .AttachedTemplate.Saved = True

    .Close ' close the document

    End With
    End Sub[/VBA]

    Just for the example, I discluded the checking, but believe you should be able to use GetObject to see if Word is there (like you were) and either
    wrdDoc.Close or wrdDoc.Application.Quit

    Hope this helps?

    Mark

Posting Permissions

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