I have a MS Word template (.dot file), and sometimes the Debug->Compile option is available, and sometimes it isn't. Why? And why should I want to compile my VBA at all?
If you compile your project (or run a Sub, which automatically compiles the project before running) then the Compile item disappears, since the project is already compiled. As soon as you edit your VBA code, the Compile item will reappear in the Debug menu.
It behaves a bit like the "Save" item in the File menu of some applications e.g. certain text editors. If the file has not changed since it was last saved, then the "Save" item is grayed out.
As for why you would want to compile: it's a good way to test for compile-time errors, without having your code actually run. If you just run it, then it will compile and then proceed to execute if there are no compile-time errors. You may not want this to happen if e.g. your code sends e-mail around, modifies your document, deletes files, or whatever.
Related
I have this problem occur regularly with different workbooks. The file opens with:
Can't find project or library
I press Alt+F11 to open the VBA editor. I try clicking on References in the Tools menu but it doesn't do anything. I try editing the modules but I can't see any code and the windows are frozen. See attached file.
This is a guess, but sounds like there is a macro is running on 'open' of your spreadsheet, but the library it needs is missing (due to early binding, and a missing reference, often due to a different version of office).
Turn your macro settings to 'ask before running' - in office 265,
this is file-->options-->trust center-->trust center
settings-->macro settings-->disable all macros with notification.
reopen the file, but Don't enable the macro
press alt+f11 to enter the macro editor, then look at tools--> references for the
missing reference and try and fix.
Save and relaunch your spreadsheet, and enable macros
I recently discovered the blissful convenience of being able to use the File, Save command from within the VBA Editor window to save the currently attached template without having to actually be in the template or close out of Word.
I thought it would be nice to have this command available on a Ribbon Tab or the QAT or even a keyboard shortcut, so I first added the Save Template command to a Ribbon Tab. But when I clicked on the tab, nothing happened.
I thought perhaps I needed to tweak things a bit with a macro, so I recorded a macro and clicked on the command, to see what the VBA statement would look like.
It looks simple and straightforward enough, very similar to a bazillion other "ActiveDocument" macros I have stored that run without any issues:
ActiveDocument.AttachedTemplate.Document.Save
But when I run the macro, I get the standard "Runtime error" message, with no hint as to what is wrong.
I cannot find any documentation regarding this command in any of my source material or anywhere on Google, nor can I find any discussions about it.
Any ideas what's missing here? Thanks in advance!
As I discovered, and Cindy Meister confirmed, the following works:
ActiveDocument.AttachedTemplate.Save
It will work even if the template is closed. The version that you got from the Macro Recorder, ActiveDocument.AttachedTemplate.Document.Save works only if the template is open. The version above works whether it's open or not. I don't program much in Word, but it looks like a template's Document object/property is instantiated when the template is opened and otherwise is nothing.
I found this code (link below) very useful but is it possible to suppress compile error msgboxes and also capture the compile errors somehow?
Can I compile VBA on workbook open?
For example I am currently reloading common modules in a list of workbooks. The compile code runs after dynamically reloading all the modules into each file, before moving on to the next file. I get a series of msgboxes within VBA Editor, I have to click OK for each box, but I can't stop the code at that point.
Ideally, I want to be able to halt (or pause) mid-process and take action within the file to correct the errors, before continuing with the process and updating the modules in the remaining files.
Is this possible?
I have a word document that has a few userforms and options to auto create an entire document. It works great on all of the installations I have tried from 2007 to 2013. The problem is that one user on one computer gets compile errors when it the user form is launched. The compiler complains that controls do not exist that actually DO IN FACT exist. I had him create a temporary account, just a simple test account, no admin permissions and it runs fine.
I have have tried killing the usual suspects
-.dotm files
- normal.dot files
- wiped word start up folders
- I have reset the word registry key
- I have renamed the appdata\Microsoft\office, appdata\Microsoft\word etc etc etc.
There are no missing references.
More oddities:
it complains about a missing control, lets just say frm.txtname.
I cut the sub that contains the reference and paste it at the bottom POOF it no longer believes the control is missing but...it finds another one to complain about.
I know it is something wonked up in the profile but I cannot find it.
Try this: In VBA editor open Tool bar and click References. You will see the list of references. Unclick those who starts with "MISSING". It should work.
I made some changes in my code and when I went to debug my program I got a build error. The error prompted my anti-virus software (Avast) to block my program as a "suspicious file".
Visual Studio returned this error:
Error 1 Could not copy the file "obj\Debug\CopyFile.exe" because it was not found.
Here is a run down of the events that occurred before this error began to happen.
Added new menu item to menu strip.
Replaced instances of radio button references in the code with the new menu items.
Ran a debug, program worked fine, until I clicked a button.
Found the remaining instances of radio button references in the code and replaced them.
Ran a debug, program was working fine. Exited debug.
Deleted the old radio buttons from the GUI. (They no longer had any references in the code.)
Tried to debug, received an error, antivirus software blocks program exe file as suspicious.
Error Could not copy the file "obj\Debug\CopyFile.exe" because it was not found.
What did I do to the code to cause this? Did I possibly delete some other area of the code on accident and I do not know? I can't see why my program won't debug.
Why don't you simply tell to your antivirus software to do its business in some other places than your working directory? ( Exclude Paths ) and ignore every action done by Visual Studio executables ?
By the way, this is one of the first things to do to get better performance when working with a complex IDE like VS that can create, move, delete thousands of files when working.
If I remember well, the AVAST has its exclusion list in File System Shield, Expert Settings, Exclusions