Results 1 to 12 of 12
  1. #1
    New Lounger
    Join Date
    Aug 2002
    Location
    Geelong, Victoria, Australia
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Referencing a function in another template (VBA (Word97/2000))

    I am embarrassed to ask this question but am new to VBA so please be patient.

    I have a large collection of WordBasic macros which I am now beginning to port to VBA.

    Straightaway I have a problem in that I cannot work out how to call a function in another template. (I have read the online help and a 3rd party book and am confused as to the code to use.)

    I believe that I need to either add a reference to the other template, provide the fully qualified object name in the code or maybe make the template global.

    Looking forward to your help.

  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: Referencing a function in another template (VBA (Word97/2000))

    Check out this lengthy thread:

    Running code in a different Word template (Word 2000 SR1a)

    I posted one method there, but I think there was a better one in one or more of the replies. Let us know what you find to work best!

  3. #3
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Referencing a function in another template (VBA (Word97/2000))

    If it is a Function, not a Sub, then you need to include a Reference to the project in which the Function resides.

  4. #4
    New Lounger
    Join Date
    Aug 2002
    Location
    Geelong, Victoria, Australia
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Referencing a function in another template (VBA (Word97/2000))

    Thanks guys.

    It is a function that I am referencing and I did read that I need to set the reference first.

    However, I receive duplicate name type errors when I try to do this. (Is it because each WordBasic macro had a Sub Main which is now represented by many Main procedures in the module?)

    Also, I assume that should use a Call method and not a Run method using this technique.

    (If you could give me the line of code that would really help.)

    Cheers,

    Peter.

  5. #5
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Referencing a function in another template (VBA (Word97/2000))

    You have to either qualify by module name, or better yet, don't use Sub Main, instead give each sub a meaningful name.

    In any case, a function, or any procedure with args, should not be called Main.

    I usually do the following:

    If I have a bunch of subs/functions that I wish to include in a single module, I name the module, e.g., modHowardsFavoriteFoods. Then I name each Sub/Function appropriattely within the module.

    Most of the time, I put each macro, i.e., Public Sub without args, in a separate module, e.g. the macro/Sub named ResetToolsOptionsView is in a module named modResetToolsOptionsView.

    However, ALL of the Auto*macros, and a related Sub, are in a module named modAutoMacros.

    Do whatever works best for you.

    Sub Main matters in VB, but not in VBA.

    VBA imports WordBasic as Sub Main merely because iit decided to it that way, no need to do so.

  6. #6
    New Lounger
    Join Date
    Aug 2002
    Location
    Geelong, Victoria, Australia
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Referencing a function in another template (VBA (Word97/2000))

    Thanks again guys.

    I have at least been able to change the default name of a module and call a function within the same module correctly.

    However, I have still been unable to call a function in a separate template. No doubt I doing some silly thing wrong with the syntax so I will play around for a while and ask you again if I can't do it.

    (This takes me back to when I began working with WordBasic when I spent a lot of time stumbling over minor syntax inconsistencies, bugs and new-to-me techniques.)

    Cheers,

    Peter.

  7. #7
    Star Lounger
    Join Date
    Jun 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Referencing a function in another template (VBA (Word97/2000))

    I maintain an application containing a large number of automated Word templates.

    Each template contains only enough code to auto-run, and specific boilerplate text/one-off macros

    All generalised functions are held in the one global template called TRIDENT.DOT

    The following line of code launchs a common user details form ( located in Trident).
    The parameter decides what options will be displayed. This is called by all 'child' templates

    Trident.Forms.Global_EntryLaunch ("DEFAULT")

    The global template MUST BE referenced( use Tools>references) within the

  8. #8
    New Lounger
    Join Date
    Aug 2002
    Location
    Geelong, Victoria, Australia
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Referencing a function in another template (VBA (Word97/2000))

    Many thanks Frank.

    Your example has shown me (contrary to, or omitted from, the online VBA help) that:

    I don't use the syntax: TemplateProject(<Project Name>)
    I don't specify the file extension (.dot)
    I do have to specify the module or object type ("Forms" in your case).

    I will give it a try.

    Cheers,

    Peter.

  9. #9
    New Lounger
    Join Date
    Aug 2002
    Location
    Geelong, Victoria, Australia
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Referencing a function in another template (VB

    That's fantastic.

    Many thanks. I have no excuses now.

    Cheers,

    Peter.

  10. #10
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Referencing a function in another template (VB

    <P ID="edit" class=small>(Edited by Eileen on 23-Aug-02 06:22. To cut the lines to a reasonable width. It was making the post very wide.)</P>The VBA online help is a tad confusing at times. And you can use the template.Module syntax - when you need it!

    If you haven't yet been able to hook up functions, temporarily (for ten minutes!) abandon your old macros and try
    doing it very simply in brand new VBA to let yourself see it working. Then extend your knowledge towards your
    WordBasic requirements.

    I spend most of my development time now building small applications by tapping into the engines that I've buil
    t in other applications.

    1) Start Word.
    2) Create a new template called Temp1, insert a user module UserMod1, create a simple function called
    strFunc1 that takes one string as an argument and passes it back as a result:
    <pre>public function strFunc1(strIn as string) as string
    strFunc1="Here is " & strIn
    end function
    </pre>

    3) Change the name of the project from "TemplateProject" to "TemplateProject1"

    4) Save this template as c:temptemp1.dot and then close it.

    5) Create a new template called Applicn1, insert a user module AppMod1, create a simple User macro
    <pre>public sub UserMac
    end sub
    </pre>


    6) Choose (Word97/SR2) Tools References and Browse to your c:temptemp1.dot. Set FilesOfType
    to be doc/dot, select your template and confirm.
    7) In the references GUI box make sure the check box next to your project's name is checked ON.
    Confirm with OK.

    8) (drum rolll) back in your UserMac, on a new line, type
    <pre>MsgBox TemplateProject1
    </pre>

    and immediately after the "1" of "TemplateProject1" type a period. You should see the "Module1" pop up
    in front of your eyes. Select this and type another period. You should see "strFunc1" pop up in front of
    your very eyes. Select this. Type an opening parenthesis. You should be prompted for a string. Type
    "hello" with the double quotes, a closing parentheses, and tap the F5 function key to run the user macro.
    It should give you your pop-up MsgBox results.

    You only NEED the template.modul stuff for uniqueness.

    9) back in your usermac, on a new line, type

    <pre>msgbox strfunc1 and a left parentheses. You should be prompted for a string argument.
    proceed as before.</pre>


    If you'd like to try this with a real library, grab a copy of Utils.dot from
    http://www.vif.com/users/cgreaves/DownLoads.htm
    and in a user macro in Normal.dot add Utils.dot as a reference, then build a usermacro
    <pre>Public Sub myMac()
    MsgBox U.strDateConst
    End Sub</pre>


    I enjoy making use of functions from utility libraries immensely.

    I am indebted to Gary Frieder et al. for their help in getting this far.

  11. #11
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Referencing a function in another template (VB

    The methods I learned for invoking existing Word/VBA application code from another app have outstripped my expectations.

    What you saw posted above (if it works) is the START of a journey. There's more. But at least get that business of (template file.dot) TemplateProject.Module.Procedure(args) working and you'll have the foundation you need for all the rest. Post back here when (not if!) you run into probblems.

    I was confused for a long time between "The name given to the template by the Windows Filing convention (MyTemplate.DOT) and "The name given to the template (project) by the Word/VBE convention (MyTemplateproject)". I suspect that that is where you ran into problems with "template" as mentioned in your posts earlier in this thread.



    If you have d/l and used my Utils.dot you'll realise that I chose a templateproject name of "U", because it's short, mnemonic, and darned easy for an old guy like me to type (near the centre of the keyboard on the side that favours my reading eye!).

    The TemplateProject is named "U"

    The Template file is named Utils172.dot (because I've been using Utils171.dot long enough and it was time to start a new version). Utils173.dot is the next planned release ...


    I also have a couple of free correspondence courses - a lesson a day by email, or a lesson a week, whatever you want, called "Using Libraries" and "Using Utils". http://www.vif.com/users/cgreaves/co...encecourse.htm

  12. #12
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Referencing a function in another template (VB

    http://www.wopr.com/cgi-bin/w3t/showthread...ums&Words=Word6 WordBasic template FaxMacs.dot&Match=And&Searchpage=0&Limit=50&Old=al lposts&Main=144453

    Here's a reference I couldn't find last night.

Posting Permissions

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