PDA

View Full Version : VB6 macro code for Word to save a document in a specified folder as a Word file



RajanSyd
12-13-2017, 10:06 PM
I'm trying to save a document from a software program after running a macro in a specified folder as a Word file that is read only and not displayed on the screen. I would expect the following to work but it is saving as the default .inv file generated by the program rather than changing to a Word file. Is there a better way to save as a .doc file? Word is also not closing afterward, so I changed the close code and then added "ActiveWindow.Close" to the original section of the macro after calling Invoice but it still doesn't close.

Sub Invoice()
Dim NewPath As String
NewPath = "m:\Invoices\"
ActiveDocument.SaveAs FileName:=NewPath & ActiveDocument.Name, FileFormat:=wdFormatDocument, ReadOnlyRecommended:=True

'ActiveDocument.Close
ActiveDocument.ActiveWindow.Close

End Sub

macropod
12-13-2017, 10:21 PM
So what does ActiveDocument.Name return?

RajanSyd
12-13-2017, 10:31 PM
So what does ActiveDocument.Name return?

For invoice 220 it saves 220.inv but if I use the following code it displays a save as dialog with Word file selected as the document type. It seems to ignore that when I hide it, unless there is a better way to save it in a nominated folder as a Word file.
ActiveDocument.SaveAs ActiveDocument.FullName, wdFormatDocument

I tried ActiveDocument.SaveAs FileName:=NewPath & ActiveDocument.FullName, FileFormat:=wdFormatDocument, ReadOnlyRecommended:=True
but get a message saying it isn't a valid file format

macropod
12-13-2017, 10:57 PM
You didn't answer my question...

RajanSyd
12-13-2017, 11:09 PM
220

The number of the invoice being generated by the software

macropod
12-13-2017, 11:24 PM
In that case, use:
ActiveDocument.SaveAs2 FileName:=NewPath & ActiveDocument.Name & ".doc", FileFormat:=wdFormatDocument, ReadOnlyRecommended:=True, AddToRecentFiles:=False
ActiveDocument.Close

Kindly don't keep posting consecutive parts of your reply. We shouldn't have to keep merging them...

RajanSyd
12-13-2017, 11:42 PM
thanks I'll try that in the morning.
I haven't used this forum (or many others) before. Should I not click Reply to respond? or did I click one of the other reply options?

macropod
12-14-2017, 12:07 AM
Should I not click Reply to respond? or did I click one of the other reply options?
Sure, but your last two posts (see above) were originally four posts. Surely you can do better than put one of two lines in each post? '220' and 'The number of the invoice being generated by the software', for example, really don't justify two separate posts...

RajanSyd
12-14-2017, 04:34 PM
OK, sorry. I posted the first then thought it was inadequate and so I posted the second without thinking of just editing the first one. It seems that the software outputs the file as 220.inv so I was going to investigate trimming it and replacing the extension but I've now heard that the .inv extension will be OK for what we are trying to achieve. Thanks for your assistance.