Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Jen Jen is offline
external usenet poster
 
Posts: 119
Default Runtime error 1004 - cannot open the file

Hello. I have assigned a macro to a button to have a workbook emailed. This
works for all users here except for two. Two users receive 'Runtime error
1004 - Excel cannot open the file 'FileName.xlsm' because the file format or
file extension is not valid. Verify that the file has not been corrupted and
that the file extension matches the format of the file.'

It errors for these two users on this line:
Set wb2 = Workbooks.Open(TempFilePath & TempFileName & FileExtStr)

Thanks!


Here is the entire code:

Sub MailWorksheet()
'Macro to send email
'http://www.rondebruin.nl/tips.htm

Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim TempFilePath As String
Dim TempFileName As String

TempFilePath = Environ$("temp") & "\"
Set wb1 = ActiveWorkbook


If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007
If wb1.HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If

End If

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


TempFileName = ThisWorkbook.Name & " " & Format(Now, "dd-mmm-yy h-mm")


wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr
Set wb2 = Workbooks.Open(TempFilePath & TempFileName & FileExtStr)

On Error Resume Next

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
.To = "email address here"
.CC = ""
.BCC = ""
.Subject = "Purchase Order"
.Attachments.Add ActiveWorkbook.FullName
.Send
MsgBox "Your purchase order has been sent. Thank you.",
vbOKOnly
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
On Error GoTo 0
wb2.Close SaveChanges:=False


Kill TempFilePath & TempFileName & FileExtStr

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Runtime error 1004 - cannot open the file

You change the code not correct

Use the code from this page
http://www.rondebruin.nl/mail/folder2/mail1.htm

--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"Jen" wrote in message ...
Hello. I have assigned a macro to a button to have a workbook emailed. This
works for all users here except for two. Two users receive 'Runtime error
1004 - Excel cannot open the file 'FileName.xlsm' because the file format or
file extension is not valid. Verify that the file has not been corrupted and
that the file extension matches the format of the file.'

It errors for these two users on this line:
Set wb2 = Workbooks.Open(TempFilePath & TempFileName & FileExtStr)

Thanks!


Here is the entire code:

Sub MailWorksheet()
'Macro to send email
'http://www.rondebruin.nl/tips.htm

Dim OutApp As Object
Dim OutMail As Object
Dim sh As Worksheet
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim FileExtStr As String
Dim FileFormatNum As Long
Dim TempFilePath As String
Dim TempFileName As String

TempFilePath = Environ$("temp") & "\"
Set wb1 = ActiveWorkbook


If Val(Application.Version) < 12 Then
'You use Excel 97-2003
FileExtStr = ".xls": FileFormatNum = -4143
Else
'You use Excel 2007
If wb1.HasVBProject Then
FileExtStr = ".xlsm": FileFormatNum = 52
Else
FileExtStr = ".xlsx": FileFormatNum = 51
End If

End If

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


TempFileName = ThisWorkbook.Name & " " & Format(Now, "dd-mmm-yy h-mm")


wb1.SaveCopyAs TempFilePath & TempFileName & FileExtStr
Set wb2 = Workbooks.Open(TempFilePath & TempFileName & FileExtStr)

On Error Resume Next

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
.To = "email address here"
.CC = ""
.BCC = ""
.Subject = "Purchase Order"
.Attachments.Add ActiveWorkbook.FullName
.Send
MsgBox "Your purchase order has been sent. Thank you.",
vbOKOnly
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
On Error GoTo 0
wb2.Close SaveChanges:=False


Kill TempFilePath & TempFileName & FileExtStr

End Sub

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
Runtime Error 1004 - File could not be accessed Carlee Excel Programming 1 March 30th 07 01:30 AM
runtime error 1004 saveas text file Janis Excel Programming 2 September 1st 06 11:11 PM
File Name Causes Runtime Error 1004 Robert Excel Programming 1 November 10th 05 02:47 PM
Error 1004 when I open a file Deer Hunter Excel Programming 3 August 27th 03 05:33 PM
runtime error 1004 when opening excel file via VBA in a browser Ken Hunter Excel Programming 2 July 31st 03 03:33 PM


All times are GMT +1. The time now is 03:24 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"