Results 1 to 12 of 12
  1. #1
    New Lounger
    Join Date
    Jul 2003
    Location
    Emanuel, South Australia, Israel
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    VBA for Word 2000 (2000 sp3)

    What code can I use in a macro NOT in the Normal template to check if a specific macro exists in the Normal template?
    In English: If Normal.ABC.ABC does not exist then XYZ.
    What works for me now is that if it does exist, XYZ causes an error, which I trap to bypass XYZ and continue. But that is poor code. Is there any way to test for existence of the macro item?

  2. #2
    Star Lounger
    Join Date
    Jan 2001
    Location
    U.S. - East Coast
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA for Word 2000 (2000 sp3)

    One method is to use the Visual Basic Extensibility object to expose a project's code modules, then search those modules for the desired string (not the most elegant method, but VBE is not that robust -- it doesn't expose a "Macros" collection for example):

    Application.VBE.VBProjects("Normal").VBComponents( "ThisDocument").CodeModule.Find("Sub MyMacro",1,1,10000,100,False,False,False)

    This example searches the "ThisDocument" module in Normal.dot for the string "Sub MyMacro". The various parameters indicate the search domain, i.e. starting/ending lines/columns, etc. The problem with this solution is that you need to be as specific as possible with the search criteria so as not to return false positives; this may require a bit more hard-coding or "knowledge" on the part of the client procedure than you would like.

  3. #3
    New Lounger
    Join Date
    Jul 2003
    Location
    Emanuel, South Australia, Israel
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA for Word 2000 (2000 sp3)

    Thanks for the idea, but I neglected a major point:
    If the macro is absent, so is the module. In that case, the code you suggested fails. What I really want to do is test the Normal Template for the existence of the module! Sorry for the blunder! There does not seem to be an equilavent Find for the VBProjrcts object.

  4. #4
    Star Lounger
    Join Date
    Jan 2001
    Location
    U.S. - East Coast
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA for Word 2000 (2000 sp3)

    <P ID="edit" class=small>(Edited by jscher2000 on 09-Jul-03 21:36. [pre] and [/pre] tags added to preserve indentation)</P>No problem, there's an ugly solution for that scenario as well!

    Consider the following snippet:

    <pre>Private Function ModuleExists(ByVal strModuleName as String) as Boolean

    Dim strDummy as String

    On Error Resume Next

    'Attempt to read a property (can be any valid property) of the required module.
    strDummy = Application.VBE.ActiveVBProject.VBComponents(strMo duleName ).Name

    If Err.Number =0 Then
    'No error, so module exists.
    ModuleExists = True
    Else
    'Error occurred, so module likely does not exist.
    ModuleExists = False
    EndIf

    End Function</pre>

    It's not elegant, but it should work. You can extend it further to test for the existence of the macro using the code from my prior post.

  5. #5
    New Lounger
    Join Date
    Jun 2003
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA for Word 2000 (2000 sp3)

    When testing for the module, will you know what the name is of the module you are testing for?

  6. #6
    New Lounger
    Join Date
    Jul 2003
    Location
    Emanuel, South Australia, Israel
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA for Word 2000 (2000 sp3)

    Yes, the first of the three, copies the third to the Normal template. Since I have no way of knowing if the user saved the Normal template, I must check to see if it is there before copying it again or an error occurs. Now I trap the error and just continue, but the code is sloppy.

  7. #7
    New Lounger
    Join Date
    Jun 2003
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA for Word 2000 (2000 sp3)

    I'm a little confused but cant you check to see if it is already saved. Something like getting the name and then checking if it is saved. Use an if statement checking if name = normal and if saved equal= true as below. Just a thought. I've been working on a somewhat similar project and have had to overcome many problems. If this doesn't help, try and make it a little clearer what you are trying to do exactly.
    wdapp as new word.application
    wddoc as word.document

    wdapp.VBE.ActiveVBProject.Name
    wdapp.VBE.ActiveVBProject.Saved

    or
    wddoc.VBProject.Name
    wddoc.VBProject.Saved

  8. #8
    New Lounger
    Join Date
    Jul 2003
    Location
    Emanuel, South Australia, Israel
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA for Word 2000 (2000 sp3)

    When someone in our office opens a document based on the attached template containing this code, he needs to send the doc to a person in China who does not have the template. Without the template, the autotext fields in the doc will not work, so I want to change the autotext fields to plain old text (with fields like {page}) and with the Normal template attached AND with a new name. However, the instant the doc template is changed to Normal, the macro stops of course. So I want to run the macro from the users Normal template. I can copy it no problem, but if he saves the Normal template and tomorrow opens another doc and runs the macro the macro fails since it cannot copy it to the Normal template -- it already exists in the Normal template!! Therefore I want to test if the macro module exists or not.

  9. #9
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA for Word 2000 (2000 sp3)

    I'm not a VBA expert, but why don't you create a custom template & put your autotext, etc. in there? Then you can send the template & document together, along with instructions for the recipient to put the template into the template folder. Have the template attached to the document.

  10. #10
    New Lounger
    Join Date
    Jul 2003
    Location
    Emanuel, South Australia, Israel
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA for Word 2000 (2000 sp3)

    There is a well-known rule (Peter's Principle?) that if something can go wrong it will. I do not want to send neither templates nor instructions. In truth I don't want any one in my company to send Word documents either (just PDFs), but I cannot control this. Getting staff to click a button and send the new Normal-template document, automatically renamed, is the most I can ask for.

  11. #11
    New Lounger
    Join Date
    Jul 2003
    Location
    Emanuel, South Australia, Israel
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA for Word 2000 (2000 sp3)

    I fail to understand how the code you suggested specifies to look in the Normal Template for the module. The code I use that works is as follows:

    Sub ConvertAutotextStart()
    '
    ' SaveToNormal Macro
    ' Macro recorded 07-Jul-03 by Israel Dalven
    ' This module copies the ConvertAutotext2 module to the Normal template
    ' If it exists already, an error occurs and the copy is aborted.

    ActiveDocument.UpdateStylesOnOpen = False

    On Error GoTo skip
    Application.OrganizerCopy Source:="Cocuments and SettingsOwnerApplication DataMicrosoftTemplatesCellglide.dot" _
    , Destination:="Cocuments and SettingsOwnerApplication DataMicrosoftTemplatesNormal.dot" _
    , name:="ConvertAutotext2", Object:=wdOrganizerObjectProjectItems
    skip:
    ConvertAutotext1.ConvertAutotext1

    It is not good practice to create errors, so how do I test for the existence of the module ConvertAutotext2?

    End Sub

  12. #12
    Star Lounger
    Join Date
    Jan 2001
    Location
    U.S. - East Coast
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA for Word 2000 (2000 sp3)

    Sorry, was a little quick in copying my test of the code...all you need to do is substitute the "Normal" project for "VBActiveProject". The code should read as follows:

    <pre>Private Function ModuleExists(ByVal strModuleName As String) As Boolean

    Dim strDummy As String

    On Error Resume Next

    'Attempt to read a property (can be any valid property) of the required module.
    strDummy = Application.VBE.VBProjects("Normal").VBComponents( strModuleName).Name

    If Err.Number = 0 Then
    'No error, so module exists.
    ModuleExists = True
    Else
    'Error occurred, so module likely does not exist.
    ModuleExists = False
    End If

    End Function
    </pre>


Posting Permissions

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