Results 1 to 6 of 6
  1. #1
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Code to retrieve a macro for editing (Word 2002 SP-2)

    In WordPerfect I always had a simple input dialog (called by Alt-M) that took the name of a macro and ran it -- or retrieved the macro for editing if I clicked on a separate button. I'm replicating it in Word and can use the following code to pull up the macro in the VBEditor:

    For Each vbcX In projRUW.VBComponents
    If vbcX.Type = vbext_ct_StdModule Then
    Set mdl = vbcX.CodeModule
    If mdl.Find("Sub " & strName, lngSLine, lngSCol, lngELine, lngECol) Then
    mdl.CodePane.Show
    lngX = mdl.ProcBodyLine(strName, vbext_pk_Proc)
    mdl.CodePane.SetSelection lngX + 1, 1, lngX + 1, 1
    Exit Sub
    Else
    If mdl.Find("Function " & strName, lngSLine, lngSCol, lngELine, lngECol) Then
    mdl.CodePane.Show
    lngX = mdl.ProcBodyLine(strName, vbext_pk_Proc)
    mdl.CodePane.SetSelection lngX + 1, 1, lngX + 1, 1
    Exit Sub
    End If
    End If
    End If
    Next vbcX

    Isn't there a more direct way?

    I tried assigning mdl.ProcStartLine(strName, vbext_pk_Proc) to lngX near the top of the loop (where the first .Find line is), figuring lngX would be 0 if the module lacked a procedure with that name, but instead I get error 35 ("sub or function not defined").

    Is there a way that complete avoids looping through the modules and instead makes reference to some kind of index that Word must surely maintain for all currently available macros?

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Code to retrieve a macro for editing (Word 2002 SP-2)

    I'm completely lost! Where are you invoking this complicated code? If you're in Word, try Alt+F8. This same dialog can, I believe, be automated to appear in response to another keystroke or event and provides most (if not all) of the functionality desired. What am I missing?

  3. #3
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Code to retrieve a macro for editing (Word 2002 SP-2)

    1. In WordPerfect, if you have a lot of macros (I have over 1,000), a simple input-name dialog is significantly faster to pop up and use than the default dialog (which lists all the macros). I'm assuming Word will be the same as my macro collection grows.

    2. My Word macro dialog (like my WordPerfect dialog) puts the name of the macro I last ran in the box when it opens, so if I'm running the same macro several times in a row (a common occurrence), I can just hit Alt-M, then Enter, to run it the 2nd and subsequent times.

    3. My dialog lets me edit the macro by simply hitting the Edit button. The Word dialog doesn't allow me to do that unless and until I've first taken the separate step of opening my macro template.

    4. Like my WordPerfect macro dialog, I'm assuming my Word macro dialog will probably end up accumulating a few other command buttons (over time) that give me some additional macro-related functionality that isn't one click away in Word's dialog.

    5. Not a huge deal, you say? OK, but sheesh, the go-to-the-macro code I posted isn't that complicated ...

    (Nonetheless, if Word VBA offers a more direct way to find and move to a particular macro in the VBE window, I'd still be interested in hearing about it.)

  4. #4
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Code to retrieve a macro for editing (Word 2002 SP-2)

    As a supplement to my original post, I've since streamlined my loop-thru-the-modules approach by making use of ProcBodyLine, like this:

    For Each vbcX In projRUW.VBComponents
    If vbcX.Type = vbext_ct_StdModule Then
    Set mdl = vbcX.CodeModule
    lngX = mdl.ProcBodyLine(strName, vbext_pk_Proc)
    If lngX > 0 Then
    mdl.CodePane.Show
    mdl.CodePane.SetSelection lngX + 1, 1, lngX + 1, 1
    Exit Sub
    End If
    End If
    Next vbcX

    As indicated in my original post, the ProcBodyLine line generates Error 35 if the module doesn't have a macro with that name, so I tweaked the error handler to Resume Next on Error 35.

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Code to retrieve a macro for editing (Word 2002 SP-2)

    I'm going to migrate this thread over to the VB/VBA board. I think we once had a thread on trying to extract macro names from either the Macros dialog or from the Organizer dialog, but I can't recall whether either was successful. Anyway, you probably will get more programmers working on this one over there.

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Code to retrieve a macro for editing (Word 2002 SP-2)

    People tend to adopt different habits when using Word to those that are common in Word Perfect

    Word uses many fewer macros, things that WordPerfect needs macros for are often done with AutoText, Styles etc.
    Macros that are commonly used are usually grouped together into menus or toolbars for quick access

    The code that you posted will need to have reduced security enabled in order to run, specifically the user would have to go to Tools > Macros > Security > Trusted Sources and check the box marked "Trust access to Visual Basic Project". This checkbox is usually clear to make it harder for Macros to propagate by inserting macro code into existing templates.

    StuartR

Posting Permissions

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