LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,560
Default Run-time error 1004 Method SaveAS of object _Workbook failed

First...many THANKS to Ron DeBruin for the code to save a sheet using 2007 in
2003 format. It solved a previous issue I had posted.
However, using Ron's code, if a name already exists a message will come up
asking if I want to replace the exisitng file. If I click No or Cancel, I get
the Run Time error at this line of the code:
..SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum

Here is the whole code, MUCH of which is Ron's! THANKS RON!!
Sub SaveMWJCAsR()

'Revised 12-17-08 to force version to run in Excel 2007 but save in 2003
..xls format
'Got from http://www.rondebruin.nl/saveas.htm

'Working in Excel 97-2007
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim MyDirectory As String
Dim JNum As String
Dim wsoutput As Worksheet

'Checks to See If A Directory Exists, If Not, Creates It
MyDirectory = ActiveWorkbook.Path & "\" & "2009 Saved Jobs"
DirTest = Dir$(MyDirectory, vbDirectory)
If DirTest = "" Then
MkDir MyDirectory
DoEvents 'just to make sure it is there
End If

'Set the Directory Here!
ChDir MyDirectory

DefPath = MyDirectory

If Right(DefPath, 1) < "\" Then
DefPath = DefPath & "\"
End If

With Application
.ScreenUpdating = False
.EnableEvents = False
End With

Set Sourcewb = ActiveWorkbook

If Sheet96.Range("E5") < "" Then
JNum = Sheet96.Range("E5")
Else
End If

Range("A1").Activate
ActiveWorkbook.Colors(53) = RGB(247, 252, 255)
Range("A1").Select

'Copy the sheet to a new workbook
ActiveSheet.Copy
Set Destwb = ActiveWorkbook

'Determine the Excel version and file extension/format
With Destwb
If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007
'We exit the sub when your answer is NO in the security dialog
that you
'only see when you copy a sheet from a xlsm file with macro's
disabled.
If Sourcewb.Name = .Name Then
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox "Your answer is NO in the security dialog"
Exit Sub
Else
'Inserted below to force 2003 .xls file
FileExtStr = ".xls": FileFormatNum = 56
End If
End If
End With

'Save the new workbook and close it
'The Path is Set Above
'TempFilePath = Application.DefaultFilePath & "\"

'Determine File Name
If Range("H42") = 0 Then
TempFileName = "Job " & JNum
Else
TempFileName = "Job " & JNum & "C"
End If

With Destwb
.SaveAs TempFilePath & TempFileName & FileExtStr,
FileFormat:=FileFormatNum
.Close SaveChanges:=False
End With

ChDir CurDir & "\.."

With Application
.ScreenUpdating = True
.EnableEvents = True
End With



End Sub



 
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
method 'SaveAs' of object '_Workbook' failed Kishi Excel Programming 1 July 12th 06 03:51 AM
Run-time error '1004': Method 'Cells' of object _Global failed Fran D[_6_] Excel Programming 3 May 12th 06 04:44 PM
Urgent help Runtime error ‘1004’ Method ‘SaveAs’ of object’-Workbook’ failed ????? funkymonkUK[_155_] Excel Programming 4 May 4th 06 12:56 PM
Run-time error '1004': Method 'Range' of object '_Global' failed Neild Excel Programming 0 February 12th 06 11:43 PM
Run-time error '1004': Method 'Range' of object '_Global' failed Tim Williams Excel Programming 2 February 12th 06 10:01 PM


All times are GMT +1. The time now is 11:08 PM.

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

About Us

"It's about Microsoft Excel"