PDA

View Full Version : Resave template file as a document



clhare
03-13-2009, 12:36 PM
I found a macro on this site that removes all VBA from the active document. How would I alter this code in order to resave the updated file (to the same folder) as a .doc file (so I don't overwrite the original .dot template file?

Here's the code I'm using the remove VBA from the active document file:

Sub RemoveVBA()
' Declare variables
Dim vbComp As Object

' From the active documentWith ActiveDocument
' Remove each VBA component in the active document
For Each vbComp In ActiveDocument.VBProject.VBComponents
With vbComp
If .Type = 100 Then
.CodeModule.DeleteLines 1, .CodeModule.CountOfLines
Else
ActiveDocument.VBProject.VBComponents.Remove vbComp
End If
End With
Next vbComp
End With

End Sub

lucas
03-13-2009, 01:54 PM
Hi Cheryl, the code in the template file should not be included in any files you clone from the template.

fumei
03-16-2009, 09:19 AM
I am confused.

"How would I alter this code in order to resave the updated file (to the same folder) as a .doc file (so I don't overwrite the original .dot template file?"

Are you talking about having the .DOT file open?

Steve is quite correct. Any procedures in the .DOT files are NOT going to be in any .DOC files cloned from that template.

fumei
03-16-2009, 09:20 AM
And you should not be saving a .DOT file as a .DOC file anyway. Why would you do that?

clhare
03-16-2009, 11:08 AM
We are periodically asked for a copy of all templates we have created for a specific client so they can review the document text. Since we only allow users "read only" access to the templates (so they won't change anything on us), we don't want to give them a copy that contains the macros. So we manually remove the macros, then resave the files as Word documents that we can send instead.

This isn't really a big deal unless there are alot of templates involved. I had to do this for 87 templates Friday, and it was really annoying.

I've been playing around with this all morning. What I ended up with is two separate macros: 1) opens the files in a selected folder and removes the VBA from each of them, and 2) resaves the files as .doc files and closes each of them. When I run these two macros, it works fine. But when I had them combined into one macro, for some reason I ended up with one of my userforms still in the .doc files. It's only removing all the VBA if I do the 2-step process.

Here's the two macros:
Sub Step1_OpenFilesAndRemoveVBA()

' Declare variables
Dim path As String
Dim strFile As String
Dim getFile
Dim vbComp As Object

' Assign or get values for variables
path = InputBox("Enter path to open.")
path = path & "\"

' Indicate where to get the files
getFile = Dir(path & "\" & "*.dot")

' Open file as long as there's another file in the folder to be opened
Do While getFile <> ""
strFile = path & getFile
Documents.Open FileName:=strFile
' Remove each VBA component in the active document
For Each vbComp In ActiveDocument.VBProject.VBComponents
With vbComp
If .Type = 100 Then
.CodeModule.DeleteLines 1, .CodeModule.CountOfLines
Else
ActiveDocument.VBProject.VBComponents.Remove vbComp
End If
End With
Next vbComp
' Open next file
getFile = Dir
Loop

End Sub
Sub Step2_SaveFilesAsDoc()

Dim i As Integer
Dim strOldFilename As String
Dim strNewFilename As String
Dim strNewPath As String

strNewPath = "c:\temp\"

' If folder doesn't already exist, create it
If Len(Dir("c:\temp\", vbDirectory)) = 0 Then
MkDir "c:\temp\"
End If

' Resave each open template as a document, then close it
For i = 1 To Documents.Count
With ActiveDocument
' Reassign filename and remove file extension
strOldFilename = ActiveDocument.Name
' Take template filename and remove last 4 characters (which would be .dot)
strNewFilename = Left(strOldFilename, Len(strOldFilename) - 4)

' Resave file as a document
.SaveAs FileName:=strNewPath & strNewFilename, FileFormat:=wdFormatDocument, _
LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _
False

' Close file
.Close
' Update next active document
End With
Next i

End Sub

Any ideas on why I end up with one of the userforms still in the document if I combine the above into a single macro?

fumei
03-16-2009, 11:12 AM
Still confused. That does not make any sense to me.

You are removing the code and passing on the text content.

" So we manually remove the macros, then resave the files as Word documents that we can send instead."

I will say it again. Documents cloned from a template do NOT have the code in them.

So....clone a document...and send it to them.




Done.

Am I missing something???

"We are periodically asked for a copy of all templates we have created for a specific client so they can review the document text. "

They get precisely that by sending them a document made from the template.

clhare
03-16-2009, 11:30 AM
I can't "run" the template in order to get the document copy as that would fire the macros which may remove text from the document. So, I am "opening" the template, removing the macros, then "resaving" the template as a document. That way nothing in the text gets changed at all.

clhare
03-16-2009, 11:34 AM
I guess I could also "run" the template but disable the macros, then save the output file, but I'd still have to do it one template at a time. I'm trying to come up with a quicker way to do it for multiple templates.