none
MS Office 2003 VBA macro constructor question

    Question

  • Please pardon my inexperience. I have the following VBA macro:

    Public Sub Class_Initialize()
        Let this.i = 0
        MsgBox "myClass Initialized"
    End Sub
    Private Static Property Let i(value As Variant)
        i = value
    End Property
    Private Property Get i()
    End Property

    It fails on the first line ( Let this.i = 0 ) with the following error:

          Run-time Error '424': Object expected.

    Surely its something simple.

    Thanks in advance for any help you may be able to provide.

    Wednesday, November 30, 2011 10:34 PM

All replies

  • I am trying to execute the following VB code:

       CustomizationContext = ActiveDocument
       KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, _
        wdKeyY), KeyCategory:=wdKeyCategoryMacro, _
        Command:="DoIt"

    and receive the following error:

    Run-time error '5346': Word cannot change the function of the specified key

    My intent is to assign a "hot-key" within the document.

    What key(s) can I assign, and/or what can I do to enable this to work?

    Thanks in advance for any help you may  be able to provide.

    Wednesday, November 30, 2011 10:03 PM
  • Since you have set the CustomizationContext to the active document, did you also store the macro in the active document?


    Stefan Blom, Microsoft Word MVP
    Wednesday, November 30, 2011 10:26 PM
  • What is the default? I used the "Tools + Macro + Visual Basic Editor" menu item in Word, then created a Class Module "myClass" in Project Explorer, that I instanciated in VBA macro Module "Module1" with the new operator. I then saved the document with the Visual Basic Editor "File + Save" menu item.

    Wednesday, November 30, 2011 10:44 PM
  • In the user interface, the default is the Normal template. In VBA, I don’t think there is a default.
     
    But are you saying that you created Module1 in the document “project”?

    Stefan Blom, Microsoft Word MVP
    Wednesday, November 30, 2011 11:05 PM
  • It would seem that my two posts have been combined.

    In both cases, I have saved the macros wherever Word chooses to save it. I am using the Word user interface to the Visual Basic Editor.

    Thanks for any insight you may be able to provide to either of my problems.

    Thursday, December 01, 2011 1:03 PM
  • "John L" wrote in message news:fa9dec33-efd2-447a-a0ca-cc9617169071@communitybridge.codeplex.com...
    In both cases, I have saved the macros wherever Word chooses to save it. I am using the Word user interface to the Visual Basic Editor.
     
     
     
    That would suggest the Normal template. If you change CustomizationContext to NormalTemplate, does that make a difference? That should help Word in locating the macro.

    Stefan Blom, Microsoft Word MVP
    Thursday, December 01, 2011 1:14 PM
  • Problem 1:

    It fails on the first line ( Let this.i = 0 ) with the following error:
    
          Run-time Error '424': Object expected.
    

    Problem 2:

    I am trying to execute the following VB code:
    
       CustomizationContext = NormalTemplate
       KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, _
        wdKeyY), KeyCategory:=wdKeyCategoryMacro, _
        Command:="DoIt"and receive the following error:
    
    Run-time error '5346': Word cannot change the function of the specified key
    

    Problem #2 fails with NormalTemplate also. I've done a little more investigation, and have found that the key combination Cntl+Shift+Y is currently unassigned in the Word document of interest.

    Once again, I apologize for the problems being intertwined. I'm learning to use the forum tool.

    Thanks again,


    • Edited by John-L_ Thursday, December 01, 2011 9:31 PM grammar
    Thursday, December 01, 2011 9:30 PM
  • For the first problem, try using Me when referring to the current object in a class definition (not “this”). But note that the reference may not even be needed; you can work with the variables directly.
     
    As far as the second problem is concerned, I don’t know why you can’t reassign the key, though. :-(  Does it work if you try it in the user interface?

    Stefan Blom, Microsoft Word MVP
    Thursday, December 01, 2011 10:25 PM