shaunda101
New Member
- Joined
- Mar 20, 2013
- Messages
- 6
Hello, I have code to save an excel sheet as a PDF and attach it to an email but when it try to modify it to save as an excel, it causes errors. Below is (1) the excel code I'm trying to fix; and (2) the working code for my PDF function. When I run the macro to save the excel worksheet in a new workbook, it creates the sheet in the a separate workbooks, but then gives as Run-time error '9': Subscript out of range." When I run debug, the line in red is what's highlighted. So it's not liking something with the filename/path??
(2) Code to save worksheet as a PDF and Email
Sub Workbook_To_Excel_And_Create_Mail()
Dim FileName As String
'Call the function with the correct arguments
FileName = Create_PDF(ActiveWorkbook, "", True, False)
If FileName <> "" Then
'To send to specific person, put email address within quotes below; next field is subject...this part can be removed if not sending email'
Mail_PDF_Outlook FileName, "", "For Your Review", _
"Please see the attached PDF file." _
& vbNewLine & vbNewLine & "Thanks!", False
Else
MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _
"Microsoft Add-in is not installed" & vbNewLine & _
"You Canceled the GetSaveAsFilename dialog" & vbNewLine & _
"The path to Save the file is not correct" & vbNewLine & _
"You didn't want to overwrite the existing PDF if it exist"
End If
End Sub
Option Explicit
'The code below are used by the macros in the other modules
Function Create_PDF(Myvar As Object, FixedFilePathName As String, _
OverwriteIfFileExist As Boolean, OpenPDFAfterPublish As Boolean) As String
Dim FileFormatstr As String
Dim Fname As Variant
Dim sDate As String
'Test If the Microsoft Add-in is installed
If Dir(Environ("commonprogramfiles") & "\Microsoft Shared\OFFICE" _
& Format(Val(Application.Version), "00") & "\EXP_PDF.DLL") <> "" Then
If FixedFilePathName = "" Then
'Open the GetSaveAsFilename dialog to enter a file name for the pdf
FileFormatstr = "PDF Files (*.pdf), *.pdf"
sDate = Replace(Replace(FormatDateTime(Now(), vbShortDate), "/", "."), ":", ".")
'This names the pdf by using 2 fields from the workbook and the date
Fname = ActiveWorkbook.Path & "\" & Sheets("Summary").Cells(9, 2) & " " & ActiveSheet.Cells(1, 1) & " " & Format(sDate, "medium date") & ".pdf"
'If you cancel this dialog Exit the function
If Fname = False Then Exit Function
Else
Fname = FixedFilePathName
End If
'If OverwriteIfFileExist = False we test if the PDF
'already exist in the folder and Exit the function if that is True
If OverwriteIfFileExist = False Then
If Dir(Fname) <> "" Then Exit Function
End If
'Now the file name is correct we Publish to PDF
On Error Resume Next
Myvar.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:=Fname, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=OpenPDFAfterPublish
On Error GoTo 0
'If Publish is Ok the function will return the file name
If Dir(Fname) <> "" Then Create_PDF = Fname
End If
End Function
(1) Code to save worksheet into new workbook (broken)
Sub SaveAsExcel2()
Dim FileFormatstr As String
Dim Fname As Variant
Dim sDate As String
Dim project As String
project = Sheets("Summary").Cells(9, 2)
Dim sheetname As String
sheetname = ActiveSheet.Cells(1, 1)
sDate = Replace(Replace(FormatDateTime(Now(), vbShortDate), "/", "."), ":", ".")
ActiveSheet.Copy
Fname = ActiveWorkbook.Path & "\" & Sheets("Summary").Cells(9, 2) & " " & ActiveSheet.Cells(1, 1) & " " & Format(sDate, "medium date") & ".xls"
ActiveWorkbook.SaveAs FileName:=Fname
ActiveWorkbook.Close
End Sub
Dim FileFormatstr As String
Dim Fname As Variant
Dim sDate As String
Dim project As String
project = Sheets("Summary").Cells(9, 2)
Dim sheetname As String
sheetname = ActiveSheet.Cells(1, 1)
sDate = Replace(Replace(FormatDateTime(Now(), vbShortDate), "/", "."), ":", ".")
ActiveSheet.Copy
Fname = ActiveWorkbook.Path & "\" & Sheets("Summary").Cells(9, 2) & " " & ActiveSheet.Cells(1, 1) & " " & Format(sDate, "medium date") & ".xls"
ActiveWorkbook.SaveAs FileName:=Fname
ActiveWorkbook.Close
End Sub
(2) Code to save worksheet as a PDF and Email
Sub Workbook_To_Excel_And_Create_Mail()
Dim FileName As String
'Call the function with the correct arguments
FileName = Create_PDF(ActiveWorkbook, "", True, False)
If FileName <> "" Then
'To send to specific person, put email address within quotes below; next field is subject...this part can be removed if not sending email'
Mail_PDF_Outlook FileName, "", "For Your Review", _
"Please see the attached PDF file." _
& vbNewLine & vbNewLine & "Thanks!", False
Else
MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _
"Microsoft Add-in is not installed" & vbNewLine & _
"You Canceled the GetSaveAsFilename dialog" & vbNewLine & _
"The path to Save the file is not correct" & vbNewLine & _
"You didn't want to overwrite the existing PDF if it exist"
End If
End Sub
Option Explicit
'The code below are used by the macros in the other modules
Function Create_PDF(Myvar As Object, FixedFilePathName As String, _
OverwriteIfFileExist As Boolean, OpenPDFAfterPublish As Boolean) As String
Dim FileFormatstr As String
Dim Fname As Variant
Dim sDate As String
'Test If the Microsoft Add-in is installed
If Dir(Environ("commonprogramfiles") & "\Microsoft Shared\OFFICE" _
& Format(Val(Application.Version), "00") & "\EXP_PDF.DLL") <> "" Then
If FixedFilePathName = "" Then
'Open the GetSaveAsFilename dialog to enter a file name for the pdf
FileFormatstr = "PDF Files (*.pdf), *.pdf"
sDate = Replace(Replace(FormatDateTime(Now(), vbShortDate), "/", "."), ":", ".")
'This names the pdf by using 2 fields from the workbook and the date
Fname = ActiveWorkbook.Path & "\" & Sheets("Summary").Cells(9, 2) & " " & ActiveSheet.Cells(1, 1) & " " & Format(sDate, "medium date") & ".pdf"
'If you cancel this dialog Exit the function
If Fname = False Then Exit Function
Else
Fname = FixedFilePathName
End If
'If OverwriteIfFileExist = False we test if the PDF
'already exist in the folder and Exit the function if that is True
If OverwriteIfFileExist = False Then
If Dir(Fname) <> "" Then Exit Function
End If
'Now the file name is correct we Publish to PDF
On Error Resume Next
Myvar.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:=Fname, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=OpenPDFAfterPublish
On Error GoTo 0
'If Publish is Ok the function will return the file name
If Dir(Fname) <> "" Then Create_PDF = Fname
End If
End Function