Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WORD VBA- how to Automaticaly save a duplicate and name a document

Status
Not open for further replies.

Walter349

Technical User
Aug 16, 2002
250
0
0
BE
I am trying to find a way in WORD 2012, to automatically save a duplicate of a document to a specified location, when the user selects the menu 'save' or 'save as' function. Ideally this should be transparent to the user. But is not strictly necessary.

The document is a template, that users complete and then save.

Additionally, is it possible to generate a file name from specific required fields in the document. Trying to get the users to stick to a naming strategy is like trying to herd cats.

Any examples of this would help as I have not been able to find any. If it was excel I would have it covered. But word is another matter.

'If at first you don't succeed, then your hammer is below specifications'
 
I'd use the Application.DocumentBeforeSave event.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks for that, It looks as if that would be the general answer, but how does it work after that?

Is it the same structure as in Excel?

How would I get the information from fields in the document to populate variables? In excel it would be by referencing the cell contents, how does that work in word fields?





'If at first you don't succeed, then your hammer is below specifications'
 
automatically save a duplicate of a document ... The document is a template, that users complete and then save.

Sounds like you are trying to prevent users from overwriting your "template".

Use a proper template - .dot - instead.

Yes, it's possible to use values from fields to make a file name. In general:

File_Name = Field1.value & ".doc
 
Mintjulep,

The template is not the problem here. That is retained as a default. The problem lies in the fact the users do not do as instructed, in that they;

a) Do not consistently use the assigned naming strategy.
b) Do not save the reports to the assigned location.

Therefore trying to find the reports can be difficult as they tend to stick them any old place.
As we are going to change the layout of the reporting format, I thought it would be a good time to address this.

I am attempting to create a VBA script that will automatically save a copy of the report to a specified location with a default file name, every time they select 'save' or 'save as'.

I have done this with excel forms to resolve a similar situation and it works great. But I am not familiar with doing the same thing in word.

So am looking for an example that would demonstrate how it works in Word. So far I have not been able to find anything that even vaguely looks at the idea.

'If at first you don't succeed, then your hammer is below specifications'
 
When in VBE search the Object browser (F2) for BeforeSave and then press the F1 key to get some help on how to deal with application's events in Word.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hmm,

Thanks to all. But on the face of it, it seems this has never been done before and nobody has any better idea than I do, on how to go about it.

So I am putting this in the too time consuming to be bothered with box.

'If at first you don't succeed, then your hammer is below specifications'
 
I am attempting to create a VBA script that will automatically save a copy of the report to a specified location with a default file name, every time they select 'save' or 'save as'.

But on the face of it, it seems this has never been done before

Actually it has been done many times before. And no, it is not all that difficult.

You can use the DocumentBeforeSave, or you can rewrite Save and SaveAs to override the original routines to save to whatever location you want, using any name you want.

Code:
Sub FileSaveAs()
Dim path As String
Dim DefaultName As String
path = "c:\blah\whatever\"
DefaultName = "my defaultname"
ActiveDocument.SaveAs path & DefaultName & ".doc"
End Sub

Sub FileSave()
Dim path As String
Dim DefaultName As String
path = "c:\blah\whatever\"
DefaultName = "my defaultname"
ActiveDocument.SaveAs path & DefaultName & ".doc"
End Sub

Both will save the active document as c:\blah\whatever\my defaultname.doc.

The template is not the problem here. That is retained as a default.
Retained as default? How are you doing that?

In any case the use of a real template would be a good idea. And again, I do not know why you think it has not been done before, but it most certainly has been done before...and asked about...a number of times.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top