PDA

View Full Version : Solved: VBA stopped working?



GrandpaB
07-22-2007, 08:30 PM
Sorry, but this is a super newbe post. I created my first "Hello World" application in Word using VBA. It consisted of a command button whose click event called MsgBox "Hello World." It was working fine, but then being curious I clicked around the IDE, added and deleted a module to the Project (HelloWorld), and somewhere I broke it! Now, when I click the command button it animates up and down, but now the MsgBox does not display and a Breakpoint that I put in the click event handler never breaks. I've opened and closed the document and opened and closed Word, but it is determined to remain broken.

Any suggestions you might have would be appreciated, thanks

fumei
07-23-2007, 03:00 AM
added and deleted a module to the Project Huh? Please expand on this.

Post a file with your code, and we can look at it.

fumei
07-23-2007, 10:30 AM
Oh, and in particular, please clarify "commandbutton". Is this a commandbutton on a userform, or an ActiveX commandbutton IN the document?

GrandpaB
07-23-2007, 01:59 PM
fumei,

I clicked on the command button icon in the tool box to create the button in my document. I then right clicked the button and changed two properties. The name became btnHI and the background color became Orange. I then double clicked the button and in the event handler the code became:

Option Explicit
Private Sub btnHI_Click()
MsgBox "HI"
End Sub


Oops, I also changed the button caption to HI and the font to Bold.

As you can see I didn't have a lot invested in this project, so I saved and closed the doucement, the VB editor and began anew. I then opened Word and in a new document I recreated the project. This time the button works as it should.

I can not see the difference in the two projects. I broke the first one, I don't know why and I can't fix it. My fear is that I will have a big project, a really big project and break it. How can I determine what went wrong?

A second question; where is the code stored? In the project Explorer I see ThisDocument under Microsoft Word Objects, and Reference to Normal under References.

Again, thanks for your assistance.

fumei
07-24-2007, 01:46 PM
Sorry, but you are not answering the questions I asked.

It looks like you used the Controls toolbar and put in an ActiveX commandbutton. "in the toolbox" does not really help, as toolbox would (to most of us) mean the toolbox in the VBE. If THAT is the case, then it is NOT an ActiveX control in the document, but a commandbutton on a userdform. However, I think it IS a control from the Controls toolbar.

Please try to post as clearly as possible what you DO.

The Control toolbar is not shown by default, so let me see if I can suggest what you could have posted.

I used View > Toolbars to get the Controls toolbar.
I used the Controls toolbar to make a commandbutton in the document.

OK, code for ActiveX controls (from the Controls toolbar) natively defaults to the ThisDocument module of the Project.

You can not delete this module. Therefore you can NOT delete the module that has the code for your commandbutton.

I have no idea what you are talking about when you state you deleted a module. I asked....you did not answer.

1. You must turn on Design Mode to be able to right click a control. Design Mode is ON when you first insert a control.

2. Changing the font, and the Caption makes absolutely no difference to code execution.

3. You must be NOT in Design Mode for the button to work in the document. The code WILL work executed [/b]from the VBE[/b].

I doubt very much you broke anything. If you saved the first one, open it, make sure it is NOT in Design Mode, and try the button.

You are in Design Mode if you can select the button. That is, if you click it, you get the handles around it. Or you can right click and get choices (Properties, View Code etc.).

To turn OFF Design Mode, display the Controls toolbar (View > Toolbars > Controls), and click the left-most icon. It looks like a triangle with a pencil above it.

If you going to do this often, keep the Controls toolbar up. I do.

GrandpaB
07-24-2007, 05:01 PM
Fumei,

Yes, we are having a communications problem. I'm sure that it is due to my not knowing how to describe the objects that you are talking about. It is a problem answering a super newbe post. Let me begin at the beginning and search for the correct terms.

