Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Save Current Workbook With Extended Filename

Hello,

I'm trying to write a macro to save (save as) the active workbook:

a. in the same directory
b. with a new filename (extension of the original)

Lets say the name of the original file is "test.xls". I would like to
Save As this file as "test_import.xls" in the same directory resulting
in the original file plus a copy with the extended name.

The following is a hard-coded approach which does what I need, but I
need to dynamically determine the file path and edit the name. The
extension to the name will always be "_import."


ChDir "C:\Temp"
ActiveWorkbook.SaveAs Filename:= _
"C:\Temp\test_import.xls" _
, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False

Any help would be appreciated.

Thanks

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 29
Default Save Current Workbook With Extended Filename

See if the following code is useful:

Sub saveMe

Dim sCurrentName as string

' Parse out the name of the current workbook without the path or the
".xls" extension
sCurrentName = Mid(ActiveWorkbook.Name, 1, Len(ActiveWorkbook.Name) -
4)

' Add whatever extension you want
sNewName = sCurrentName & "_import"

' Do a minimal save
ActiveWorkbook.SaveAs ActiveWorkbook.Path & "\" & sNewName

End sub

HTH
/ Tyla /


On Mar 20, 11:05 am, "Nate" wrote:
Hello,

I'm trying to write a macro to save (save as) the active workbook:

a. in the same directory
b. with a new filename (extension of the original)

Lets say the name of the original file is "test.xls". I would like to
Save As this file as "test_import.xls" in the same directory resulting
in the original file plus a copy with the extended name.

The following is a hard-coded approach which does what I need, but I
need to dynamically determine the file path and edit the name. The
extension to the name will always be "_import."

ChDir "C:\Temp"
ActiveWorkbook.SaveAs Filename:= _
"C:\Temp\test_import.xls" _
, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False

Any help would be appreciated.

Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,101
Default Save Current Workbook With Extended Filename

Private Sub SaveWorkbookToFolder()
Dim userInput As String
Dim anyFilename As String
Dim SaveToPath As String

anyFilename = ActiveSheet.Name & ".xls"
SaveToPath ="C:\Temp\test_import.xls"

anyFilename = ActiveSheet.Name & ".xls"
If Dir(SaveToPath & anyFilename) = "" Then

ActiveWorkbook.SaveAs Filename:=SaveToPath & anyFilename
Else

Select Case MsgBox("A file named: '" & ActiveSheet.Name & " already
exists in " & SaveToPath _
& vbCrLf & "What would you like to do?" & vbCrLf _
& "Overwrite the existing file? [Yes]" & vbCrLf _
& "Save file with a different name? [No]" & vbCrLf _
& "Cancel - do not save this file at this time. [Cancel]", _
vbYesNoCancel + vbExclamation + vbDefaultButton2, "Commission
Manager")

Case Is = vbYes

Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=SaveToPath & ActiveSheet.Name
Application.DisplayAlerts = True

Case Is = vbNo


userInput = "dummy entry to make it work"

GetFileNameFromUser:
Do While userInput < ""
anyFilename = InputBox$("Enter a new filename to use:", _
"Commission Manager", ActiveSheet.Name)
If Right(UCase(Trim(anyFilename)), 4) < ".XLS" Then
anyFilename = anyFilename & ".xls"
End If
If ValidateFilename(anyFilename) < "" Then

MsgBox "The filename you have entered is not a valid
filename." _
& vbCrLf & "Filenames may not have any of these
characters in them:" _
& vbCrLf & " \ / : * ? < | " & Chr$(34) & vbCrLf _
& "Please provide a valid filename.", vbOKOnly,
"Invalid Filename"
GoTo GetFileNameFromUser
End If
If Trim(UCase(anyFilename)) = ".XLS" Then

If MsgBox("You have chosen to Cancel the file save." & _
"Did you really intend to Cancel this operation?", _
vbYesNo + vbInformation, "Confirm Cancel") < vbYes Then
GoTo GetFileNameFromUser
Else

anyFilename = ":* QUIT *:"
userInput = ""
End If
End If
If userInput < "" Then

userInput = Dir(SaveToPath & anyFilename)
End If
Loop


If anyFilename < ":* QUIT *:" Then

ActiveWorkbook.SaveAs Filename:=SaveToPath & anyFilename
End If

Case Else


Application.DisplayAlerts = False
ActiveWindow.Close
Application.DisplayAlerts = True

End Select
End If


End Sub

"Nate" wrote:

Hello,

I'm trying to write a macro to save (save as) the active workbook:

a. in the same directory
b. with a new filename (extension of the original)

Lets say the name of the original file is "test.xls". I would like to
Save As this file as "test_import.xls" in the same directory resulting
in the original file plus a copy with the extended name.

The following is a hard-coded approach which does what I need, but I
need to dynamically determine the file path and edit the name. The
extension to the name will always be "_import."


ChDir "C:\Temp"
ActiveWorkbook.SaveAs Filename:= _
"C:\Temp\test_import.xls" _
, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False

Any help would be appreciated.

Thanks


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
macro to grab current workbook filename TR Young Excel Worksheet Functions 3 June 12th 09 12:41 PM
SaveAs uses current open workbook filename; big wheelz Excel Programming 3 September 2nd 06 11:17 AM
save workbook as current date using a macro ExcelJon Excel Discussion (Misc queries) 2 June 21st 06 03:05 AM
Save Workbook as Current Date/Time Jacob_F_Roecker Excel Discussion (Misc queries) 1 October 25th 05 09:37 AM
unable to save current workbook within WorkbookBeforeClose TTN Excel Programming 4 November 20th 03 05:35 PM


All times are GMT +1. The time now is 06:49 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"