Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-07-2016, 03:50 AM
BJH BJH is offline Transfer of embedded macros after template is opened, Windows 7 64bit Transfer of embedded macros after template is opened, Office 2010 64bit
Novice
Transfer of embedded macros after template is opened,
 
Join Date: Dec 2016
Location: Swansea, UK
Posts: 4
BJH is on a distinguished road
Default Transfer of embedded macros after template is opened,

Dear Word experts.



I have a Word document template (.dotm) which I have set up with a standard layout and numbered outline format, a bunch of associated styles, and some helper macros which I have written. The helper macros are embedded into the template file itself.

When I open the template file, Word starts a new document based on the template, so all my standard layout and styles etc are in the new document. As you would expect.

Unfortunately the embedded macros are not present in the new document. They still work for me, because word can "see" them in the template document which exists on my computer. But if I send my new document to someone else, my macro functions are not available for them.

Is there an easy solution to this problem?

Many thanks!

Brian.

Last edited by BJH; 12-07-2016 at 09:52 AM. Reason: Corrected .docm to .dotm. Thanks macropod.
Reply With Quote
  #2  
Old 12-07-2016, 05:44 AM
macropod's Avatar
macropod macropod is offline Transfer of embedded macros after template is opened, Windows 7 64bit Transfer of embedded macros after template is opened, Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

If it's a true template, it would have a dotm extension, not docm. That said, what you're describing is the normal behaviour for a document created from a template. If you want the macros to travel with the document, you'll have to add them to the document - and save it in the docm format. Although this could be done programmatically, it would require giving 'trusted access' to the VBA project environment, which is a significant security risk for you. Otherwise, you could send both the document and the template - which the recipient would need to copy to their own templates folder (not a simple task for most users) so the macros would once again be available to the document.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 12-07-2016, 05:53 AM
Charles Kenyon Charles Kenyon is offline Transfer of embedded macros after template is opened, Windows 8 Transfer of embedded macros after template is opened, Office 2013
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,125
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Otherwise, you could save your .dotm template as a .docm macro-enabled document. This would mean that when you want to use it, you would want to immediately save it with a new name, then make your changes, save it again, and send it to someone else.

This assumes that the recipient is able to run the macros when they receive it. For a knowledgeable user, this will not be a problem. However, for many users, it would be.

If you use this method, I would advise keeping a backup copy of your original. It is all too easy to end up saving changes particular to one job to your original.

Another method would involve using the vba editor (Alt+F11) exporting the module(s) holding your macros (probably NewMacros), and then importing them into the new document and saving it as a .docm file. Again, the recipient would need to be able to use the macros.
Reply With Quote
  #4  
Old 12-07-2016, 08:56 AM
gmayor's Avatar
gmayor gmayor is offline Transfer of embedded macros after template is opened, Windows 10 Transfer of embedded macros after template is opened, Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You can use following macro in the template to write the macros the document requires, to the document, which must then be saved as a macro enabled document in order for those macros to work in the document.

The function copies the code from a text file, stored locally, to the document that requires the macros. This is essentially the process used in http://www.gmayor.com/interactive_multichoice.html, though that uses a password protected document to hold the code rather than a text file - the text file is quicker.

In the example below, the macro code clears the named module of the document, then writes the text from the text file to it.

Code:
Public Function AddVBACodeFromTXT(oDoc As Document, strSource As String, strModule As String)
Dim strLines As String
Dim i As Long, j As Long
Dim strCode As String
Dim iFile As Integer: iFile = FreeFile

Open strSource For Input As #iFile
strCode = Input(LOF(iFile), iFile)
Close #iFile

    i = oDoc.VBProject.VBComponents(strModule).CodeModule.CountOfLines
    oDoc.VBProject.VBComponents(strModule).CodeModule.DeleteLines 1, i
    oDoc.VBProject.VBComponents(strModule).CodeModule.AddFromString strCode
lbl_Exit:
    Exit Function
End Function

Sub Example()
    AddVBACodeFromTXT ActiveDocument, "E:\Path\Forum\Test\Code.txt", "ThisDocument"
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 12-07-2016, 09:48 AM
BJH BJH is offline Transfer of embedded macros after template is opened, Windows 7 64bit Transfer of embedded macros after template is opened, Office 2010 64bit
Novice
Transfer of embedded macros after template is opened,
 
Join Date: Dec 2016
Location: Swansea, UK
Posts: 4
BJH is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
If it's a true template, it would have a dotm extension, not docm.
Thanks for replying to my question. Yes you're right, it's a .dotm file, I made a mistake in my post.

Quote:
Originally Posted by macropod View Post
If you want the macros to travel with the document, you'll have to add them to the document - and save it in the docm format.
Ah ok. That's a shame, as I lose the advantages of the template.

Quote:
Originally Posted by macropod View Post
Although this could be done programmatically, it would require giving 'trusted access' to the VBA project environment, which is a significant security risk for you.
I'm not sure I've understood this part of your reply. The functionality I was hoping for is that the template, which contains the helper macros, would transfer these macros to the new document when it is opened. Whether those macros actually run or not would still be dependent upon the macro security settings of the user installation, of course.


Quote:
Originally Posted by macropod View Post
Otherwise, you could send both the document and the template - which the recipient would need to copy to their own templates folder (not a simple task for most users) so the macros would once again be available to the document.
Ok thanks. I understand from your reply that the functionality I was hoping for does not exist, and in order to replicate the behaviour I have on my installation I'd have to give instructions for users to copy the .dotm to their templates folder.
Otherwise, I'd have to distribute a .docm, and then the macros would be available, but in that instance it is likely that someone will eventually overwrite the document (which is why a template is useful in the first place).

