PDA

View Full Version : [SOLVED:] Create Document Based on Template and Make it the Active Document



clhare
01-08-2014, 07:30 AM
I have a macro that will create a document based on a template. The only problem is, when the macro is done, the new document is not the active document. The blank document (or whatever document I was in when I ran the template) is the active document.


Documents.Add Template:= _
Application.StartupPath & "\Templates\My Template" _
, NewTemplate:=False, DocumentType:=0

How do I get the new document to be the active document?

Thanks!

Cheryl

gmaxey
01-08-2014, 10:30 AM
Dim oDoc as Word.Document
Set oDoc = Documents.Add(Template:= _
Application.StartupPath & "\Templates\My Template" _
, NewTemplate:=False, DocumentType:=0)
oDoc.Activate

clhare
01-08-2014, 01:45 PM
First it didn't work, then it did, now it doesn't again. I don't know what is wrong. Doesn't make any sense...

Cheryl

fumei
01-08-2014, 04:10 PM
That indeed does not make any sense. Document.Add should make the new document ActiveDocument, unless there is further code that changes that. Greg's code makes it explicit...the new document is ActiveDocument. It is is not then SOMETHING is changing it.

clhare
01-09-2014, 06:08 AM
This is the only code in the procedure:


Sub MyTemplate()

Dim oDoc As Word.Document

' Create a new doc based on selected template
Set oDoc = Documents.Add(Template:= _
Application.StartupPath & "\Templates\My Template" _
, NewTemplate:=False, DocumentType:=0)

' Active the new document
oDoc.Activate

End Sub

I just opened Word and created a blank document, then I ran this macro. It created the new document based on my template, but the blank document I started with is the active document. I had someone else try it and the same thing happened to her. We are both trying this macro in Windows 7/Word 2010. I don't know if that makes any difference to the code?

Thanks!

Cheryl

Frosty
01-14-2014, 07:51 PM
Try running that macro in a "clean" version of Word with no other addins... you do this by doing Start > Run > winword /a

That gives you a microsoft word process without any other addins. Then create that macro in that session of Word.

Odds are, you've got some other macro which is doing some action on an event of AutoOpen or AutoNew or DocumentNew or DocumentChange, etc etc... if you don't have access to the other code running, you need to figure out which addin is doing it.

clhare
01-17-2014, 08:14 AM
I ran the macro in a "clean" version of Word and it worked fine.

This macro is actually one of quite a number of macros that are stored in a template that is in the Startup folder so they are always available (they are on a customized ribbon). I have a couple of Startup templates. None of them use AutoOpen, AutoNew, DocumentNew, DocumentChange, etc.

I have no other add-ins that I can find. I am at a loss as to how to proceed. Is there any other way to make the new document active?

Cheryl

Frosty
01-17-2014, 11:23 AM
You need to find out which addin is causing the undesirable behavior. It might not be your code-- it could also be a COM addin. Try disabling addins one at a time and re-running the code until you identify the "other code" (even if you can't see it).

The developer tab will help you uncheck both com addins and word global addins one by one without having to restart word. That process of elimination will help you focus the search. But the code displayed above is correct. You just have to find the conflict.

Frosty
01-17-2014, 11:25 AM
Also... You may not be looking at a class module within your own code-- not just the ThisDocument module, but an application event handler-- eliminate the possibilities one-by-one-- and if it's one of your templates, then you'll be able to find the actual code. If it isn't, then you'll have to talk to that developer/vendor

fumei
01-17-2014, 01:02 PM
If I may add, this may be a opportunity to rethink putting a number of template or addins in Startup. Personally I do not like the idea (unless it is actually required), of putting template addins in Startup. I think people mostly put them there because of habits, or not knowing they do not need to. Addins can be added dynamically from anywhere. Better yet, loading them NOT from Startup means you can - unlike those in Startup! - unload them whenever you want. The ones in Startup can not be unloaded. They remain for the entire session.

Bottom line though is Frosty is correct. The code is fine, but something, somewhere, is interfering.

Frosty
01-17-2014, 01:53 PM
Actually, you can manually uncheck word addins loaded from the startup during a session, they will simply get loaded the next time you start word. You can't click the remove button in templates and addins, but you can uncheck them and they will unload for that session.

i believe this has been true for many versions of word, but my memory could be failing me

clhare
01-29-2014, 06:03 AM
I figured it out... by accident!

First, my template isn't in the actual "startup" folder, it is in another folder we have designated as "startup" as not all users have the exact same setup. And, the macro that creates a new document from a template is called by a userform where the user can select from several different templates.

Anyway, I couldn't figure out what was causing this issue, so had decided to just add a message box that says "if the new doc isn't showing, you will need to switch to it..." When I was testing the update to see how it would look, I noticed the userform was sitting behind the message box (in the original document). I didn't like how that looked, so I added code to unload the userform and.... it now works! The unloaded userform was the problem.

I always unload my userforms, not sure how this one slipped through without being unloaded. Anyway, I am glad to have figured it out. You guys were right... something else was going on!

Thanks for your help!