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

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Sharing VBA code between projects (templates) (Word 2000/XP)

    Author
    Topic
    #396244

    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:

    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

    Thanks!

    Viewing 2 reply threads
    Author
    Replies
    • #741156

      Try

      Application.Run “[Global.dot]!ErrorMod.UnknownErrorSub”

      This only works if Global.dot is loaded.

      • #741216

        This works great! mice Yippie! – Thank you!

      • #741217

        This works great! mice Yippie! – Thank you!

      • #761663

        (Edited by DesertKris on 26-Dec-03 12:41. Better clarity – the “variable” is really a constant.)

        (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.

        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

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

        • #761698

          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.

        • #761699

          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.

        • #761971

          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.

        • #761972

          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.

      • #761664

        (Edited by DesertKris on 26-Dec-03 12:41. Better clarity – the “variable” is really a constant.)

        (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.

        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

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

    • #741157

      Try

      Application.Run “[Global.dot]!ErrorMod.UnknownErrorSub”

      This only works if Global.dot is loaded.

    • #763629

      > 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.

    Viewing 2 reply threads
    Reply To: Sharing VBA code between projects (templates) (Word 2000/XP)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: