Word Save Template command returning error - vba

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.

Related

VBA: How to edit Attribute inside a Procedure?

I recently discovered that is possible to use Attribute [ProcedureName].VB_*, inside procedures.
What I found
But this code is not shown inside VBA Editor.
Same code in VBA Editor (left) and exported .cls in Notepad (right)
When Attribute is written inside VBA Editor, it's marked red
Attributes added manually in the VBA Editor
The question
Is there more elegant way how to maintain these Attributes than export class module, edit them outside and import them back (facepalm needed)?
Also I haven't found official documentation for these Attributes (on https://learn.microsoft.com/en-us/office/vba/).
Thank you!
PS: As always I hitted something interesting and related just after Question had been sent. A step-by-step example on Microsoft Docs site suggests to do it by Export-Edit-Import method :(
The process for getting Rubberduck to apply annotations requires a few steps.
Execute a Rubberduck refresh. If the code does not compile cleanly then Rubberduck will show a 'Parse Error'. In this case you need to sort out your code first. This can be an issues if you are applying Rubberduck to an existing code base. If not then learn to compile and rubberduck.Refresh early and regularly (i.e. after every complete edit)
If Rubberduck parses successfully you then need to look at Rubberduck.Code Inspections. To take action about an inspection result select the individual result and right click. This will provide a list of actions that can be applied by Rubberduck.

Auto Execution of AutoOpen Word Macro

I was having requirement to run the Macro when we open the document. For that purpose i have created .Dotm template with AutoOpen method. Problem is it doesn't execute. When i add the AutoOpen macro in Normal file it works perfectly.
We don't want to add the AUtoOpen code in Normal.dotm file. How can we achieve the same using different Template file.
I had exactly the same problem. I had 2 separate .dotms with AutoOpen modules that were not auto opening on XP or Win7 x64 both with Office 2010. I subsequently noticed that all my Subs in the AutoOpen module were Private. Hmmmmm. Made my Main Sub Public, left all other Subs Private, and both of them now AutoOpen. Maybe this will help.
"For that purpose i have created .Dotm template with AutoOpen method."
Create new documents with this NotNormalWithAutoOpenMethod.Dotm with File | New.
For an existing document, create a new document based on NotNormalWithAutoOpenMethod.Dotm. Copy the contents of the existing document into the new blank document. Save and Close. Upon reopening the new document, the code should run.
Wasn't me, but I'm guessing you got downvoted because you haven't said what you've tried, what version of Word you're using, or what OS you're on. All of those things are useful in diagnosing the issue.
I'm also guessing that you can not simply save the macro to whatever document you want to run it in, because that should be trivial. Am I correct?
If so, I am trying to do something very similar using Word 2010 on Windows 7 64-bit, but I can't get it to work yet either. The document I need to open is auto-generated from pandoc and is opened in compatibility mode, so perhaps that's my issue. If that's not the case for you, some of the things I've tried might help you.
Use the /t option from the command line: start winword /tmacros.dotm document.docx
If that doesn't work, try adding the /m option as well: start winword /tmacros.dotm /mAutoOpen document.docx.
Instead of naming the macro AutoOpen, try AutoExec or Document_Open.
Note: there is no space between the /t or /m options and the filenames. I've also tried the full path to winword.exe instead of start winword but that didn't make a difference.
Hope this helps, good luck! And please post the solution back here if you figure it out on your own.
Sources:
Command line switch: http://support.microsoft.com/kb/290961
AutoExec/AutoOpen: http://support.microsoft.com/kb/286310

Where do I put macros (besides in normal) so they are always available in word 2013?

I had a bunch of macros that I had in C:\Users\userx\AppData\Roaming\Microsoft\Templates that were always available in word 2010 but since upgrade to word 2013 I cannot run them. Word apparently does not automatically load them anymore. I used to just do alt F8 and pick from the list. I also had a few added to the quick options bar but they are gone also.
Searched this site and MS website and can't seem to find a solution. Is there another location that I need to move the files containing the macros to or is there some other action that I need to take so I can run them again?
My guess is this is already answered here and I am just not searching for the right things. My apologies if that is the case, please just point me to the appropriate post.
You can place a shortcut to the templates containing your macros in the following location
C:\Users\%username%\AppData\Roaming\Microsoft\Word\STARTUP
That will make your macros available whenever Word opens and restore the Alt+F8 functionality you're looking for. If you add toolbars to your templates, those will also automatically be attached to the Normal template when you open Word.

How can I call a VBA macro that is defined in a different file?

The situation is the following:
Macro Foo is defined in File foo.vss
Macro Bar is defined in File bar.vsd.
Bar() is at some point supposed to call Foo().
Is that possible?
If it helps:
foo.vss is a stencil file that is opened in bar.vsd.
FYI:
They are forcing me to use Visio/vba. Its a strange old System that generates SQL from the diagrams, really ugly.
This is possible, using the ExecuteLine method on the document containing the code.
So in your example it would be something like:
Visio.Documents("Foo.vss").ExecuteLine "Foo"
I've used this quite a bit for calling Visio macros from Excel and it's worked quite nicely. ExecuteLine seems like it will execute pretty much anything you can execute from the Immediate Window, so you actually are passing in a line of valid VBA code...
Hope that helps
Never worked with visio before, but a quick browse got me these for excel vba; I'm not sure if it'll work for you, but try it out:
http://www.vbaexpress.com/kb/getarticle.php?kb_id=279
http://www.xtremevbtalk.com/showthread.php?t=139135

Why does the VBA Editor open on its own sometimes?

I created a script in Outlook 2003 VBA that watches for new appointments, and sets them to tentative and no reminder as I create them. I find that seemingly at random, the VBA editor will open itself. It doesn't happen when I use the new script, but it did happen this morning when I un-hibernated my laptop, for example.
The editor doesn't pop up any runtime errors or highlight any lines in the script, it's just there as if I had pressed Alt-F11 to launch it. Sometimes I close other apps and see that it's been sitting back there for a while.
This behavior has only been occurring since I created this new script. Any ideas why this would occur?
As stated in, Microsoft Support Center:
"This problem occurs only if the Microsoft Visual Basic Editor window
is maximized."
Solution:
Un-Maximize Visual Basic Editor and close it.
And you are done!
If you have previously set breakpoints, it may be that you still have a phantom breakpoint. Try choosing "remove all breakpoints" and see if that clears the problem.
Check also that you have not chosen "Break on all errors" under options, and that your error handler is functioning properly.
Believe it or not, this is a known issue. Check out this MS Support link:
http://support.microsoft.com/kb/829059
I have the same problem and found it while I was looking for a solution. Since it gives no advice, I kept searching and came across your post. I will come back with an update if I find a solution.
The MS page linked by Jaquez hints at the answer; just make sure you don't run the editor maximized, and the problem should go away. If you restore it to a non-maximized state, then resize it to take up the whole screen, it should remember its position.
It's a pain if you only have a small screen, but on a normal large monitor it works fine, and solved the problem for me.
I have the same problem, except that it only occurs when I've gone in and unlocked the VBA project for editing and haven't restarted Outlook. It seems to open up when I get a new email (that's when my VBA runs) so basically I just have to restart Outlook and lock the project back up.
Sorry I don't know what's really causing it, but locking the VBA project should fix the problem...
My solution was to search for the .otm file of my VBA project. It was stored under data\microsoft\outlook. I closed Outlook and deleted the .otm file. Restarted Outlook and it was solved.
Just wanted to add that I was running into a phantom breakpoint issue with VBA in excel. Clearing all breakpoints did not work for me, but I resolved it as follows: Copy all content from offending module into a text file. Delete module. Re-create module. Copy back in all text. Save. Phantom breakpoint gone.