PDA

View Full Version : Solved: Inserted button is searching for macro in stead of a function



michelle
06-20-2005, 02:34 AM
Dear Word VBA users,



At the moment I am trying to get with the help of a self made and registered dll file a button on Word 2003, everything goes well only when the button is pressed a macro with the name "PrintTest" is being searched. Such a macro is not available so the function is not activate. Can someone tell me pls how I change the code to start by pressing the button the function "PrintTest".



I tried:

.OnAction = "PrintTest"

.OnAction = "PrintTest()"

.OnAction = "=PrintTest()"



Thanks for any help.



Michelle. :dunno





Source code:



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Option Explicit

Private strButton



Private Sub Class_Initialize()

strButton = "test"

RemoveAddButton

AddButton

End Sub



Private Sub RemoveAddButton()

Dim N As Integer

Dim oBar As Office.CommandBar

Dim oButton As Office.CommandBarButton



Documents.Add DocumentType:=wdNewBlankDocument



Set oBar = Application.ActiveDocument.CommandBars("Standard")



For N = oBar.Controls.Count To 1 Step -1

If oBar.Controls.Item(N).Caption = strButton Then

oBar.Controls.Item(N).Delete

End If

Next N



Set oButton = Nothing

Set oBar = Nothing

End Sub



Private Sub AddButton()

Dim oBar As Office.CommandBar

Dim oButton As Office.CommandBarButton



Set oBar = Application.ActiveDocument.CommandBars("Standard")

Set oButton = oBar.Controls.Add(Type:=msoControlButton, Before:=1, Temporary:=True)



With oButton

.Caption = strButton

.FaceId = 1000

.Style = msoButtonIconAndCaption

.OnAction = "PrintTest"

End With



Set oButton = Nothing

Set oBar = Nothing

End Sub



Public Sub PrintTest()

MsgBox "this is a test of me"

End Sub

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

sandam
06-20-2005, 05:30 AM
The best i can suggest is to make the sub a private sub because your button will call it within the dll.



Private Sub PrintTest

sandam
06-20-2005, 05:34 AM
Uhhh, nevermind my last post. You are setting your button = nothing before you have called the procedure. Rather set your button = nothing after you have called the sub PrintTest. Other wise it creates the button and then deletes the button.

michelle
06-20-2005, 06:07 AM
Hi Sandam



I don't understand what you mean, I think ?Set oButton = Nothing? is only used after the creating of the button.

For you I did remove all ?= Nothing?

Still:

If

.OnAction = PrintTest()

The function is directly called and pressing the button after, nothing happened

And

If

.OnAction = "PrintTest" or .OnAction = "=PrintTest()"

The button is searching for an macro!



Michelle.:dunno





Source:

Option Explicit

Private strButton

Private Sub Class_Initialize()
strButton = "test"
RemoveAddButton
AddButton
End Sub

Private Sub RemoveAddButton()
Dim N As Integer
Dim oBar As Office.CommandBar
Dim oButton As Office.CommandBarButton

Documents.Add DocumentType:=wdNewBlankDocument

Set oBar = Application.ActiveDocument.CommandBars("Standard")
For N = oBar.Controls.Count To 1 Step -1
If oBar.Controls.Item(N).Caption = strButton Then
oBar.Controls.Item(N).Delete
End If
Next N
End Sub

Private Sub AddButton()
Dim oBar As Office.CommandBar
Dim oButton As Office.CommandBarButton

Set oBar = Application.ActiveDocument.CommandBars("Standard")
Set oButton = oBar.Controls.Add(Type:=msoControlButton, Before:=1, Temporary:=True)
With oButton
.Caption = strButton
.FaceId = 1000
.Style = msoButtonIconAndCaption
.OnAction = "PrintTest"
End With
End Sub

Private Function PrintTest()
MsgBox "this is a test of me"
End Function

sandam
06-20-2005, 06:26 AM
I'm sorry, I'm having a slow day :). You were right with your first piece of code. As to why it doesn't call the procedure, I'm not sure. Perhaps if you changed .OnAction = "PrintTest" to .OnAction ="Me.PrintTest"? For your call to look to the class for the procedure. or instead of me, use the dll name?

I'm afraid I don't have the tools to do this at the moment. PC troubles.... :banghead

sandam
06-20-2005, 06:28 AM
also - if you put your code into vba tags, it becomes more readable. [ v b a ] [ / v b a ]

fumei
06-20-2005, 09:00 AM
Correct. Use .OnAction = Me.PrintTest