PDA

View Full Version : Solved: create a working button for word



michelle
06-19-2005, 08:06 AM
Dear VBA Word users,

We are busy to get a button on Word 2000, the button must call a function and not a macro, the code we made starts the function directly and pressing the button nothing is happening.

Can someone pls look ad the code of AddButton and give a suggestion what goes wrong. Code starts with function: runSome

Nice regards,

Michelle. :dunno

Code:
_______________________________________________________


Option Explicit

Private strButton

Function runSome()
strButton = "test"
RemoveAddButton
AddButton
End Function

Private Sub RemoveAddButton()
On Error GoTo ErrHandler
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
Exit Sub

ErrHandler:
MsgBox Err.Description
Set oButton = Nothing
Set oBar = Nothing
End Sub

Private Sub AddButton()
On Error GoTo ErrHandler
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
.Application = printTest()
' .OnAction = printTest()
' .OnAction = printTest()
End With

Set oButton = Nothing
Set oBar = Nothing

Exit Sub
ErrHandler:
MsgBox "AddButton " & Err.Description
Set oButton = Nothing
Set oBar = Nothing
End Sub

Private Function printTest()
MsgBox "test"
End Function


_______________________________________________________

mdmackillop
06-19-2005, 09:56 AM
Hi Michelle, I think your problem is the .Application = PrintTest() line. Change this to

With oButton
.Caption = strButton
.FaceId = 1000
.Style = msoButtonIconAndCaption
.OnAction = "printTest"
End With

I'm not clear why RunSome is a function as it is not returning a value. I would make this a Sub routine, also PrintTest, unless there is more happening here than indicated by your code sample.
Regards
MD

michelle
06-19-2005, 11:30 AM
Thanks mdmackillop for the information.

If I use "printTest" an error directly comes: AddButton Name is a readonly property

I saw also after running the printTest() the same error will occur !

Michelle.

mdmackillop
06-19-2005, 11:46 AM
I had that earlier, but it seems to have disappeared. Here is the full code I'm using

Option Explicit

Private strButton

Sub RunSome()
strButton = "test"
RemoveAddButton
AddButton
End Sub

Private Sub RemoveAddButton()
On Error GoTo ErrHandler
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
Exit Sub

ErrHandler:
MsgBox Err.Description
Set oButton = Nothing
Set oBar = Nothing
End Sub

Private Sub AddButton()
On Error GoTo ErrHandler
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

Exit Sub
ErrHandler:
MsgBox "AddButton " & Err.Description
Set oButton = Nothing
Set oBar = Nothing
End Sub

Public Sub PrintTest()
MsgBox "test"
End Sub


I've found I need to run this from Normal.dot, where the PrintTest macro is "available". If I run RunSome from another document, it creates the new document, button etc., but the button in the new document is not finding the PrintTest macro.

michelle
06-19-2005, 11:57 AM
Hello mdmackillop,

It is working!!

Thanks a lot for your energy on your free weekend!

Michelle. :friends:

mdmackillop
06-19-2005, 12:00 PM
Glad to help.

MD

MOS MASTER
06-19-2005, 12:01 PM
Hi Michelle, :yes

I See several problems in your code.

You must always set "CustomizationContext" in Word because otherwise you'll add the button to Normal.dot and you don't want that.

The biggest problem is that you are running the code from somewhere (I don't know of) and add a new document on which the button must stick. Well if there is a button in that document you have to make sure the code to that button is in that document or in the attached template! (Or global accessable)

Cause otherwise the code won't run.
I've altered your code somewhat and added a attachment so you can see how it works.
The code:
Option Explicit

Private strButton

Sub runSome()
strButton = "test"
ChangeFileOpenDirectory ThisDocument.Path & Application.PathSeparator
Application.Documents.Add Template:="Michelle.dot"
RemoveAddButton
AddButton
End Sub

Private Sub RemoveAddButton()
On Error Resume Next
Application.CommandBars("Standard").Controls(strButton).Delete
Err.Clear
End Sub

Private Sub AddButton()
On Error GoTo ErrHandler
Dim oBar As Office.CommandBar
Dim oButton As Office.CommandBarButton

Application.CustomizationContext = ActiveDocument

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

Err_Exit:
Set oButton = Nothing
Set oBar = Nothing

Exit Sub
ErrHandler:
MsgBox "AddButton " & Err.Description
Resume Err_Exit
End Sub

Private Function printTest()
MsgBox "test"
End Function


Enjoy! :whistle: