Solved

How to make vba word functions and menus available for all word documents

Posted on 2008-11-03
5
518 Views
Last Modified: 2013-11-10
Hi,

I have written a word template with functions and menus (written in vba)
Now I have problems with the enrollment of the word application. My goal: I want the functions and menus be usable in all new word documents. To reach this goal I use the snippet code, where I have written into the Document_New and in the Document_Open a code, which saves the dot file into the startup folder of the user.
Problem:
Works fine, - the user can see all new buttons and use all functions of the dot file in all new documents, but only after he had closed winword and open it again.
It seems to me, that if I save the opened dot file into the startup folder, winword needs an new start to read the dot file out of the startup folder for every new file.

This is no good to me, because I have an installation procedure, where the template is opened outomatically and the first thing I would have to tell the user now: "Pleae close word and open word again, to use the new features for any other documents"....

So pleaaase help.
What is the best way to
1. check where the startup folder of the user is
2. how to copy the new dot file into his startup folder so that the user can use it, without having to restart word
Maybe it is better to make this not in vba? Is it better to make it with a external installation script. But how could I check then which office version is installed, because the startup folders are not the same. And in Office 2007 I need a 2007 Template to copy there, in office 2003 a normal dot

I hope to make the enrollment of my application very soon. So please help me to get over this. Thanks!


Private Sub Document_New()
 

Dim b As Integer

Dim MyButton As CommandBarButton

Dim booButtonexistiert As Boolean

Dim ControlBar As CommandBar

Dim strPath As String

Dim strStartupPfad As String
 

booSelectionEreignis = False

On Error GoTo errMenü

For Each ControlBar In Application.CommandBars

    If Not ControlBar.BuiltIn And ControlBar.Name = "Checkitmed" Then

        booButtonexistiert = True

        ControlBar.Visible = True

    End If

Next
 
 
 
 

Set app = Application

    If booButtonexistiert = False Then

        Set ControlBar = Application.CommandBars.Add("Checkitmed", msoBarTop)

        ControlBar.Visible = True

        

          Set MyButton = ControlBar.Controls.Add(msoControlButton)

        MyButton.Style = msoButtonIconAndCaption

        MyButton.Caption = "Öffnen"

        MyButton.OnAction = "VorlageoeffnenmitCheck"

        MyButton.FaceId = 173
 

        Set MyButton = ControlBar.Controls.Add(msoControlButton)

        MyButton.Style = msoButtonIconAndCaption

        MyButton.Caption = "Textmarken"

        MyButton.OnAction = "modCheckitmedProf.TextmarkenEditor"

        MyButton.FaceId = 176
 

      

              

        CommandBars("Checkitmed").Controls.Add Type:=msoControlButton, ID:=225 _

        , Before:=2
 

     End If

Stop

strStartupPfad = fktPfadInklBackslash(Application.Options.DefaultFilePath(wdStartupPath))

If fktExistiertDatei(strStartupPfad & "Checkit-med.dot") = False Then

    'Vorlage in den Startup Folder kopieren, damit die Funktionen

    'in allen Word Dokumenten zur Verfügung stehen

    ThisDocument.SaveAs strStartupPfad & "Checkit-med", wdFormatTemplate, , , , , , , , , , , , True

End If
 

Exit Sub

errMenü:

MsgBox "Die Checkit-med Menüleiste konnte nicht erstellt werden", , "Menüleiste Fehler"
 

End Sub
 
 
 

Private Sub Document_Open()
 

Dim b As Integer

Dim MyButton As CommandBarButton

Dim booButtonexistiert As Boolean

Dim ControlBar As CommandBar

Dim strPath As String

Dim strStartupPfad As String
 

booSelectionEreignis = False

On Error GoTo errMenü

For Each ControlBar In Application.CommandBars

    If Not ControlBar.BuiltIn And ControlBar.Name = "Checkitmed" Then

        booButtonexistiert = True

        ControlBar.Visible = True

    End If

Next
 
 
 

Set app = Application

    If booButtonexistiert = False Then

        Set ControlBar = Application.CommandBars.Add("Checkitmed", msoBarTop)

        ControlBar.Visible = True

        

          Set MyButton = ControlBar.Controls.Add(msoControlButton)

        MyButton.Style = msoButtonIconAndCaption

        MyButton.Caption = "Öffnen"

        MyButton.OnAction = "VorlageoeffnenmitCheck"

        MyButton.FaceId = 173
 

        Set MyButton = ControlBar.Controls.Add(msoControlButton)

        MyButton.Style = msoButtonIconAndCaption

        MyButton.Caption = "Textmarken"

        MyButton.OnAction = "modCheckitmedProf.TextmarkenEditor"

        MyButton.FaceId = 176
 

      

              

        CommandBars("Checkitmed").Controls.Add Type:=msoControlButton, ID:=225 _

        , Before:=2

     End If

Stop

