Link to home
Start Free TrialLog in
Avatar of vopip
vopip

asked on

How to programmatically manipulate Word VBA code not in a module?

I am looking to manipulate the Document_Open event code (via vba in Word). I would like to do this for a directory's entire contents of .docm files. I know how to do the same with VBA modules in a document (using MyDoc.VBProject.VBComponents().CodeModule, but cannot find how to grab the event code.
ASKER CERTIFIED SOLUTION
Avatar of mvidas
mvidas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vopip
vopip

ASKER

Matt,
When I do that I am getting an "Object doesn't support this property or method" error. The error is generated on the "with" line. Code:

Public Sub LoopThrough()
    Dim oWord As Word.Application
    Dim doc As Word.Document
   
    Set oWord = New Word.Application

    Set doc = oWord.Documents.Open("MyDocument.docm")

    With doc.VBProject.VBComponents("ThisDocument").CodeModule
        Print "See it"
    End With

End Sub
I did just notice that you said .docm instead of just .doc, which must be the error. This computer only has office 2003 on it, my home desktop has 2007 and I can check there. Using this pc, your code ran fine. I'll check later, unless someone else steps in first :) Sorry for that oversight!
Matt
If you can already do it for one document, all you have to do is to open each document, make the changes, and save it again. But perhaps I'm  missing something.
Avatar of vopip

ASKER

Matt,
Isn't the only way to even get a Document_Open event in a 2007 document to have it named .docm? I've tried with .docx and it won't let me save it with macro code.

Thanks,
Vince
Do you have the reference to the Microsoft Visual Basic for Applications Extensibility set, and have you enabled the  access to VB projects in the Trust Centre?
.DocM means that it is macro-enabled. DocX documents are not permitted to contain macros.
Avatar of vopip

ASKER

Graham,

Regarding "Do you have the reference to the Microsoft Visual Basic for Applications Extensibility set, and have you enabled the  access to VB projects in the Trust Centre?" The answer is yes to both and both are enabled in both the source document as well as (in my simple test) the single target.

To answer your first question, the problem I am trying to solve has approximately 1000 documents that currently are being manipulated by hand. I am looking for a solution to modifiy all of the Document_Open code in each of those 1000 document files (all of which, since they contain the macro, are .docm).

Thanks,
Vince
Ok, I've checked at home and it is still working correctly (now using the macro-enabled .docm).
I don't have extensibility referenced, though I did have to Trust access to the VBA project object model in the trust center. It was a different error for that though, at least for me.

Where are you running this from? I've only been testing it from another word doc's vba, though based on your code I'm guessing another office application's vba. I don't know if that would make a difference, just trying to narrow it down for you.

Matt
Avatar of vopip

ASKER

Matt,
I am running from another word doc's vba module. Is it possible for you to attach the two test files you are using?

Vince
I'd be glad to, though they really are just blank files, one with MsgBox "hi" in the document_open (c:\mydocument.docm), and the other with this in module 1:

Public Sub LoopThrough()
    Dim doc As Document
   
    Set doc = Documents.Open("c:\MyDocument.docm")

    With doc.VBProject.VBComponents("ThisDocument").CodeModule
        MsgBox "See it"
    End With

End Sub


Try using that variation of code instead of yours, making sure to specify the MyDocument.docm path. If still no joy I'd be glad to post the sample files
Avatar of vopip

ASKER

Must have been something funky with the document I started with. I tried just changing how the file was being opened to your solution. It failed with the same message, so I created a new "driver" file and posted your code above and it worked just fine. Thanks.
Are you having a problem processing several files?

Here is the outline of a way of processing all the files in a folder
Sub ProcessFile()
    
    Dim strFolder As String
    Dim strFile As String
    Dim wdDoc As Word.Document
    Dim proj As VBProject
    strFile = Dir$(strFile & "*.doc")
    Do Until strFile = ""
        Set wdDoc = Documents.Open(strFolder & "\" & strFile)
        'Process current document
        Set proj = wdDoc.VBProject
        'do your processing here
        
        wdDoc.Close wdSaveChanges
        strFile = Dir$() 'next file
    
Loop

End Sub

Open in new window