Results 1 to 13 of 13
  1. #1
    hadley
    Guest

    CommandBarButton OnAction

    Hi,

    (Word 97) I'm having some problems programmatically creating commandbar buttons with an OnAction thats runs a sub with one parameter, eg. openTemplate("letter.dot"). When I do this I get an unhelpful automation error message (-214... blah blah blah is not a valid setting for OnAction). Interestingly, this error does not occur when I just use openTemplate.

    Can anyone offer any suggestions?

    Thanks,

    Hadley

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: CommandBarButton OnAction

    Post your code and let's take a look...
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  3. #3
    hadley
    Guest

    Re: CommandBarButton OnAction

    Here you go:

    <pre>Sub loadTemplateToolbar()
    Dim strTemplatePath As String, strTemplate As String
    Dim cdbTemplate As CommandBar, cdbtTemplate As CommandBarButton
    Dim i As Integer

    strTemplatePath = Application.Options.DefaultFilePath(wdUserTemplate sPath) & ""

    For i = 1 To CommandBars.Count
    If CommandBars(i).Name = "template" Then
    CommandBars(i).Delete
    Exit For
    End If
    Next i

    If cdbTemplate Is Nothing Then
    Set cdbTemplate = CommandBars.Add("template", msoBarTop, False, False)
    End If

    strTemplate = Dir(strTemplatePath)
    Do Until strTemplate = ""
    Set cdbtTemplate = cdbTemplate.Controls.Add(msoControlButton)
    With cdbtTemplate
    .OnAction = "newTemplate(""" & strTemplate & """)"
    .Parameter = strTemplate
    .Style = msoButtonCaption
    .DescriptionText = Left$(strTemplate, InStr(strTemplate, ".") - 1)
    .Caption = Left$(strTemplate, InStr(strTemplate, ".") - 1)
    .Visible = True
    End With

    strTemplate = Dir()
    Loop

    cdbTemplate.Visible = True

    Set cdbTemplate = Nothing
    Set cdbtTemplate = Nothing

    End Sub
    </pre>


    Hadley
    <img src=/S/cool.gif border=0 alt=cool width=15 height=15>

  4. #4
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: CommandBarButton OnAction

    The syntax for .onaction appears to require no variables in the name. Why not set a global var then read it in newTemplate.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  5. #5
    hadley
    Guest

    Re: CommandBarButton OnAction

    Set a global variable where exactly?

    I've just created a toolbar full of buttons which I can't seem to distinguish in anyway. I can't specify a parameter for the function. I can't tell which button has been clicked. And I'm not going to create a different function for every button. <img src=/S/mad.gif border=0 alt=mad width=15 height=15>

    Hadley
    <img src=/S/cool.gif border=0 alt=cool width=15 height=15>

  6. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: CommandBarButton OnAction

    According to online help, you have at least 2 choices to pass a parameter

    (1) in the line:
    Set cdbtTemplate = cdbTemplate.Controls.Add(msoControlButton)

    expression.Add(Type, Id, Parameter, Before, Temporary)

    "For custom controls, you can use this argument to send information to Visual Basic procedures, or you can use it to store information about the control (similar to a second Tag property value)."

    (2) Tag property of the button control

    Online help doesn't show how to retrieve these values in your procedure...let us know if it works.

  7. #7
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: CommandBarButton OnAction

    Jeff,

    Nice sunset.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  8. #8
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: CommandBarButton OnAction

    Hadley (is this the "old" Hadley?)

    Test Jeff's suggestion. That sounds like the way to go.
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  9. #9
    hadley
    Guest

    Re: CommandBarButton OnAction

    Yes it is the old Hadley and I have tried Jeff's suggestion (before starting this thread) and abandoned it as I can see no way of retrieving the parameter or tag value. ARRRRRGGGGGHHHHHHH. I'll think I'll just abandon the f**cking buttons and use a combobox instead.

    Hadley
    <img src=/S/cool.gif border=0 alt=cool width=15 height=15>

  10. #10
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: CommandBarButton OnAction

    Hadley,

    Does this apply to your situation?

    <pre>Sub newTemplate()
    MsgBox CommandBars("template").Controls(1).Parameter
    MsgBox CommandBars("template").Controls(1).Tag
    End Sub
    Sub loadTemplateToolbar()
    Dim strTemplatePath As String, strTemplate As String
    Dim cdbTemplate As CommandBar

    Dim i As Integer

    strTemplatePath = Application.Options.DefaultFilePath(wdUserTemplate sPath) & ""

    For i = 1 To CommandBars.Count
    If CommandBars(i).Name = "template" Then
    CommandBars(i).Delete
    Exit For
    End If
    Next i

    If cdbTemplate Is Nothing Then
    Set cdbTemplate = CommandBars.Add("template", msoBarTop, False, False)
    End If

    strTemplate = Dir(strTemplatePath)
    Do Until strTemplate = ""
    Set cdbtTemplate = cdbTemplate.Controls.Add(msoControlButton)
    With cdbtTemplate
    .OnAction = "newTemplate"
    .Style = msoButtonCaption
    .DescriptionText = Left$(strTemplate, InStr(strTemplate, ".") - 1)
    .Caption = Left$(strTemplate, InStr(strTemplate, ".") - 1)
    .Visible = True
    .Tag = strTemplate
    .Parameter = strTemplate
    End With

    strTemplate = Dir()
    Loop

    cdbTemplate.Visible = True

    Set cdbTemplate = Nothing
    Set cdbtTemplate = Nothing

    End Sub
    </pre>

    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  11. #11
    hadley
    Guest

    Re: CommandBarButton OnAction

    If there are 6 buttons on the toolbar and you click button 3, does this still work? (sorry, at home and don't have word 97). I'd assume that it would always return the parameter/tag of the first button on the toolbar.

    hadley
    <img src=/S/cool.gif border=0 alt=cool width=15 height=15>

  12. #12
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: CommandBarButton OnAction

    Got it!

    MsgBox CommandBars.ActionControl.Tag

    See <A target="_blank" HREF=http://support.microsoft.com/support/kb/articles/Q159/6/92.asp>ACC97: How to Create Command Bars by Using Visual Basic Code</A>

    However... I have 21 templates, and can't seem to get the toolbar to display them all (there is a "more" chevron). I had thought the text would remain horizontal when docked to the side of the screen, but instead it turns vertical. Maybe a drop-down list...

  13. #13
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: CommandBarButton OnAction

    Here's a helpful picture too:

    <A target="_blank" HREF=http://msdn.microsoft.com/library/default.asp?url=/library/en-us/off2000/html/ofproActionControlX.asp>http://msdn.microsoft.com/library/default....ionControlX.asp</A>
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

Posting Permissions

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