PDA

View Full Version : Solved: Word 97 only: OnAction Method fails



Howard Kaikow
05-19-2005, 02:21 AM
Anybody with Word 97 able to reproduce the following problem?

Code module is followed by a class module.


' The following works in Word 2000, Word 2002, and Word 2003, but not in Word 97.
' Code fails on the OnAction method in SetToolbar and SetMenu.

Option Explicit
Private clsTest As clsTestCommandbars
Private Sub TestMe()
Dim docWord As Word.Document
Dim gappWord As Word.Application

Set gappWord = New Word.Application

Set docWord = gappWord.Documents.Add(newtemplate:=True)

Set clsTest = New clsTestCommandbars

With clsTest
.SetToolbar True, docWord
.SetMenu docWord
End With

With docWord
.SaveAs FileName:="I:\Word-Hodgepodge\Tests\Word97-OnAction\X.dot"
.Close
End With

Set docWord = Nothing
gappWord.Quit
Set gappWord = Nothing
Set clsTest = Nothing
End Sub


Option Explicit
Private cbarToolbar As Office.CommandBar
Private cmdMenu As Office.CommandBarControl

Private strMacros() As String
Private strUserButtonNames() As String
Private strUserMenuItemLabel() As String

Private Const strToolbarname As String = "Bagels"
Private Const strMenuName As String = "&Lox"
Private Const strTag As String = "You're it!"

Private Const lngMaxbutton As Long = 2

Public Sub SetToolbar(blnToolbarBottom As Boolean, docWord As Word.Document)
Dim cmdControl As Office.CommandBarControl
Dim i As Long

With docWord
On Error Resume Next
.CommandBars(strToolbarname).Delete
On Error GoTo 0

If blnToolbarBottom Then
Set cbarToolbar = .CommandBars.Add(Name:=strToolbarname, _
Position:=Office.msoBarBottom, menubar:=False, temporary:=False)
Else
Set cbarToolbar = .CommandBars.Add(Name:=strToolbarname, _
Position:=Office.msoBarTop, menubar:=False, temporary:=False)
End If
End With

With cbarToolbar
For i = 0 To lngMaxbutton
Set cmdControl = .Controls.Add(Type:=Office.msoControlButton, temporary:=False)
With cmdControl
.Style = Office.msoButtonCaption
.Caption = strUserButtonNames(i)
.OnAction = strMacros(i)
.TooltipText = strMacros(i)
.Tag = strTag
End With
Next i
.Enabled = True
.Visible = True
End With
Set cmdControl = Nothing
End Sub

Public Sub SetMenu(docWord As Word.Document)
Dim cmdControl As Office.CommandBarControl
Dim i As Long

With docWord
On Error Resume Next
.CommandBars(strMenuName).Delete
On Error GoTo 0

Set cmdMenu = .CommandBars.ActiveMenuBar.Controls.Add(Type:=Office.msoControlPopup, _
temporary:=False)
End With
With cmdMenu
.Caption = strMenuName
.Visible = True
End With

With cmdMenu
For i = 0 To lngMaxbutton
Set cmdControl = .Controls.Add(Type:=Office.msoControlButton, temporary:=False)
With cmdControl
.Style = Office.msoButtonCaption
.TooltipText = strMacros(i)
.Tag = strTag
.Caption = strUserMenuItemLabel(i)
.Visible = True
.OnAction = strMacros(i)
End With
Next i
' Add item to end of menu
Set cmdControl = .Controls.Add(Type:=Office.msoControlButton, temporary:=False)
With cmdControl
.BeginGroup = True
.Style = Office.msoButtonCaption
.Caption = "About Howard Kaikow's macros"
.OnAction = "RunHelpAbout"
.Tag = strTag
End With
End With
Set cmdControl = Nothing
End Sub

Private Sub Class_Initialize()
ReDim strUserButtonNames(lngMaxbutton)
ReDim strUserMenuItemLabel(lngMaxbutton)
ReDim strMacros(lngMaxbutton)

strMacros(0) = "RunHelpAbout"
strMacros(1) = "RunHelpAbout"
strMacros(2) = "RunHelpAbout"

strUserButtonNames(0) = "Pasta"
strUserButtonNames(1) = "Veggies"
strUserButtonNames(2) = "Orange Juice"

strUserMenuItemLabel(0) = "&" & strUserButtonNames(0)
strUserMenuItemLabel(1) = "&" & strUserButtonNames(1)
strUserMenuItemLabel(2) = "&" & strUserButtonNames(2)
End Sub

MOS MASTER
05-19-2005, 11:32 AM
Hi Howard, :yes

I've made a testdocument and can confirm it works in 2002/2003.

Have 97 at home and will test it when I get home and give you the results tommorow!

:whistle:

Steiner
05-20-2005, 12:40 AM
I could reproduce the error with Word97 and I can vaguely remember I had the same problem a while ago.

But I just can't remember or find any reference whether I could fix it, and if so, how.

First I thought it had to do something about the variable type OnAction was set to. I tried to store the string in a variant first, and then set it, but it did not help, so this must have been another problem...

Daniel

Howard Kaikow
05-20-2005, 10:05 AM
I posted this yesterday, but somehow it did not make it, so here's the answer.
-------------------
I believe that I found the cause of the problem. I'll have to dig thru the Office 97 PG to see if it is documented.


