Documents.Add results in template being locked - vba

I have a macro that creates a new document based on a template stored on a network share. This macro is stored in each user's Word\STARTUP folder as the file "macros.dotm" and is executed by a button added to a toolbar.
The template file gets locked as soon as the macro code is executed and stays locked so long as the derivative document is still open by another user.
It has no impact on their ability to open new documents based on the macro, but if I want to edit the template, I have to ask them to close Word (and hope nobody else goes into it).
Macro code:
Documents.Add Template:="F:\templates\letterhead.dotm", NewTemplate:=False, DocumentType:=0`

One way around this is detaching the document from the template after it is generated:
Dim doc As Document
Set doc = Documents.Add(Template:="F:\templates\letterhead.dotm", NewTemplate:=False, DocumentType:=0)
Set doc.AttachedTemplate = Nothing
Alternatively, change the filesystem permissions on the template so only you have write access.

The issue is that the template is on a network drive - this is a pretty common issue. One way to get around it is to have your calling template copy over that template locally first and then create a new doc based off of that (and then delete the template when done).

Related

How Do i turn off tracked changes using macros?

I have been trying to create a macro that will automatically run when I open a new word document that will verify that tracked changes are turned off. For some reason, whenever I run a macro that opens a new word document that is a .dotm file that contains a form, it turns on tracked changes and I have not told it to do so.
I have tried various different ways of fixing this issue.
Before I close the User form that has been opened when I opened the new file, I add this line
ActiveDocument.TrackRevisions = False
I also tried with an if statement
if(ActiveDocument.TrackRevisions) then
ActiveDocument.TrackRevisions = False
end if
I also tried doing it so that it was not even involved in the user form at all but the document itself by inserting it in the "ThisDocument" section under the "Microsoft Word Objects" File. Here is what is located there.
Private Sub Document_New()
UserFormInfo.Show
'ActiveDocument.Bookmarks("cursor").Select
ActiveDocument.TrackRevisions = False
ActiveDocument.Save
End Sub
I expect to have this turn off tracked changes, however, this does not occur. Any help is appreciated.
What ended up working for me is I had to remake my template. I had to export my user form so that it was fully removed from all my word files and then remake the template, after that, I had no problem with tracked changes turning on when I ran my macro to open this template. I believe it was a setting I must have had turned on when I had made my template.

Load macros from dotm

The scenario:
When word starts, it connects via vba to a Service, which returns a list of paths. Each of These paths points to a .dotm file. These files contain information about various controls (inside the ribbon, for example a butto which adds a specific footer).
What I want: I need a possibility to load several .dotm files, but without copying or moving them to a specific location.
Basically that's it. I've searched wide and far and I have the fear, that this Approach is actually not possible, and that I have to copy all dotm files inside the startup-folder and let word do the rest
Is there any way how I can load several dotm files into a single word-file, so that the ribbon gets extended depending on the dotm's
Best regards, please and thanks :)
From word documentation:
This example attaches the template "Letter.dot" to the active document.
ActiveDocument.AttachedTemplate = "C:\Templates\Letter.dot"
You can use this to attach all your templates to the document.
See also the Templates collection.
you can add a template to the Templates collection by using the Add method with the Addins collection to load a global template
Sub AddTemplate()
' For this example to work correctly, verify that the
' path is correct and the file exists.
AddIns.Add FileName:="C:\Program Files\Microsoft Office" _
& "\Templates\Letters & Faxes\MyFax.dot", Install:=True
End Sub
It's not possible to load multiple templates to a single Word file. Only one template can be attached to a document.
It is possible to load multiple templates as add-ins in the Word environment. These will be available then to all documents opened in the Word application. Use the Addins-Add method to add a template to the list in Document/Document Template, the "Global templates and add-ins" list. (This is the equivalent of the "Add" button in the dialog box.)
Dim bInstalled as Boolean
Dim Path as String
Dim fileName as String
'Populate the variables, then...
Application.Addins.Add Filename:=Path & fileName, Install:=bInstalled
Set bInstalled to false if you want the template in the list, but not loaded (Ribbon isn't displayed, for example); set it to true to also load it. Once a template is in the list, whether loaded or unloaded, it will generally remain in the list unless Word is reset in some manner. The code for managing template add-ins would be more efficient if it first checks whether an Add-in is already in the list before adding it again.
Templates that are in the list can be loaded/unloaded using the Addins.Installed property. If the tools in a template should be available only to certain documents then they can be loaded/unloaded dynamically by using events, such as DocumentChange.
The code to load the add-ins and manage them (events) should probably be in a central template in Word's Startup folder or in the template attached directly to the document.

Add a Document_Open vba script to document when created from a template?

I have a template, Template.dotm, which is taken by a server and populated with data, then saved to a .doc. A user is then passed a link to this document so they can download it. I want to run a VBA script on that document when the user opens it.
Is there a way to accomplish this from within the template's vba script? The script of course runs fine if I simply double click the template to open an instance of it, but since the server saves a copy first, the script is gone by the time the user sees the document.
It turns out since the file was stored locally to the template but was being opened over the network, Word was confused and severed the connection to the template. I now have a copy of the template in a shared network drive, which I set to the attached template in Document_New.
Not the nicest solution, but it's working and was easy to implement. Thought I'd post for anyone who finds themselves in a similar situation.

Exclude template reference and macros from document

I am creating a template which has some macros inside. What the macros do is to open a user form which will prompt the user for some information which will then go into the new document.
The problem is that once the new document is created, it still somehow have a reference to this template and the macro code.
I have to send the document to people outside of the organization and they get a warning that the document contains macros.
Is there a way to avoid this? I want the document to be created without any of the macros or referrence to the original template.
Try creating the new document with "Normal" as a parameter for the Documents.Add method. The default seems to be to create a document off of the current open template (or Normal if none).

Using Word automation, is it possible to find out if the active document contains VBA code?

Using Word Automation, I want to save the active document programmatically. In Office 2007/2010, the document needs to be saved explicitly as "macro enabled" to preserve any VBA code in that document.
Rather than asking the user to choose, I would like my application to be able to determine if there is VBA code in the active document. Is that possible?
Yes this can be determined via the HasVBProject property. For example:
If ActiveDocument.HasVBProject = True Then
'Code to save as .dotm
Else
'Code to save as .dotx
End If
For those who might stumble upon this post later, it is worth noting that this code should be placed outside the document being tested for the presence of macros (otherwise it would detect itself). Two often-used options would be to access the code from an external application or from a template stored in Word's Startup folder.