Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 459137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:44:33+00:00 2026-05-12T22:44:33+00:00

I’m presented with a puzzle. Where I work there are a huge amount of

  • 0

I’m presented with a puzzle. Where I work there are a huge amount of Word templates which all contains an autonew eventhandle which contains some errors. And this error lies within all templates. And I was wondering if there might be a way of scanning a directory for templates which contains this macro and change the macro code slightly?

Could this be possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T22:44:33+00:00Added an answer on May 12, 2026 at 10:44 pm

    Yes, you can do that. You can access the VBA project of any document using:

    Application.VBE.ActiveVBProject.VBComponents
    

    Your project must have a reference to “Microsoft Visual Basic for Applications Extensibility”.

    To run the code, you must enable the “Trust Access to Visual Basic Project” option in Word, using

    Tools->Macro->Security (Trusted
    Publishers tab)

    The VBComponents collection contains all the standard modules, class modules, forms and “document” modules that the project contains. If you Google it, you’ll find plenty of help on how to access/modify them.

    EDIT: OK, some more detail. This method will search all the VbComponents of a document looking for a method with the specified name, and perform a search/replace within the first one it finds.

    Public Sub ReplaceInProject(ByVal oDocument As Document, ByVal strMethodName As String, ByVal strFindText As String, ByVal strReplaceWithText As String)
    
        ' For each module (of any type - could use oVbComponent.Type to restrict
        ' this to certain types of module)
    
        Dim oVbComponent As VBComponent
        For Each oVbComponent In oDocument.VBProject.VBComponents
    
            Dim oCodeModule As CodeModule
            Set oCodeModule = oVbComponent.CodeModule
    
            ' See if we can find the method in this module
    
            Dim ixStartLine As Long
            ixStartLine = FindMethodStartLine(oCodeModule, strMethodName)
    
            If ixStartLine > 0 Then
    
                ' Get all the text of the method
    
                Dim numLines As Long
                numLines = oCodeModule.ProcCountLines(strMethodName, vbext_pk_Proc)
    
                Dim strLines As String
                strLines = oCodeModule.Lines(ixStartLine, numLines)
    
                ' Do the find/replace
    
                strLines = Replace(strLines, strFindText, strReplaceWithText)
    
                ' Replace the method text.
    
                oCodeModule.DeleteLines ixStartLine, numLines
    
                oCodeModule.InsertLines ixStartLine, strLines
    
            End If
    
        Next oVbComponent
    
    End Sub
    
    Private Function FindMethodStartLine(ByVal oCodeModule As CodeModule, ByVal strMethodName As String) As Long
    
        FindMethodStartLine = 0
    
        ' ProcStartLine will raise an error if the method is not found;
        ' we'll just ignore the error and return -1
    
        On Error Resume Next
        FindMethodStartLine = oCodeModule.ProcStartLine(strMethodName, vbext_pk_Proc)
    
    End Function
    

    Note that this will only work with Sub and Function methods, not property Get/Set/Let because I’m using vbext_pk_Proc. It’s a PITA that you need to be explicit about this. Frankly, the whole API for the CodeModule component seems almost designed to frustrate. For example, while the VbComponent object has a Find method (which you’d think was a convenient way to find the text you’re looking for), it actually returns True or False(!). Useful, I don’t think!

    The designers of this API must have had a really bad hangover when they did this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.