strStartupPfad = fktPfadInklBackslash(Application.Options.DefaultFilePath(wdStartupPath))

If fktExistiertDatei(strStartupPfad & "Checkit-med.dot") = False Then

    'Vorlage in den Startup Folder kopieren, damit die Funktionen

    'in allen Word Dokumenten zur Verfügung stehen

    ThisDocument.SaveAs strStartupPfad & "Checkit-med", wdFormatTemplate, , , , , , , , , , , , True

End If

Exit Sub

errMenü:

MsgBox "Die Checkit-med Menüleiste konnte nicht erstellt werden", , "Menüleiste Fehler"

End Sub

Open in new window

0
Comment
Question by:rolandkg1001
  • 3
  • 2
5 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 22865883
You could add the template as an Add-in. It then becomes effective immediately.



    Dim strStartUp As String

    Const strTemplate = "MyFile.dot"

    

    strStartUp = Application.Options.DefaultFilePath(wdStartupPath)

    FileCopy "C:\MyFolder\" & strTemplate, strStartUp & "\" & strTemplate

    AddIns.Add strStartUp & "\" & strTemplate, True

Open in new window

0
 

Author Comment

by:rolandkg1001
ID: 22866031
Thanks for your very quick help,

but if I use your solution, my problem is, that I have to know the location of myFolder. Because I do not know, where the users installs my word template.

But if I use your solution like the snipset in Document_New event, I get an error message "Befehl ist nicht vorhanden". err.number: 4605

By the way: fktExistiertDatei is a function which tests if the file exists already.

Can you help me around this error?
If fktExistiertDatei(strStartupPfad & "Checkit-med.dot") = False Then

    'Vorlage in den Startup Folder kopieren, damit die Funktionen

    'in allen Word Dokumenten zur Verfügung stehen

    ThisDocument.SaveAs strStartupPfad & "Checkit-med", wdFormatTemplate, , , , , , , , , , , , True

    AddIns.Add strStartupPfad & "Checkit-med.dot", True

End If

Open in new window

0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 22866698
You don't need the file copy, because your code saves the template to the Startup folder anyway.

Just put the instruction after that.

strStartupPfad = fktPfadInklBackslash(Application.Options.DefaultFilePath(wdStartupPath))

If fktExistiertDatei(strStartupPfad & "Checkit-med.dot") = False Then

    'Vorlage in den Startup Folder kopieren, damit die Funktionen

    'in allen Word Dokumenten zur Verfügung stehen

    ThisDocument.SaveAs strStartupPfad & "Checkit-med", wdFormatTemplate, , , , , , , , , , , , True

    AddIns.Add strStartupPfad & "Checkit-med", True '<------ New line

End If

Exit Sub

Open in new window

0
 

Author Comment

by:rolandkg1001
ID: 22874067
Sorry, but I still get the error 4605. The error tells me that this command is not available.
Is this maybe because I have Checkit-med.dot in an open status?

I use the document_open procedure of checkit-med.dot to save it as addin under the startup folder. Next comes your code line and I get the error

     ThisDocument.SaveAs strStartupPfad & "Checkit-med", wdFormatTemplate, , , , , , , , , , , , True
    On Error GoTo errAddin
    AddIns.Add strStartupPfad & "Checkit-med", True

So can you help me out of this error please?
0
 

Author Comment

by:rolandkg1001
ID: 22874292
I have found out the problem:

the template was protected!

I have changed the code.

thanks for all
strStartupPfad = fktPfadInklBackslash(Application.Options.DefaultFilePath(wdStartupPath))

If fktExistiertDatei(strStartupPfad & "Checkit-med.dot") = False Then

    'Vorlage in den Startup Folder kopieren, damit die Funktionen

    'in allen Word Dokumenten zur Verfügung stehen

        ThisDocument.SaveAs strStartupPfad & "Checkit-med", wdFormatTemplate, , , , , , , , , , , , True

     On Error GoTo errAddin

     AddWordAddin strStartupPfad & "Checkit-med.dot"

End If
 

'in a seperate Modul:
 

Public Sub AddWordAddin(ByVal strDateiPfad As String)
 

Dim oAddin As AddIn

Dim intProtectionType As Integer
 

intProtectionType = wdNoProtection
 

On Error GoTo Err_Addin
 

If Application.ActiveDocument.ProtectionType <> wdNoProtection Then

    intProtectionType = ThisDocument.ProtectionType

    Application.ActiveDocument.Unprotect

End If
 

' Add the Word Addin and Install It

Set oAddin = Application.AddIns.Add(strDateiPfad, True)
 

If intProtectionType <> wdNoProtection Then

    Application.ActiveDocument.Protect intProtectionType

End If
 

Finally:

If Not oAddin Is Nothing Then Set oAddin = Nothing
 

Err_Addin:
 

If Err <> 0 Then

    Err.Clear

   GoTo Finally

End If

End Sub

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now