PDA

View Full Version : Docm file saving as a new docm file name without prompting



guyver3
04-30-2016, 04:53 PM
Hello

I am wondering if you guys can help me.

I would like to be able to save as, my docm file to a different named docm file using a button, and without prompting the save as box. I would put in the file name I want in the code. I have seen an example of one for PDFs, but can not find one for docm files, so I am not sure what code to use. Any ideas, would be great.

Thanks.

gmayor
04-30-2016, 08:56 PM
You have to specify the format

ActiveDocument.SaveAs2 _
Filename:="C:\path\filename.docm", _
Format:=wdFormatXMLDocumentMacroEnabled
Note that this will overwrite any file of the same name without prompting.

guyver3
05-01-2016, 05:32 AM
Thanks gmayor, that is great!

There is one more thing, how would I have it save simply where the document already is? Thanks.

gmayor
05-01-2016, 05:57 AM
Dim strPath As String
strPath = ActiveDocument.Path
ActiveDocument.SaveAs2 _
Filename:=strPath & "\filename.docm", _
Format:=wdFormatXMLDocumentMacroEnabled

guyver3
05-01-2016, 07:08 AM
Thanks, this works and puts it in the active directory.

But there is one thing with the "\filename.docm". I would like to name it using text from below instead of entering it manually, e.g.
NewFileName = ActiveDocument.Surname.Text & " " & Firstname.Text & " Contract",

I tried it as:
ActiveDocument.SaveAs2 FileName:=strPath & NewFileName, _

It names it with the above information, but with the active folder name at the start followed by the surname,
firstname and contract, and puts it on the desktop, not in the actual folder.

If you have any ideas how to amend it, that would be cool. Thanks.

gmaxey
05-01-2016, 07:10 AM
I think Graham meant FileFormat not Format

To address his note, you could:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim strPath As String
strPath = ActiveDocument.Path
If Not CreateObject("scripting.filesystemobject").FileExists(strPath & "\filename.docm") Then
ActiveDocument.SaveAs2 FileName:=strPath & "\filename.docm", FileFormat:=wdFormatXMLDocumentMacroEnabled
Else
If MsgBox("The file named already exists. Do you want to overwrite and replace it?", vbInformation + vbYesNo, "OVERWRITE") = vbYes Then
ActiveDocument.SaveAs2 FileName:=strPath & "\filename.docm", FileFormat:=wdFormatXMLDocumentMacroEnabled
End If
End If
lbl_Exit:
Exit Sub
End Sub

guyver3
05-01-2016, 08:17 AM
Sorry gmaxey, I don't think that is it. I think it just comes down to changing the filename part in quotes to somehow to write the file as what is contained from this:
NewFileName = ActiveDocument.Surname.Text & " " & Firstname.Text & " Contract",

So from the word document, it takes say Smith David, and adds the word Contract. So Smith David Contract.

The NewfileName gets the data fine, it's just a matter of somehow inserting it once the StrPath being the
current directory has been collected, so after the strPatch, there must be a way of adding it
refering to NewFileName below?

Filename:=strPath & "\filename.docm", _

Thanks.

guyver3
05-01-2016, 09:05 AM
Hello Again

It's OK, I have solved it, I used it with:
ActiveDocument.SaveAs FileName:=ActiveDocument.Path & "\" & NewFileName, _

Thanks for your help anyway.

gmaxey
05-01-2016, 10:25 AM
guyver3,

I'm glad you have your issue resolved. However, that fact remains that a line in Graham's last post:

Filename:=strPath & "\filename.docm", _
Format:=wdFormatXMLDocumentMacroEnabled

... doesn't compile (at least not here) so it couldn't work and he likely made a typo. The named parameter is FileFormat not Format.

Our posts must have crossed because I did not see your 10:08 post before sending my 10:10.

16064

gmayor
05-01-2016, 09:22 PM
Greg is correct. 'Format' should have been 'FileFormat', but the issue with the filename, as you appear to have discovered, revolves around the fact that the strPath statement

strPath = ActiveDocument.Pathdoes not include the path separator (hence I added it to the fixed filename). If you are going to derive a filename, you need to include the path separator. You can do so as you have done in the SaveAs2 command or you could add it to strPath e.g.
strPath = ActiveDocument.Path & chr(92).

When naming the document from text in the document, there is a propensity to introduce illegal filename characters which will cause the process to crash, and the problem of handling existing filenames. Both can be addressed with functions you will find on my web site at http://www.gmayor.com/useful_vba_functions.htm

gmaxey
05-02-2016, 04:18 AM
Graham, when I think to do it, I will use Application.PathSeparator. Probably doesn't matter in many cases, but I suppose there could be places where the separator isn't what I think it is