Results 1 to 7 of 7
  1. #1
    Lounger
    Join Date
    Aug 2002
    Location
    Sierra Madre, California, USA
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Sharing VBA code between projects (templates) (Word 2000/XP)

    I know how to share code between modules in the same project (Word template). Is there a way to share code between projects (two different Word templates)? For now I want to work strictly with the templates. For example I have an ErrorTrapping module in a global template. I want the Fax template to be able to access this code stored in the Global template.

    Here's what I'd like to do, but not sure how to reference it...

    Code in Fax template:
    <pre>Public Sub CmdOK_Click()
    On Error GoTo ErrHandler
    'some code goes here....
    Exit Sub
    ErrHandler:
    ErrorMod.UnknownErrorSub ' This code module lives in a different template called Global.dot
    'the project is called GTM
    End Sub</pre>


    Thanks!

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

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

    Re: Sharing VBA code between projects (templates) (Word 2000/XP)

    Try

    Application.Run "[Global.dot]!ErrorMod.UnknownErrorSub"

    This only works if Global.dot is loaded.

  4. #3
    Lounger
    Join Date
    Aug 2002
    Location
    Sierra Madre, California, USA
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Sharing VBA code between projects (templates) (Word 2000/XP)

    This works great! <img src=/S/mice.gif border=0 alt=mice width=50 height=25> Yippie! - Thank you!

  5. #4
    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: Sharing VBA code between projects (templates) (Word 2000/XP)

    Can a global template store Document Variables and/or Document Properties? Either is relatively easy to access and update in a document... but I don't know about a global template.

  6. #5
    Lounger
    Join Date
    Aug 2002
    Location
    Sierra Madre, California, USA
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Sharing VBA code between projects (templates) (Word 2000/XP)

    <P ID="edit" class=small>(Edited by DesertKris on 26-Dec-03 12:41. Better clarity - the "variable" is really a constant.)</P>(Hans Your suggestion worked great for accessing the error module.)

    I have a new problem. I have some client specific variables (file path location stuff) that I keep in a global template -which work great because they are called from that template. I want to be able to keep the file path location for the database in this global template -currently I am keeping it in each document template because that is where it is called from. Once declared, the file path doesn't change. The problem is that I cannot work on the templates at home without going into EACH document template and changing the file path location since it is different on my network. I would much rather have to change it in just one place.

    Example - When the user starts the letter template. It opens a database containing user info for the signature block, etc. I currently use the following identical statement in EACH document template.

    <pre>Const strProvider As String = "Microsoft.Jet.OLEDB.4.0" 'Jet version
    Const strDBPath As String = "C:StaffInfo.mdb" 'which I have to change at the client site

    Set cnn = New ADODB.Connection
    cnn.ConnectionString = "Provider=" & strProvider & ";Data Source=" _
    & strDBPath & ";Persist Security Info=False"
    cnn.Open</pre>

    Any suggestions of how I can store the variable "strDBPath" in the global template and access it from various other templates? Thanks!

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

    Re: Sharing VBA code between projects (templates) (Word 2000/XP)

    You could do the following:
    - Open the global template in the Word interface (otherwise, you can't enter or modify code in it)
    - Insert the following in a standard module:

    Private Const strProvider As String = "Microsoft.Jet.OLEDB.4.0" 'Jet version
    Private Const strDBPath As String = "C:StaffInfo.mdb" 'which I have to change at the client site

    Public Function GetConnectString() As String
    GetConnectString = "Provider=" & strProvider & ";Data Source=" & _
    strDBPath & ";Persist Security Info=False"
    End Function

    - Use it as follows:

    cnn.ConnectionString = Application.Run("GetConnectString")

    - Now, you will only have to open the global template in the interface, change the constants and save the global template.

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

    Re: Sharing VBA code between projects (templates)

    > Is there a way to share code between projects

    About four years ago I began collecting utility functions in a Word template Utils.dot. The last time I looked it had over 500 procedures. Especially useful are the string-handling routines and the GUI form management code.

    I build applications which have "engines" in their hearts. For example, my Indexer (builds Index entries & keywords) has an engine called "Interesting Words" within it. The Interesting Words engine delivers, well, interesting words, that the indexer uses to tag index entries ({XE} fields).

    Other applications then make use of this engine. For example, my Precis Generator will call the Interesting Words engine to obtain the interesting words for each paragraph, first sentence, and sentence of a document, then builds a precis accoding to statistical merits of each paragraph, first sentence or sentence. Other appliactions make use of other engines.

    It's a fast way to build applications.


    I keep my utils.dot and all applications in my Startup folder.

Posting Permissions

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