save a (previously) unsaved document without any user input

L

Larry

Is it possible programmatically to save a previously unsaved document
without any user input, without displaying the SaveAs dialog? It
doesn't necessarily matter what the name of the saved file will be or
what folder it is saved to. This is one step in a larger macro.

Larry
 
J

Jay Freedman

Hi, Larry,

Sure, you just do something like

ActiveDocument.SaveAs Filename:="c:\my documents\blah1.doc"

If you want to get fancy, you can throw in a routine that uses the Dir
function to check whether the proposed filename is already in use, and
increment the number until it finds one that doesn't exist yet.
 
L

Larry

Jay, I feel stupid that I didn't figure out such a simple thing, but the
Help material in Word 97 VBA had all this stuff about the arguments that
go with the SaveAs statement, but nothing as simple as a simple path!

Now, if could get the routine that increments the number of the file to
be saved over any existing file in the same folder, that would be great.
That introduces the problem of finding the highest number filename
existing in the folder, and then making the new file name one number
higher than that. I have no idea how to do that. The only thing I can
come up with is something crude like this, which obviously could only
work once and so is useless:

Dim ExistingFile As String
Dim sFileName As String
ExistingFile = "C:\Documents\tests\blah1.doc"

If ExistingFile <> "" Then
sFileName = "c:\documents\tests\blah2.doc"
Else
sFileName = "c:\documents\tests\blah1.doc"
End If

ActiveDocument.SaveAs FileName:=sFileName
ActiveWindow.Caption = (WordBasic.filenameinfo(ActiveDocument.Name, 4))

Larry
 
L

Larry

I guess it would involve using the Dir statement to loop through the
files in a folder and finding the one with the highest number, then
incremending that by one to use as the name for the next file, but I
have no idea how to do that.
 
D

Doug Robbins - Word MVP

Hi Larry,

See the article “How to save a document using a filename that gets
incremented by 1 each time if the filename already exists” at:

http://www.mvps.org/word/FAQs/MacrosVBA/SaveIncrementedFilename.htm

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top