Results 1 to 2 of 2
  1. #1
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Word 97 only: OnAction method fails (Word 97)

    Note: I've replaced the original post to include a better example.

    Anybody with Word 97 able to reproduce the following problem?

    Code module is followed by a class module.

    <pre>' 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-HodgepodgeTestsWord97-OnActionX.dot"
    .Close
    End With

    Set docWord = Nothing
    gappWord.Quit
    Set gappWord = Nothing
    Set clsTest = Nothing
    End Sub
    </pre>



    <pre>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:=Offi ce.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
    </pre>


  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word 97 only: OnAction method fails (Word 97)

    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.
    <pre>' 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-HodgepodgeTestsWord97-OnActionNew.dot"
    .Close
    End With
    Set docWord = gappWord.Documents.Open(FileName:="I:Word-HodgepodgeTestsWord97-OnActionNoMacros.dot")
    ''''''''''''''''''''''''''''''''''''''
    ' Use extant template that contains macros required.
    ' Set docWord = gappWord.Documents.Open(FileName:="I:Word-HodgepodgeTestsWord97-OnActionExtant.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
    </pre>


    The class.
    <pre>
    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).D elete
    On Error GoTo 0

    Set cmdMenu = .CommandBars.ActiveMenuBar.Controls.Add(Type:=Offi ce.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
    </pre>


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •