1
votes

I have a word document which I am using as a template for other projects. The file is saved as an .docx and includes a Macro to save the document with a file name dependent on content control box titled "STATE". I would prefer the macro save the document without the macro included. When I run the macro, the document saves to the correct file location however when opened I get an error that reads:

We're Sorry. We can't open FILE LOCATION because we found a problem with its contents. No error details available.

I assume there is an error somewhere in my code. I am using Word 2013. Any help would be appreciated!

Sub Silent_save_to_DOC()

Dim strText As String, strDate As String, strDrop As String

strText = ActiveDocument.SelectContentControlsByTitle("STATE")(1).Range.Text

Dim strFilename As String
strFilename = "C:\Users\Tom\Desktop\Quote - " & strText & ".docx"
ThisDocument.SaveAs strFilename

End Sub
2

2 Answers

3
votes

The problem is that a *.docx file may not contain macros, only *.docm files may contain macros. Your code is forcing Word to save the document with macros and an incompatible file extension. As a security measure Word will not open a *.docx file that contains macros - as the message is informing you.

Use a true template file - *.dotm - to create your documents. The template can contain the macros, which will not be copied to the document.

If you want to unlink the template (with the macros) from the document you can attach the Normal template to the document (ActiveDocument.AttachedTemplate = NormalTemplate).

When you use SaveAs be sure to also specify the file format using the FileFormat parameter, not just the file name. For a docx file that would be ^wdFormatXMLDocument`.

Do not use ThisDocument.SaveAs as ThisDocument refers to the document containing the macro. Instead, use ActiveDocument if you cannot otherwise use a specific Document object.

0
votes

It's also helpful if you specify the file format should there be any possibility that the save format isn't the same as that of the file that was opened. Hence:

Dim strText As String, strFilename As String
With ActiveDocument
  strText = .SelectContentControlsByTitle("STATE")(1).Range.Text
  strFilename = "C:\Users\Tom\Desktop\Quote - " & strText & ".docx"
  .SaveAs2 FileName:=strFilename, FileFormat:=wdFormatXMLDocument
End With