A standard module.




' The following works in Word 2000, Word 2002, and Word 2003, but not in Word 97.
' Code fails on the OnAction method in SetToolbar and SetMenu.

' It appears that Word 97 requires that the macros referenced by OnAction exist
' at the time of creationof the control.
Option Explicit
Private clsTest As clsTestCommandbars
Private Sub TestMe()
Dim docWord As Word.Document
Dim gappWord As Word.Application

Set gappWord = New Word.Application

''''''''''''''''''''''''''''''''''''''
' Create new template with no macros.
Set docWord = gappWord.Documents.Add(newtemplate:=True)
With docWord
.SaveAs FileName:="I:\Word-Hodgepodge\Tests\Word97-OnAction\New.dot"
.Close
End With
Set docWord = gappWord.Documents.Open(FileName:="I:\Word-Hodgepodge\Tests\Word97-OnAction\NoMacros.dot")
''''''''''''''''''''''''''''''''''''''
' Use extant template that contains macros required.
' Set docWord = gappWord.Documents.Open(FileName:="I:\Word-Hodgepodge\Tests\Word97-OnAction\Extant.dot")

Set clsTest = New clsTestCommandbars

With clsTest
.SetToolbar True, docWord
.SetMenu docWord
End With

With docWord
.Save
.Close
End With

Set docWord = Nothing
gappWord.Quit
Set gappWord = Nothing
Set clsTest = Nothing
End Sub

The class.




Option Explicit
Private cbarToolbar As Office.CommandBar
Private cmdMenu As Office.CommandBarControl

Private strMacros() As String
Private strUserButtonNames() As String
Private strUserMenuItemLabel() As String

Private Const strToolbarname As String = "Bagels"
Private Const strMenuName As String = "&Lox"
Private Const strTag As String = "You're it!"

Private Const lngMaxbutton As Long = 2

Public Sub SetToolbar(blnToolbarBottom As Boolean, docWord As Word.Document)
Dim cmdControl As Office.CommandBarControl
Dim i As Long

With docWord
On Error Resume Next
.CommandBars(strToolbarname).Delete
On Error GoTo 0

If blnToolbarBottom Then
Set cbarToolbar = .CommandBars.Add(Name:=strToolbarname, _
Position:=Office.msoBarBottom, menubar:=False, temporary:=False)
Else
Set cbarToolbar = .CommandBars.Add(Name:=strToolbarname, _
Position:=Office.msoBarTop, menubar:=False, temporary:=False)
End If
End With

With cbarToolbar
.Enabled = True
.Visible = True
For i = 0 To lngMaxbutton
Set cmdControl = .Controls.Add(Type:=Office.msoControlButton, temporary:=False)
With cmdControl
.Style = Office.msoButtonCaption
.Caption = strUserButtonNames(i)
.OnAction = strMacros(i)
.TooltipText = strMacros(i)
.Tag = strTag
End With
Next i
End With
Set cmdControl = Nothing
End Sub

Public Sub SetMenu(docWord As Word.Document)
Dim cmdControl As Office.CommandBarControl
Dim i As Long

With docWord
On Error Resume Next
.CommandBars.ActiveMenuBar.Controls(strMenuName).Delete
On Error GoTo 0

Set cmdMenu = .CommandBars.ActiveMenuBar.Controls.Add(Type:=Office.msoControlPopup, _
temporary:=False)
End With
With cmdMenu
.Caption = strMenuName
.Visible = True
End With

With cmdMenu
For i = 0 To lngMaxbutton
Set cmdControl = .Controls.Add(Type:=Office.msoControlButton, temporary:=False)
With cmdControl
.Style = Office.msoButtonCaption
.TooltipText = strMacros(i)
.Tag = strTag
.Caption = strUserMenuItemLabel(i)
.Visible = True
.OnAction = strMacros(i)
End With
Next i
' Add item to end of menu
Set cmdControl = .Controls.Add(Type:=Office.msoControlButton, temporary:=False)
With cmdControl
.BeginGroup = True
.Style = Office.msoButtonCaption
.Caption = "About Howard Kaikow's macros"
.OnAction = "HelloWorld"
.Tag = strTag
End With
End With
Set cmdControl = Nothing
End Sub

Private Sub Class_Initialize()
ReDim strUserButtonNames(lngMaxbutton)
ReDim strUserMenuItemLabel(lngMaxbutton)
ReDim strMacros(lngMaxbutton)

strMacros(0) = "DoThis"
strMacros(1) = "DoThat"
strMacros(2) = "DoWhatever"

strUserButtonNames(0) = "Do This"
strUserButtonNames(1) = "Do That"
strUserButtonNames(2) = "Do Whatever"

strUserMenuItemLabel(0) = "&" & strUserButtonNames(0)
strUserMenuItemLabel(1) = "&" & strUserButtonNames(1)
strUserMenuItemLabel(2) = "&" & strUserButtonNames(2)
End Sub

MOS MASTER
05-20-2005, 10:44 AM
I could reproduce the error with Word97 SR-2

I will try your sollution aswell to see if it fixes it for me aswell

MOS MASTER
05-21-2005, 09:38 AM
Hi Howard, :yes

I can confirm your sollution works on 97 SR-2 aswell! :thumb