PDA

View Full Version : [SOLVED:] Convert all files in directory to .docm (Word 2013)



davis1118
11-28-2017, 07:49 AM
I need to place a macro into a lot of word documents that are placed in multiple different directories. All the current documents are either .doc or .docx, so I need to convert them to .docm first. I'm trying to create a master document that I can move from directory to directory and run a macro that would convert all word documetns in that directory to .docm. I have found a few examples of looping through directories as well as saving a document to .docm. But I haven't found both examples put together.
I have placed the master convert file into a test folder along with 2 .doc test files. I must be missing something because when I run the macro from the master convert file nothing happens. I don't even get any errors from the macro. Here is what I have so far.


Option Explicit
Sub Convert2Macro()
Dim vDirectory As String
Dim vFile As String
Dim oDoc As Document
vDirectory = ActiveDocument.Path
vFile = Dir(vDirectory & "*.doc*")
Do While vFile <> ""
Set oDoc = Application.Documents.Open(FileName:=vDirectory & vFile)
ActiveDocument.SaveAs2 FileName:="\\FILE\ (file://\\FILE\)" & oDoc.Name & ".docm", _
FileFormat:=wdFormatXMLDocumentMacroEnabled, _
LockComments:=False, _
Password:="", _
AddToRecentFiles:=True, _
WritePassword:="", _
ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, _
SaveFormsData:=False, _
SaveAsAOCELetter:=False, _
Encoding:=1252, _
InsertLineBreaks:=True, _
AllowSubstitutions:=False, _
LineEnding:=wdCRLF, _
CompatibilityMode:=0
oDoc.Close SaveChanges:=False
vFile = Dir
Loop
End Sub


Thank you for the help! - David

davis1118
11-28-2017, 12:40 PM
I figured out that I needed to add an "\" after the activedocument.path to make the macro work. But now it saves the documents with .docm as part of the filename, and it also makes a copy of the master file. I'm not sure what else to change to fix those two problems. Here is what the code looks like now.


Option Explicit
Sub Convert2Macro()
Dim vDirectory As String
Dim vFile As String
Dim oDoc As Document

vDirectory = ActiveDocument.Path & "\"
vFile = Dir(vDirectory & "*.doc*")

Do While vFile <> ""
Set oDoc = Application.Documents.Open(FileName:=vDirectory & vFile)
ActiveDocument.SaveAs2 FileName:=vDirectory & oDoc.name & ".docm", _
FileFormat:=wdFormatXMLDocumentMacroEnabled, _
LockComments:=False, _
Password:="", _
AddToRecentFiles:=True, _
WritePassword:="", _
ReadOnlyRecommended:=False, _
EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, _
SaveFormsData:=False, _
SaveAsAOCELetter:=False, _
Encoding:=1252, _
InsertLineBreaks:=True, _
AllowSubstitutions:=False, _
LineEnding:=wdCRLF, _
CompatibilityMode:=0
oDoc.Close SaveChanges:=False
vFile = Dir
Loop
End Sub



Thanks again! - David

macropod
11-28-2017, 01:30 PM
What is the point of converting *all* documents to docm?

The docm format is only for documents containing macros. The default save format for Word 2007 & later is docx (i.e. any new files you create won't automatically save in the docm format, either), but files created in previous Word versions don't need to be converted to that unless you're still working on them and they need to have features added that are only supported in the docx/docm format. Furthermore, converting doc files to the docx/docm format is liable to result in changes to page layouts, which could compromise past efforts to format them in a particular way.

davis1118
11-28-2017, 02:06 PM
Sorry, I guess I should have explained in further detail. I need to have only certain word documents converted on the company server to .docm not all. But the word files that need to be converted are in multiple file locations on the main server. So I wanted to make a conversion macro document that I can move from directory to directory where the files that need to be converted are located. The converted documents will then have a macro placed in them that will run at save to create a back-up and PDF copy.
I thought about creating a template and making it an add-in. But I want the macro to run when the save button is clicked so it is error proof. The add-in would run on every word document, which doesn't need to happen. I don't know very much about the add-in options so maybe there is a way to limit which files the add-ins run on?

Thank you-David

macropod
11-28-2017, 02:16 PM
There is still no need to convert the files to docm; all you need do is add the relevant code to their template. In fact, doing so is safer and more reliable than adding macros to each document because:
a) programmatically adding macros to each document requires giving trusted access to the VBA project model, which is a security risk; and
b) macros in documents typically require users to allow them to run (every time the document is opened), whereas macros in their templates don't ordinarily require that. Your whole scheme will fall over if the user doesn't allow the macro to run.
Clearly, if you attach only the documents requiring this action to a template with the relevant code, then only those documents will be affected; other documents will not.

davis1118
11-28-2017, 03:33 PM
I definitely like your idea. I gave it a shot and ran into a road block. My company has the security settings set so that a template or add-in will always require the macro to be enabled even after trusting it. I created a trusted location to place the template but it's only trusted for me, not the other users on the network. I might have to spend more time looking into getting the template idea that you suggested to work, but my company seems to have most of the security working against me for that option.
Seems like I might end up having to go back to putting a macro on each document that calls up the master document. It would act like the template without being a template using the code below. You would have to enable the macro for the first use only. I know the engineers that would be using the documents, so I could always make sure that they enable the macros for the first use. What do you think of using this code to link to the master document?


Application.Documents.Add("C:\Test\MyDocument.doc")
Application.Run "UniqueProcedureName"

Thank you for the input.

macropod
11-28-2017, 03:42 PM
Regardless of whether you add the code to a template or to each document, you're presumably going to run into the same 'roadblock'. By far the easiest solution is to liaise with your IT people so you can store the template in a folder that is trusted for all users.

davis1118
11-29-2017, 07:20 AM
There's nothing more worrying than asking IT for more privileges , lol. Thanks for the help Paul, much appreciated! This is a new option that I didn't realize I had.