Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Daring to go back to a class module (VBA Word 2000)

    I've only worked a little with class modules and with only limited success, so bear with me. I'm trying to use the WindowActivate event to hide one toolbar and show another depending on the existence of a custom document property. I've got more to do, but this is my start.

    Here is what I have in my class module (try not to laugh too hard):

    <pre>Public WithEvents App As Word.Application

    Private Sub App_WindowActivate(ByVal Doc As Document, ByVal Wn As Window)

    Dim I As Integer
    For I = 1 To ActiveDocument.CustomDocumentProperties.Count
    If ActiveDocument.CustomDocumentProperties.Item(I).Na me = "BusTechUser" Then
    CommandBars("Disable Template Macros").Visible = True
    CommandBars("Enable Template Macros").Visible = False
    Exit Sub
    End If
    Next I

    CommandBars("Disable Template Macros").Visible = False
    CommandBars("Enable Template Macros").Visible = True

    End Sub</pre>


    Here is what I put in the AutoExec of the global template:

    <pre>Public X As New WhichDocEnableDisable

    Sub Main()
    Register_Event_Handler
    End Sub
    Sub Register_Event_Handler()
    Set X.App = Word.Application
    End Sub
    </pre>



    Right now when I try this, nothing changes with which toolbars are displayed and if I switch to a document that has this particular custom document property, I get a run-time error '5'": Invalid procedure call or argument.

    Please help me clean up this mess and understand how to do this right.

    Thanks!!
    Troy

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Daring to go back to a class module (VBA Word 2000)

    I assume that your class module is called WhichDocEnableDisable, othwerwise this will never work.
    I would prefer a name like clsWordApplication, but I guess that is just a matter of taste.

    The Sub Main() looks wrong, this should have been Sub AutoExec()

    I think there is a problem with your loop and the setting of the .Visible property of your two toolbars. The way it is written you will always change the toolbars back to their original state in the two lines after the loop.

    Try defining a boolean variable such as
    Dim blFoundMyCustomProperty as Boolean

    And then changing the code in the loop to
    If ActiveDocument.CustomDocumentProperties.Item(I).Na me = "BusTechUser" Then
    blFoundMyCustomProperty = True
    Exit For
    End If

    Then after the loop you want
    If blFoundMyCustomProperty then
    CommandBars("Disable Template Macros").Visible = False
    CommandBars("Enable Template Macros").Visible = True
    Else
    CommandBars("Enable Template Macros").Visible = True
    CommandBars("Enable Template Macros").Visible = False
    End If

    StuartR

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Daring to go back to a class module (VBA Word 2000)

    Troy,

    Using a module named AutoExec with a procedure named Main is rather antiquated - it is a relic from WordBasic. Slightly more modern is to use a procedure named AutoExec in a module with an arbitrary name (other than AutoExec), and even better - since you're using class modules anyway - is to put the code in the Document_Open event procedure of ThisDocument.

    I recommend putting the code from the App_WindowActivate event procedure into a procedure in a standard module, and call that procedure from App_WindowActivate. Procedures in standard modules are easier to debug.

    Finally, open the global template as a document, and try to debug the code. Perhaps, the name of one of the toolbars is not spelled correctly. I created a template with your code, but with the names of two toolbars that exist on my PC. It ran without problems.

  4. #4
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Daring to go back to a class module (VBA Word 2000)

    Stuart,
    Thanks for your speedy response.

    Regarding AutoExec, I should have mentioned that this was the main procedure in a module called AutoExec, which I think should work the same as a procedure that is called AutoExec.

    Regarding: "I think there is a problem with your loop and the setting of the .Visible property of your two toolbars. The way it is written you will always change the toolbars back to their original state in the two lines after the loop."

    Unless, I'm missing something, that is not true. If the property is found then Disable Template Macros would be visible, Enable Template Macros would be invisible, and the procedure would exit without running the remaining lines of code.

    Regarding the runtime error, I found I put the wrong name for the toolbar in the code.

    Thanks for the help!!
    Troy

  5. #5
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Daring to go back to a class module (VBA Word 2000)

    Hans,

    Good idea about the AutoExec. Done.

    Regarding putting the code in a standard module, that makes sense, and yes it is easier to debug that way, but for some reason the code doesn't fire when I do that. I moved the code back into the class module and everything works fine. Any ideas?

    Finally, yes, I had the wrong name for the toolbars, I had relabeled them at some point and put the label name instead of the actual toolbar name.

    Thanks!!
    Troy

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Daring to go back to a class module (VBA Word 2000)

    <hr>Regarding putting the code in a standard module, that makes sense, and yes it is easier to debug that way, but for some reason the code doesn't fire when I do that. I moved the code back into the class module and everything works fine. Any ideas?<hr>
    You need a very small procedure in your class module that calls the new procedure in the standard module.

    StuartR

  7. #7
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Daring to go back to a class module (VBA Word 2000)

    That's what I tried, but it did not seem to work.

    Any ideas why?

    Thanks!!
    Troy

  8. #8
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Daring to go back to a class module (VBA Word 2000)

    Did you use a unique name for the procedure in the standard module. If you kept the name App_WindowActivate for both procedures then it wouldn't have worked. Was the standard module in the same document as the class module?

    StuartR

  9. #9
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Daring to go back to a class module (VBA Word 2000)

    Good ideas!! Unfortunately none of them apply.

    I'm not getting terribly complex with anything in the class module, so I'll just leave it there for now and copy the procedures out to a regular module if I need.

    If you have any other ideas feel free to let me know, but I wouldn't lose much sleep over this one.

    Thanks!!
    Troy

  10. #10
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Daring to go back to a class module (VBA Word 2000)

    What doesn't work?

    After I ran the Register_Event_Handler procedure, the WindowActivate event fired just fine for me.
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  11. #11
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Daring to go back to a class module (VBA Word 2000)

    Good clue!! I ran the Register_Event_Handler procedure from the ThisDocument object on the Document_Open event, but apparently that stopped working (no idea why). I added a procedure to run it and called it AutoExec, and everything is fine now.

    Thanks!!
    Troy

  12. #12
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Daring to go back to a class module (VBA Word 2000)

    Were you doing any coding, or any code testing when it stopped working?

    You may have hit End when you hit an error and reset everything.
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  13. #13
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Daring to go back to a class module (VBA Word 2000)

    <P ID="edit" class=small>(Edited by gwhitfield on 28-Mar-03 07:29. Cut width of post )</P>OK. Now I have a problem with my code not firing from the class module. I can't figure out what I changed that would have made a difference. This exact code works fine from a regular module, but none of it works from the class module.

    In my class module I have:

    <pre>Public WithEvents App As Word.Application

    Private Sub App_WindowActivate(ByVal Doc As Document, ByVal Wn As Window)
    Dim I As Integer
    If ActiveWindow.Document.AttachedTemplate = "BrandedDocTemplate.dot" Then
    CommandBars("Disable Template Macros").Visible = True
    CommandBars("Enable Template Macros").Visible = False
    Else
    CommandBars("Disable Template Macros").Visible = False
    CommandBars("Enable Template Macros").Visible = True
    Exit Sub
    End If

    SetMenu:
    'Set menu options based on document type.
    Dim CBar As CommandBar
    Dim CBarSubMenu As CommandBarControl
    Dim CBarSubMenuItem As CommandBarButton

    BusTechUserTemplateName = "BrandedDocTemplate.dot"
    TemplatePathAndName = Options.DefaultFilePath(Path:=wdUserTemplatesPath) & "" & BusTechUserTemplateName
    CustomizationContext = ActiveWindow.Document
    Set CBar = CommandBars("Menu Bar")
    On Error Resume Next
    Dim myCaption As String
    If ActiveDocument.CustomDocumentProperties("BusTechUs er").Value = "Business" Or _
    ActiveDocument.CustomDocumentProperties("BusTechUs er").Value = "User" Then
    With CommandBars("Menu Bar").Controls("Branded Templates").Controls("Headings").Controls("Restore Numbered Headings (Default)")
    .Caption = "Restore Numbered Headings"
    .OnAction = "StyleAppendicesHeadings1Through9BusUserYESNumbere d"
    .HelpFile = "cataFileBrandedTemplatesTech DocBRANDTEMPLATEHELP.HLP"
    .HelpContextId = 2
    End With
    'MsgBox CommandBars("Menu Bar").Controls(1).Move
    With CommandBars("Menu Bar").Controls("Branded Templates").Controls("Headings").Controls("Restore Non-numbered Headings")
    .Caption = "Restore Non-numbered Headings (Default)"
    .OnAction = "StyleAppendicesHeadings1Through9BusUserNONumbered "
    .HelpFile = "cataFileBrandedTemplatesTech DocBRANDTEMPLATEHELP.HLP"
    .HelpContextId = 4
    .Move Before:=1
    End With
    With CommandBars("Menu Bar").Controls("Branded Templates").Controls("Styles").Controls("Restore Numbered Headings (Default)")
    .Caption = "Restore Numbered Headings"
    .OnAction = "StyleAppendicesHeadings1Through9BusUserYESNumbere d"
    .HelpFile = "cataFileBrandedTemplatesTech DocBRANDTEMPLATEHELP.HLP"
    .HelpContextId = 2
    End With
    With CommandBars("Menu Bar").Controls("Branded Templates").Controls("Styles").Controls("Restore Non-numbered Headings")
    .Caption = "Restore Non-numbered Headings (Default)"
    .OnAction = "StyleAppendicesHeadings1Through9BusUserNONumbered "
    .HelpFile = "cataFileBrandedTemplatesTech DocBRANDTEMPLATEHELP.HLP"
    .HelpContextId = 4
    .Move Before:=1
    End With
    ElseIf ActiveDocument.CustomDocumentProperties("BusTechUs er").Value = "Technical" Then
    With CommandBars("Menu Bar").Controls("Branded Templates").Controls("Headings").Controls("Restore Numbered Headings")
    .Caption = "Restore Numbered Headings (Default)"
    .OnAction = "StyleAppendicesHeadings1Through9TechnicalYESNumbe red"
    .HelpFile = "cataFileBrandedTemplatesTech DocBRANDTEMPLATEHELP.HLP"
    .HelpContextId = 2
    .Move Before:=1
    End With
    With CommandBars("Menu Bar").Controls("Branded Templates").Controls("Headings").Controls("Restore Non-numbered Headings (Default)")
    .Caption = "Restore Non-numbered Headings"
    .OnAction = "StyleAppendicesHeadings1Through9TechnicalNONumber ed"
    .HelpFile = "cataFileBrandedTemplatesTech DocBRANDTEMPLATEHELP.HLP"
    .HelpContextId = 4
    End With
    With CommandBars("Menu Bar").Controls("Branded Templates").Controls("Styles").Controls("Restore Numbered Headings")
    .Caption = "Restore Numbered Headings (Default)"
    .OnAction = "StyleAppendicesHeadings1Through9TechnicalYESNumbe red"
    .HelpFile = "cataFileBrandedTemplatesTech DocBRANDTEMPLATEHELP.HLP"
    .HelpContextId = 2
    .Move Before:=1
    End With
    With CommandBars("Menu Bar").Controls("Branded Templates").Controls("Styles").Controls("Restore Non-numbered Headings (Default)")
    .Caption = "Restore Non-numbered Headings"
    .OnAction = "StyleAppendicesHeadings1Through9TechnicalNONumber ed"
    .HelpFile = "cataFileBrandedTemplatesTech DocBRANDTEMPLATEHELP.HLP"
    .HelpContextId = 4
    End With
    End If
    End Sub</pre>



    In a regular module I have:
    <pre>Public X As New WhichDocEnableDisable

    Sub Register_Event_Handler()
    Set X.App = Word.Application
    End Sub
    </pre>


    Any ideas why this is not working from the class module?
    Thanks!!
    Troy

  14. #14
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Daring to go back to a class module (VBA Word 2000)

    Actually, it seems to be sporadic (not a fun thing for my users). Sometimes it works fine. Other times it just does not seem to register the application. I don't know why.

    Troy

  15. #15
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Daring to go back to a class module (VBA Word 2000)

    Is it specific users? What are their macro security settings, High, medium, low? Are they clicking Enable Macros, if presented with the dialog box?

    Just a couple of things that spring to mind.
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

Page 1 of 2 12 LastLast

Posting Permissions

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