Saving document with new name via vba

R

rachitm

Hi,

Following is the scenario:

I have a document named: CCPC_01.doc
The user opens up the document makes the changes.

I want a macro which would save the updated version of this document
(doesnt matter if its in the same folder) with the following name:
CCPC_01_locked.doc

All Help is Appreciated
 
R

rachitm

To restate what I would like help with is that I want a macro which
will add "_locked".doc at the end of the current name of the document
and save it as a new file.

Thanks
 
T

Tony Strazzeri

To restate what I would like help with is that I want a macro which
will add "_locked".doc at the end of the current name of the document
and save it as a new file.

Thanks

Hi Rach,

Try this.
Dim strNewName As String
Dim dt As Single 'Location of the dot before the filename
extension

strNewName = ActiveDocument.FullName
dt = InStrRev(strNewName, ".")
strNewName = Left(strNewName, dt - 1) & "_Locked" &
Mid(strNewName, dt)
ActiveDocument.SaveAs FileName:=strNewName

This will keep save the document to the same folder and preserve the
extension.
Of course this new doc now becomes the activedociument so if you then
want to go back/work on the original document you have to close this
new one and open the original.

Cheers
TonyS.
 
R

rachitm

Thanks Tony , this works perfect!

Got a question, is there a way that the original document can still be
open after it saves it with the new document name.....?
 
R

Russ

Rachitm,
Thanks Tony , this works perfect!

Got a question, is there a way that the original document can still be
open after it saves it with the new document name.....?
A simple modification of Tony's routine should do want you want.
The original, unmodified document is reopened.

Dim strOriginalName As String
Dim strNewName As String
Dim dt As Single

strOriginalName = ActiveDocument.FullName
strNewName = ActiveDocument.FullName
dt = InStrRev(strNewName, ".")
strNewName = Left(strNewName, dt - 1) & "_Locked" & _
Mid(strNewName, dt)
ActiveDocument.SaveAs FileName:=strNewName
'ActiveDocument.Close
Documents.Open FileName:=strOriginalName



UnComment the .Close line, if that is what you want.
 

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