Shared sub function

B

Bo Rasmussen

Hi,

Suppose I have some functions (subs) that I want to share between all my
templates, how do I do that? I've tried to put the subs in a global
template but my other templates fails to locate them. It works fine for
macros. The only point where my subs differ from macros is that they have
parameters.....

Regards
Bo
 
J

Jezebel

Put all the shared code into a template. Save the template into Word's
startup folder so it loads automatically as an add-in when Word starts.

In your other templates, add a reference to the add-in. To do this, open the
template. Switch to VBA. In the Project Explorer select the template. Go to
Tools > References. Select the Add-in. The Add-in will appear in the Project
Explorer under the template's References sub-heading.

Code in the templates can now call functions in the add-in's modules: Type
the add-in name as shown in bold in the Project Explorer and press enter --
intellisense will list your add-in's public methods and functions.

Note that the add-in's VBA project is usable at all times, but is unviewable
unless you have actually opened the add-in file itself.
 
J

Jonathan West

Bo Rasmussen said:
Hi,

Suppose I have some functions (subs) that I want to share between all my
templates, how do I do that? I've tried to put the subs in a global
template but my other templates fails to locate them. It works fine for
macros. The only point where my subs differ from macros is that they have
parameters.....


You can still use the Application.Run method to call subroutines in another
template even if they have parameters.
 
B

Bo Rasmussen

Hi Jonathan,

Could you give me an example of that? How would you call the sub below if it
was located in e.g. Global.dot using Application.Run?

' Used to update an enclosing bookmark
' See http://word.mvps.org/FAQs/MacrosVBA/InsertingTextAtBookmark.htm
Sub UpdateBookmark(BookmarkToUpdate As String, TextToUse As String)
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range
BMRange.Text = TextToUse
ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub

Regards
Bo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top