?
Solved

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

Posted on 2010-01-06
13
Medium Priority
?
462 Views
Last Modified: 2012-05-08
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.
0
Comment
Question by:vopip
  • 5
  • 4
  • 4
13 Comments
 
LVL 35

Accepted Solution

by:
mvidas earned 2000 total points
ID: 26194196
Hi Vopip,

Try using
 MyDoc.VBProject.VBComponents("ThisDocument").CodeModule

All of the event Document event code should be located within each file's ThisDocument object.

Matt
0
 

Author Comment

by:vopip
ID: 26194382
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
0
 
LVL 35

Expert Comment

by:mvidas
ID: 26194585
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 26194697
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.
0
 

Author Comment

by:vopip
ID: 26194705
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
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 26194724
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?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 26194739
.DocM means that it is macro-enabled. DocX documents are not permitted to contain macros.
0
 

Author Comment

by:vopip
ID: 26194826
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
0
 
LVL 35

Expert Comment

by:mvidas
ID: 26194910
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
0
 

Author Comment

by:vopip
ID: 26194993
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
0
 
LVL 35

Expert Comment

by:mvidas
ID: 26195061
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
0
 

Author Comment

by:vopip
ID: 26195251
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.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 26196274
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

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Lost Word File? Eagerly, need it back? Read ahead; this File Recovery guide is for you.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question