Start function after loading add-in

A

AHarm

Hi,

I have made a MS Word add-in with vb6.
Everything works fine when i click on one of the buttons in my
commandbar.
Now i want to call a function automatically when i open a document to
determine the filename of the document.
I can't get this to work.
Here is my code:
------------------------------------------------------------------------------------
Option Explicit
Implements IDTExtensibility2

Dim oHostApp As Object
Dim WithEvents btnSave As Office.CommandBarButton
Dim WithEvents btnFields As Office.CommandBarButton
Dim WithEvents btnGecorrigeerd As Office.CommandBarButton
Dim WithEvents btnTest As Office.CommandBarButton
Dim msgAntwoord As VbMsgBoxResult
Dim dlgSaveAs As FileDialog


Private Sub IDTExtensibility2_OnConnection(ByVal Application As
Object, _
ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
ByVal AddInInst As Object, custom() As Variant)

On Error Resume Next
' Set a reference to the host application...
Set oHostApp = Application
' If you aren't in startup, then manually call
OnStartupComplete...
If (ConnectMode <> ext_cm_Startup) Then Call
IDTExtensibility2_OnStartupComplete(custom)

End Sub

Private Sub IDTExtensibility2_OnStartupComplete(custom() As
Variant)
Dim oCommandBars As Office.CommandBars
Dim oMISBar As Office.CommandBar
On Error Resume Next
' Set up a custom button on the "Standard" commandbar...
Set oCommandBars = oHostApp.CommandBars
' Create a custom toolbar with the name MIS, Floating on
start-up
Set oMISBar = oCommandBars.Add("MIS",
Office.MsoBarPosition.msoBarTop, False, True)

If oCommandBars Is Nothing Then
' Outlook has the CommandBars collection on the
Explorer object
Set oCommandBars =
oHostApp.ActiveExplorer.CommandBars
End If

Set oMISBar = oCommandBars.Item("MIS")
' Show the toolbar at start-up
oMISBar.Visible = True
If oMISBar Is Nothing Then
' Access names it's main toolbar Database
Set oMISBar = oCommandBars.Item("Database")
End If

' In case the button was not deleted, use the exiting
one...
Set btnSave = oMISBar.Controls.Item("MIS")
If btnSave Is Nothing Then
Set btnSave = oMISBar.Controls.Add(1)
With btnSave
.Caption = "MIS: Opslaan"
.Style = msoButtonIconAndCaption
.FaceId = 3

' The following items are optional, but recommended.
' The Tag property lets you quickly find the control
' and helps MSO keep track of it when there is more
than
' one application window visible. The property is
required
' by some Office applications and should be
provided.

.Tag = "Opslaan"

' The OnAction property is optional but recommended.
' It should be set to the ProgID of the add-in, such
that if
' the add-in is not loaded when a user presses the
button,
' MSO loads the add-in automatically and then raises
' the Click event for the add-in to handle.

.OnAction = "!<MyCOMAddin.Connect>"
.ToolTipText = "Sluit het document en sla het
op in het MIS"
.Visible = True
End With
End If
'Button Fields
Set btnFields = oMISBar.Controls.Item("MIS")
If btnFields Is Nothing Then
Set btnFields = oMISBar.Controls.Add(1)
With btnFields
.Caption = "MIS: Velden"
.Style = msoButtonIconAndCaption
.FaceId = 501
.Tag = "Velden"
.OnAction = "!<MyCOMAddin.Connect>"
.ToolTipText = "Selecteer de velden voor de
raamwerkbrief"
.Visible = True
End With
End If

'Button Gecorrigeerd
'Slaat de laatste versie van het document op in de
database en zet de status op "Gecorrigeerd".
Set btnGecorrigeerd = oMISBar.Controls.Item("MIS")
If btnGecorrigeerd Is Nothing Then
Set btnGecorrigeerd = oMISBar.Controls.Add(1)
With btnGecorrigeerd
.Caption = "MIS: Gecorrigeerd"
.Style = msoButtonIconAndCaption
.FaceId = 161
.Tag = "Gecorrigeerd"
.OnAction = "!<MyCOMAddin.Connect>"
.ToolTipText = "Sluit het document en sla het
op in het MIS"
.Visible = True
End With
End If

'Button Test
Set btnTest = oMISBar.Controls.Item("MIS")
If btnTest Is Nothing Then
Set btnTest = oMISBar.Controls.Add(1)
With btnTest
.Caption = "MIS: Testen"
.Style = msoButtonIconAndCaption
.FaceId = 1605
.Tag = "Testen"
.OnAction = "!<MyCOMAddin.Connect>"
.Visible = True
End With
End If

Set oMISBar = Nothing
Set oCommandBars = Nothing
End Sub

Private Sub IDTExtensibility2_OnDisconnection(ByVal RemoveMode As
_
AddInDesignerObjects.ext_DisconnectMode, custom() As
Variant)

On Error Resume Next
If RemoveMode <> ext_dm_HostShutdown Then _
Call IDTExtensibility2_OnBeginShutdown(custom)

Set oHostApp = Nothing

End Sub

Private Sub IDTExtensibility2_OnBeginShutdown(custom() As Variant)
'Deze functie is belangrijk voor het opruimen van alle variabelen
bij het afsluiten.
On Error Resume Next
btnSave.Delete
Set btnSave = Nothing
btnFields.Delete
Set btnFields = Nothing
btnGecorrigeerd.Delete
Set btnGecorrigeerd = Nothing
btnTest.Delete
Set btnTest = Nothing
opslaan.CloseDataConnection
End Sub

Private Sub btnSave_Click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
If InStr(ActiveDocument, "MIS") > 0 Then 'De knoppen mogen alleen
maar werken bij een MIS document.
Call opslaan.saveData(ActiveDocument.path, ActiveDocument,
ActiveDocument, "Gesloten")
Else 'Dit is geen MIS document
MsgBox "U kunt de knop 'MIS: Opslaan' alleen gebruiken bij een
MIS document." 'Melding naar gebruiker
End If
End Sub

Private Sub btnFields_Click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
If InStr(ActiveDocument, "RAMWRKBRF_") > 0 Then 'De MIS-knoppen
mogen alleen maar werken bij een MIS document.
SelecteerVelden.Show 'opent het scherm met de te selecteren
velden
SelecteerVelden.SetFocus 'toont het scherm met de te
selecteren velden
Else
MsgBox "U kunt alleen maar velden toevoegen aan een
raamwerkbrief." 'RMWRKBRF controle
End If
End Sub

Private Sub btnTest_Click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
Call maakbrief.maakbrief
End Sub

Private Sub btnGecorrigeerd_Click(ByVal Ctrl As
Office.CommandBarButton, CancelDefault As Boolean)
msgAntwoord = MsgBox("Weet u zeker dat u het document
gecorrigeerd heeft en wil opslaan? Het document zal worden gesloten",
vbQuestion Or vbYesNo, "MIS: Gecorrigeerd")
If msgAntwoord = vbYes Then
MsgBox "Yes"
Else
MsgBox "No"
End If
End Sub

Private Sub IDTExtensibility2_OnAddInsUpdate(custom() As Variant)
'You do nothing if this is called, but you need to
'add a comment so Visual Basic properly implements the
function...
End Sub

--------------------------------------------------------------------------------------------------------
I tried to add the call of the function to
"IDTExtensibility2_OnStartupComplete" but that doesn't work.
The only thing that works for me is adding a msgbox with a string.

Does anybody know how a can automaticly start a function when the
document is open without using a macro?
 

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