Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-16-2013, 09:48 AM
d4okeefe d4okeefe is offline Rename Document & Save Windows Vista Rename Document & Save Office 2010 64bit
Advanced Beginner
Rename Document & Save
 
Join Date: Apr 2013
Posts: 77
d4okeefe is on a distinguished road
Default Rename Document & Save

I would like to create a macro that renames an open document, and saves the newly named document to the same folder as the open document.

For example, the file I have open is located in H:\, and the file name is 00000 Frame aa 0.docx. After running the macro, the new file name should be: 00000 Frame aa 01.docx

Or, the open document's file name is 12345 de Ville cv 01.docx, and the new file name should be 12345 de Ville cv 02.docx.

In essence, the new file name should be very similar to the open document's, except that the number before .docx should be one higher.

To add a wrinkle, I would also like the macro to display a warning if the new document name is already in the folder.



Thanks!

Daniel
Reply With Quote
  #2  
Old 05-22-2013, 10:49 PM
macropod's Avatar
macropod macropod is offline Rename Document & Save Windows 7 32bit Rename Document & Save Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Sub ReSave()
Dim StrName As String, StrExt As String, StrNum As String, Rslt
With ActiveDocument
  StrName = .Name
  StrExt = Right(StrName, Len(StrName) - InStrRev(StrName, ".") + 1)
  StrName = Left(StrName, InStrRev(StrName, ".") - 1)
  StrNum = Format(Split(StrName, " ")(UBound(Split(StrName, " "))) + 1, "00")
  StrName = Left(StrName, InStrRev(StrName, " "))
  StrName = .Path & "\" & StrName & StrNum & StrExt
  If Dir(StrName) <> "" Then
    Beep
    Rslt = MsgBox("The new filename:" & vbCr & StrName & vbCr & "already exists." & _
      vbCr & "Continue saving (overwrite existing file)?", vbOKCancel)
    If Rslt = vbCancel Then Exit Sub
  End If
  .SaveAs2 FileName:=StrName, FileFormat:=.SaveFormat
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]

Last edited by macropod; 05-22-2013 at 11:06 PM. Reason: Added warning code
Reply With Quote
  #3  
Old 05-23-2013, 09:01 AM
d4okeefe d4okeefe is offline Rename Document &amp; Save Windows Vista Rename Document &amp; Save Office 2010 64bit
Advanced Beginner
Rename Document &amp; Save
 
Join Date: Apr 2013
Posts: 77
d4okeefe is on a distinguished road
Default

Thank you, Paul. As you likely guessed, I'm new to VBA, but find it very useful.

In the code, I changed .SaveAs2 to simply .SaveAs, in order to prevent an error. Is there a reason why I should have kept .SaveAs2?

Thanks again,

Daniel
Reply With Quote
  #4  
Old 05-23-2013, 09:04 AM
macropod's Avatar
macropod macropod is offline Rename Document &amp; Save Windows 7 32bit Rename Document &amp; Save Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

SaveAs has been deprecated. Its successor is SaveAs2. If you were getting errors, that suggests you were using the macro with something earlier than Word 2007.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 05-23-2013, 09:35 AM
d4okeefe d4okeefe is offline Rename Document &amp; Save Windows Vista Rename Document &amp; Save Office 2007
Advanced Beginner
Rename Document &amp; Save
 
Join Date: Apr 2013
Posts: 77
d4okeefe is on a distinguished road
Default

Sorry. The information about my Office program was wrong on my profile, although I am using Word 2007.

Thanks again. You have made my work easier!

Daniel
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can not save read only document crescere Word 1 03-30-2013 11:19 PM
Word ask to save template whenever i save a derived document jorbjo Word 3 10-04-2012 10:52 AM
Using Save As on a Read Only Document jenc13 PowerPoint 1 01-18-2012 11:34 AM
Rename Document &amp; Save Cannot save Word document don madsen Word 6 09-16-2011 09:02 PM
Rename Document &amp; Save code to save / rename / send attachments unit213 Outlook 1 09-26-2007 08:15 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:42 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft