Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
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,144
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 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
  #2  
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
  #3  
Old 12-07-2016, 02:25 PM
macropod's Avatar
macropod macropod is online now 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: 22,514
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
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 02:56 PM.


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