Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-14-2014, 11:36 AM
aznative aznative is offline Programatically change macros for existing templates Windows 7 64bit Programatically change macros for existing templates Office 2010 32bit
Novice
Programatically change macros for existing templates
 
Join Date: Jul 2014
Posts: 1
aznative is on a distinguished road
Angry Programatically change macros for existing templates

Is it possible? How do I...?
Trying to determine a (best) way to **programatically** change out the macros for templates scattered across many sites. The requirements have changed and the macros need to be upgraded and enhanced. Macros include AutoNew, AutoExit, etc. in addition to several AddOn menu event actions. Need to retain the other template objects as is (AutoText, Styles, menu AddOns, etc.) but replace the macros. Each site can have any number of templates, and the templates are all unique; so cannot just switch out the templates in mass. Would be nice to just strip off the existing macro module (using VBA) from each template and replace it, then resave the template. But have not found any way to accomplish that.



Any suggestions as to how best to accomplish this?
Reply With Quote
  #2  
Old 07-14-2014, 09:51 PM
macropod's Avatar
macropod macropod is offline Programatically change macros for existing templates Windows 7 32bit Programatically change macros for existing templates Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 20,488
macropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant futuremacropod has a brilliant future
Default

The following macro updates a line of VBA code in all code modules in all templates in the selected folder. The actual editing is done by the ‘EditCode’ module. The rest of the code is for the locating and looping through the files. The StrFnd and StrRep variables can contain from as little as a single letter to a whole code module.

Simply add the code to a document, then run it & point its browser to the folder containing the templates to be updated.
Code:
Sub UpdateTemplates()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.dotm", vbNormal)
While strFile <> ""
  Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
  Call EditCode(wdDoc)
  wdDoc.Close SaveChanges:=True
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
 
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
 
Sub EditCode(wdDoc As Document)
Dim VBC, VBComp
Dim i As Long, j As Long, bFnd As Boolean
Dim StrFnd As String, StrRep As String, StrNew As String
StrFnd = "Old code line1" & vbCr & "Old code line2" 'Text to Find
StrRep = "New code line1" & vbCr & "New code line2" ' Replacement Text
Set VBC = wdDoc.VBProject.VBComponents
bFnd = True
For Each VBComp In VBC
  With VBComp.CodeModule
    i = 1
    j = .CountOfLines
    bFnd = .Find(Target:=StrFnd, StartLine:=i, StartColumn:=1, EndLine:=j, _
      EndColumn:=255, WholeWord:=True, MatchCase:=False, PatternSearch:=False)
    Do Until bFnd = False
      StrNew = Replace(.Lines(i, 1), StrFnd, StrRep)
      .ReplaceLine i, StrNew
      j = .CountOfLines
      bFnd = .Find(Target:=StrFnd, StartLine:=i, StartColumn:=1, EndLine:=j, _
        EndColumn:=255, WholeWord:=True, MatchCase:=False, PatternSearch:=False)
    Loop
  End With
Next VBComp
Set VBC = Nothing
End Sub
Note: You will need to allow access to the VBA project object model to use the code (File|Options|Trust Centre|Trust Centre Settings|Macro Settings)
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Tags
macros, replace, template

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Updating active R/O templates (dot) and macros (dotm) gw1500se Word 0 05-27-2014 12:11 PM
Office 2010 change location workgroup templates appear in from My Templates folder nickc Word 0 11-20-2013 03:38 AM
Can you make templates dynamically update macros/etc. from Normal.DOT? New Daddy Word 2 11-18-2013 09:07 AM
Assigning templates to existing documents. Update Styles Enmasse. bannerdog Word 1 02-28-2012 03:53 PM
Templates and Macros eliz.bell Excel 2 03-28-2011 11:44 AM

Other Forums: Access Forums - Senior Forums

All times are GMT -7. The time now is 12:45 PM.


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