I guess another option is to use a .docm file and version control it. But getting the users to understand version control... hmmm.

Many thanks again for your response.
Reply With Quote
  #6  
Old 12-07-2016, 09:52 AM
BJH BJH is offline Transfer of embedded macros after template is opened, Windows 7 64bit Transfer of embedded macros after template is opened, Office 2010 64bit
Novice
Transfer of embedded macros after template is opened,
 
Join Date: Dec 2016
Location: Swansea, UK
Posts: 4
BJH is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
You can use following macro in the template to write the macros the document requires, to the document, which must then be saved as a macro enabled document in order for those macros to work in the document.
Ah, interesting. Yes this sounds like it could be the solution I require. It sounds like this is what macropod alluded to in his response. Of course, if the user has macros disabled due to security settings this wouldn't work, but that's the same problem as with any macro enabled document anyway.

Thanks very much I'll give that a try.

Thanks also to Charles Kenyon for his response - you mention some concerns that I share (user will overwrite the .docm).
Reply With Quote
  #7  
Old 12-07-2016, 02:25 PM
macropod's Avatar
macropod macropod is offline Transfer of embedded macros after template is opened, Windows 7 64bit Transfer of embedded macros after template is opened, Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,962
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by BJH View Post
Ah, interesting. Yes this sounds like it could be the solution I require. It sounds like this is what macropod alluded to in his response.
It's similar. And, as I said, using it requires giving trusted access to the VBA project, which is a significant security risk.

Minimising that risk would require you to manually allow access, run the macro, then manually disallow access every time you want to create such a document; you can't automate the allow/disallow process; it has to be done manually.

Given those requirements, you may as well just copy the code across manually - which would require less work and no security exposure when you forget to disallow access after using the other approach. Or you could adopt the macro-enabled document approach Charles suggested.

Whichever approach you take, if the recipient is on a different system, they will have to allow macros to run. If they're on the same system, you could store the template in a trusted location the documents created from it can always access. That way, you won't have to add the code to every document, send the template or, generally, need users to enable macros.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old 12-07-2016, 10:03 PM
gmayor's Avatar
gmayor gmayor is offline Transfer of embedded macros after template is opened, Windows 10 Transfer of embedded macros after template is opened, Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Surely any template that uses macros has a potential security risk? Templates are usually stored (and in Word 2016 at least) saved by default in the user templates folder (or used as add-ins in the Word startup folder). These folders will by default be trusted folders, so the macro stored in the template will run from those locations and present no greater potential risk than any other macro that may be in the document created from the template.

Some documents require macros to perform specific tasks, and if there is no connection back to the template (as would be the case if the document was mailed or otherwise passed to someone for completion) to run the macros from the template, then the macros need to be in the document.

Clearly the user would require that macro enabled document to be trusted in order to run it, and that presents no more security issues whether the code is copied manually or by using a macro to copy it, but it is certainly more convenient to use macros to copy then save the document as macro enabled, than to do so manually.

Macros in documents and templates (especially when prepared by others) do indeed present a potential danger and some end users may be unwilling to run them. That is universally true of all macros, but it remains a fact that some processes cannot be completed without macros.

This of course requires users to take a leap of faith, depending on who has supplied the document, to enable the macros in order to run the process, and this remains true whether you supply just the document or make available the template to provide the macro functions.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #9  
Old 12-08-2016, 03:07 AM
BJH BJH is offline Transfer of embedded macros after template is opened, Windows 7 64bit Transfer of embedded macros after template is opened, Office 2010 64bit
Novice
Transfer of embedded macros after template is opened,
 
Join Date: Dec 2016
Location: Swansea, UK
Posts: 4
BJH is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
This of course requires users to take a leap of faith, depending on who has supplied the document
Agreed. I was hoping there would be some way to sign the document, with a pgp key or cerificate of some description, and that our IT department would be able to set up a list of trusted developers which MSOffice would refer to when deciding whether to enable macros or not.

If that kind of solution were possible, most users could have macros disabled by default, except when a document with a good signature from a trusted source is opened.

From my experience so far, there isn't really a good solution for this.

Anyway, a bit off-topic.

Thanks both for your responses.
Reply With Quote
  #10  
Old 12-08-2016, 05:11 AM
Charles Kenyon Charles Kenyon is offline Transfer of embedded macros after template is opened, Windows 8 Transfer of embedded macros after template is opened, Office 2013
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,125
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Trust settings can be set to allow trusted sources with a certificate.
Reply With Quote
Reply

Tags
macro transfer



Similar Threads
Thread Thread Starter Forum Replies Last Post
Transfer of embedded macros after template is opened, Newsletter created with template on Mac has many mistakes when opened on PC neco23 Word 1 08-27-2015 09:53 PM
Transfer of embedded macros after template is opened, When creating new template from established template macros getting lost. TechEd Word 3 06-09-2014 07:22 PM
Transfer of embedded macros after template is opened, Word Template is not recognised when opened by double click. Creates new document. stevewak Word 8 03-19-2014 05:12 AM
Creating document from template but macros are still linked to template webharvest Word 0 07-16-2011 09:34 AM
Transfer of embedded macros after template is opened, Macros not working when template is emailed EMH Excel Programming 1 07-06-2011 09:24 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:19 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft