Results 1 to 8 of 8
  1. #1
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Global variables and re-using code (Word 2003)

    Dear Loungers,

    My template that has a form for completing various custom properties, this code needs to be genericised i.e. it will be used by several templates. And I know that there will eventually be several forms for various document categories. So what I want to do is have the code once, in a global template, call it from a workgroup template and pass the information about the category, and therefore which form to open, to the global code so that the user gets the correct interface. Crudely the logic should be like this:

    1. If new doc and where the selected template is for category X:
    Open New Document using template that is for document category X
    Call Global UpdateProperties Routine to display form for category X
    User updates the properties
    Global procedure applies the updates to the document (including to DocCategory)
    Save the document
    Return control to the user

    2. If existing doc:
    Open Existing Document, File property DocCategory is already set to Y (by a previous instance of 1 above)
    Call Global UpdateProperties Routine to display form for category Y
    User updates the properties
    Global procedure applies updates to document
    Return control to the user

    Problem A) is around the "global" variable aspect of this because I think that declaring a Public variable only makes it available to the modules in the document/template not all templates so I can't have a global varaible declared, for example in the global template that can be used by all modules in all templates (is that right?)

    Therefore If I call a global procedure with an argument then in case 1 above I can use Document_New and pass the Document Category to the called procedure which will look like this: Public Sub MT_UpdateFileProperties(strDocCategory As String). But I want to use the same procedure if it is an existing document, so I should set strDocCategory to the value in ActiveDocument.CustomDocumentProperties(DocCategor y) - I'm assuming that active document will be the document and not the global template

    Problem [img]/forums/images/smilies/cool.gif[/img] I don't know how to call a module in another template - I assume this is possible but have failed thus far. What is the correct syntax?

    thank you....... liz

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Global variables and re-using code (Word 2003)

    a) You should indeed pass the information to a procedure in another template as arguments, you can't use global variables for that.

    [img]/forums/images/smilies/cool.gif[/img] Try
    <code>
    Application.Run "NameOfMacro"
    </code>
    with the appropriate name substituted, of course. If the name of the macro is not unique, you can use
    <code>
    Application.Run "NameOfAddin!NameOfModule.NameOfMacro"
    </code>
    again, with the appropriate names substituted.

    If you want to pass one or more arguments, you must specify them separately, e.g. to call NameOfMacro(37), you'd use
    <code>
    Application.Run "NameOfMacro", 37</code>

  3. #3
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Global variables and re-using code (Word 2003)

    Hans,

    I must be being stupid since I am getting errors.

    1. if I use this:
    Application.Run "HK2007!UpdateProperties.MT_UpdateFileProperti es", "Proposal"
    I get Runtime error 438 Object doesn't support this property or method
    All the element exist and MT_UpdateFileProperties is defined as public, perhaps I don't have to but I don't think that would make a difference.

    2. I went back to my original idea of using call:
    Call [HK2007!UpdateProperties}.MT_UpdateFileProperties(" Proposal")
    this reports an error of external name not defined.

    I'm sure this is an oversight by me but I can't see it.

    liz

    p.s re previous conversation about VBA reference material and knowing what to look for: the book that until now I thought was quite good, VB & VBA in a Nutshell published by O'Reilly, doesn't have an entry for Run (it does for Call). Why would that be?

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Global variables and re-using code (Word 2003)

    You cannot use Call to execute a procedure in another document/template, you'll have to use Application.Run. Application.Run is very finicky and acts differently in different Office applications. It works best for me if I make the name of the procedure unique, so that I can use

    Application.Run "MT_UpdateFileProperties", "Proposal"

    i.e. no reference to the template and module names, only to the unique procedure name. Another option is to set a reference (in Tools | References) to the template; for this you need to assign a unique name to the VBA project in the template (by default, the VBA project is always named Project). You can then use Call, and the name of the procedure can be found in IntelliSense (the popup list that appears when you type a dot, for instance).

  5. #5
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Global variables and re-using code (Word 2003)

    Hans,

    Since you said Application.Run is slightly awkward I decide to use your other suggestion using references, this works beautifully. But I wondered if there was a reason for your opting for Applican-Run rather than using references, it's really nice that intellisense works and I don't mitsype the name!

    I have transferred the templates to another machine, I wonder how the references are stored - do I need to establish them on any new machine they are installed on? That would be impractical since we couldn't have users doing it for themselves and it would be impossible to do it for all of them - there is a procedure which they follow if they aren't in the office or they use a laptop but we can't add ".... now go into VB editor and update references..."

    The reason I ask is because there is an error, I am just off to check out the problem so I may be jumping the gun here. I am told it is a compile error and it seems to be pointing to the external module so I am thinking that it may be references related.

    If you have any knowledge of what happens to references when moving templates between machines I'd be greatful as I have to resolve this tonight otherwise no documents can be created tomorrow. And could this be the reason why you plump for Application.Run?

    Thanks...................... liz

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Global variables and re-using code (Word 2003)

    The advantage of Application.Run is that it doesn't depend on where exactly the other document/template is stored, as long as it's open when you use it.

    References are stored in the document/template, so they travel with it, but unfortunately they store the exact path. If you transfer both files (the "referencer" and "referenced") to another computer, the path of the "referenced" file should be exactly the same, otherwise VBA won't be able to find the "referenced" file. The only exception is when the "referenced" file is stored in the same folder as the "referencer", VBA should then be able to find it.

  7. #7
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Global variables and re-using code (Word 2003)

    Hans,

    ... as I have now discovered!! I shall now play with Application Run and see if that will work for me

    thanks........liz

  8. #8
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Re: Global variables and re-using code (Word 2003)

    Hans,

    Now working! It was a bit pernickety about getting rid of the references. I got an error "runtime error 5981" and it complains that it can't access macro storage. however this was fixed by ensuring all references that I created removed and then close and reopen Word.

    Thank you................. liz

Posting Permissions

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