1.)I opened a new Word document and added the Visual Basic toolbar.
2.)On the VB toolbar I clicked the Control Toolbar icon.
3.)On the Control Toolbox I clicked the Command Button. and lo a button appeared on the document.
4.)I noticed that on the VB toolbar the Design Mode icon was highlighted.
5.)I right clicked on the button, a menu opened & I selected Properties.
6.)In the Properties dialog I changed the Caption to "HI" (without quotes), the background color to Orange, and the Name to "btnHI."
7.)I closed the Properties dialog and double clicked the button.
8.)This opened the Visual Basic Editor.
9.)I modified the btnHI_Click event handler to like this:
Private Sub btnHI_Click()
MsgBox "HI"
End Sub
10.)On the Windows XP Start bar, I clicked the button with the Word icon to return to the document.
11.)On the VB Toolbar I clicked Exit Design Mode.
12.)On the document I clicked the Orange HI button and the MsgBox displayed.
13.)The application was working and then I started exploring the Visual Basic Editor.
14.)I clicked all the items in VB Editor's Menu Bar and Tool Bar.
15.)Then in the VB Editor, I explored the Project Explorer. I expanded (clicked the + signs) all the items in the Tree View. I assumed that the code resided in Normal>Modules>New Macros. Oh my, I didn't want to modify the Normal styles. In the Project Explorer I right clicked Project, in the menu that opened I clicked Insert>Module.
16.)In The Project Explorer there was now some new branches in the tree view Project>Modules>Module1.
17.)I Cut (ctrl-X) the existing code and Pasted (ctrl-V) into Module1.
18.)I went back to the document, made sure that I was not in Design Mode and clicked the HI button. My wonderful application failed!
19.)I attempted to fix the application. I went back to the Project Explorer and right clicked Module1. In the menu that opened I clicked remove.
20.)Now the Project Explorer looked as it did before I started my exploration.
21.)I went back to the document, put it in Design Mode, double clicked the HI button and recreated the code as I previously described.
22.)The application remains broken.

Fumei, I hope that I have properly explained the sequence of events that caused my problem. I want to know if I can recover from the black hole that I've fallen into? I also want to know where the code resides? I'm now certain that my assumption about it being in the Normal style is incorrect.

I do appreciate your patience with my inexperience. Thanks again.

fumei
07-26-2007, 07:58 AM
Wow. Thank you. I sincerely admire your persistence. It is to be commended.

OK.
Let's do this in chunks.

1.)I opened a new Word document and added the Visual Basic toolbar.
2.)On the VB toolbar I clicked the Control Toolbar icon.
3.)On the Control Toolbox I clicked the Command Button. and lo a button appeared on the document.


Do not do this. Clicking the Control Toolbox on the VB toolbar simply displays the Controls Toolbar. Bypass this, and just display the Controls toolbar.

6.)In the Properties dialog I changed the Caption to "HI" (without quotes), the background color to Orange, and the Name to "btnHI."

You must be using an unusual Windows color scheme, as Orange is not normally available.

I have some serious comments, but I will save them for a moment. Here is your problem.

17.)I Cut (ctrl-X) the existing code and Pasted (ctrl-V) into Module1.
18.)I went back to the document, made sure that I was not in Design Mode and clicked the HI button. My wonderful application failed!

You made a new module, and it looks like you made it under Normal. That is, you added Module1 to Normal.dot. NOT, repeat NOT, the document you had.

ActiveX controls, the button, are objects of the document

ThisDocument module:Private Sub btnHI_Click()
MsgBox "HI"
End Sub will work.

ANY other module, it will NOT work. In any other module VBA has no idea what btnHi is. The button is an object of the document. The ThisDocument module obviously refers to the document, so the code runs.

Any other module.....nope. Unless you reference it directly. So in any other module:Sub Yadda()
Application.Run MacroName:="ThisDocument.btnHi_Click"
End Subwill execute the procedure btnHi_Click IF it is in fact in ThisDocument.

However, it sounds like you moved the procedure btnHi to another module, in which case....it won't work. VBA has NO idea what btnHi refers to.

21.)I went back to the document, put it in Design Mode, double clicked the HI button and recreated the code as I previously described.
22.)The application remains broken.

I did this, and it worked fine. So you must have done something else. Check that the module is ThisDocument.

OK, some comments.

Learn about scope. This is crucial in using any programming. Objects and variables have scope, that is, the legitmate...ummmm...region...that they validly function. You problem was the Sub btnHi was no longer in scope.

This also applies to your question regarding where the code resides.

It depends. What do you mean by "the code"?

Double clicking a control (while in Design Mode) will put the code into the ThisDocument module of the document.

NOT Normal.dot.

"Projects" are VBA projects. Take a look in the Project Explorer.

Normal

Project(document_name)

These are separate entities. They have their own scope.


I'm now certain that my assumption about it being in the Normal style is incorrect.
Whooooaaa. Slow down. "style" is a very specific term in Word. It has nothing to do with code.

It seems clear to me that you are interested in learning this stuff. Here are some suggestions.

Do NOT use Normal to store code. If you record a macro, the default location to store it is Normal. Do not do this.

Make a blank document. Save it as a template...NOT a document. Call it, say, myCode. Do NOT save it in the default location Word wants to save templates. Find the Startup folder for Word.

On my system it is:

C:\Program Files\Microsoft Office\Office10\Startup

Save the new template file (myCode.dot) there. This makes it a global template. It will load automatically.

Now in Normal.dot (the Normal project), if you have a module already you can use that and write it directly. If you want to use a keyboard shortcut, record a macro setting a keyboard shortcut. Make sure you give it a sane name. Say...GetMyCode. Click the keyboard button on the macro dialog, and set a shortcut. Say you set it to use the keyboard shortcut Alt-M. Just start the recording, then stop it. In the NewMacros module (automatically created if there is no module) of Normal, you will see something like:Sub GetMyCode()
'
' Macro1 Macro
' Macro recorded 7/26/2007 by Gerry Knight
'
End SubIt is empty, as I did not do anything. It does NOT record what you used for the keyboard shortcut. So immediately write that in, so you will know what it is.
Sub GetMyCode()
'
' GetMyCode Macro
' keyboard shortcut = Alt-M
' Macro recorded 7/26/2007 by Gerry Knight
'
End SubNext step, add an instruction to open your global template.
Sub GetMyCode()
'
' GetMyCode Macro
' keyboard shortcut = Alt-M
' Macro recorded 7/26/2007 by Gerry Knight
Documents.Open _
Filename:="C:\Program Files\Microsoft Office\Office10\Startup\myCode.dot"
End SubThis will make it easy to get at your code storing file.

To review.

Make a template file that will hold your code. That is ALL it does. The reason you do this is because normal.dot gets corrupted. It is heartbreaking to lose months of code work. Keep it separate as much as possible. You can still record macros. Just make sure that they are record to your myCode file, NOT normal.dot (which again, is the default).

Set a macro to open that file easily. That is the purpose of the Alt-M shortcut. Unlike normal.dot, to add, edit the code in your global, you MUST open the file itself.

However, any code that you have in your global will be (if the file is in Startup) available in ALL files opened in Word.

Next. Control codes (like for your button, as objects of the document. Therefore the code for them must be in the ThisDocument module of the document itself, OR there is an explicit reference to the event procedure.

GrandpaB
07-27-2007, 06:03 PM
Thanks for your advice; I've printed and will analyze it. In the mean time I fooled around with VBA. I think that I found the source of my problem. The security level for Macros was set to high. I set the security level to medium and the button works. I set the security to high and the button animates when it is clicked, but the click event does not fire.

Your suggestions on where to store the code was very helpful, thanks.

fumei
07-28-2007, 11:29 AM
Oh...yes, if Security is High, ActiveX control events are disabled.