ich bekomme immer einen Fehler wenn ich folgenden Code ausführen möchte. Der Code ist auch in einem anderen Arbeitsblatt und dort funktioniert er top.
Sub SaveAsPDFandSendWeekly()
Dim xSht As Worksheet
Dim xFileDlg As FileDialog
Dim xFolder As String
Dim xYesorNo As Integer
Dim xOutlookObj As Object
Dim xEmailObj As Object
Dim xUsedRng As Range
Dim xPath As String
Set xSht = ActiveSheet
Dim cellname As String
cellname = Range("B3").Value
xPath = "TEST" 'here "workshet to pdf" is the destination folder to save the pdf files
xFolder = xPath + "\" + xSht.Name + " " + cellname + ".pdf"
If Len(Dir(xFolder)) > 0 Then
xYesorNo = MsgBox(xFolder & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it? _
_
", _
vbYesNo + vbQuestion, "File Exists")
On Error Resume Next
If xYesorNo = vbYes Then
Kill xFolder
Else
MsgBox "if you don't overwrite the existing PDF, I can't continue." _
& vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Exiting Macro"
Exit Sub
End If
If Err.Number 0 Then
MsgBox "Unable to delete existing file. Please make sure the file is not open or write _
protected." _
& vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Unable to Delete File"
Exit Sub
End If
End If
Set xUsedRng = xSht.UsedRange
If Application.WorksheetFunction.CountA(xUsedRng.Cells) 0 Then
'Save as PDF file
xSht.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xFolder, Quality:=xlQualityStandard
'Create Outlook email
Set xOutlookObj = CreateObject("Outlook.Application")
Set xEmailObj = xOutlookObj.CreateItem(0)
With xEmailObj
.Display
.To = "test"
.CC = "test"
.Subject = xSht.Name + " " + cellname
.Attachments.Add xFolder
If DisplayEmail = False Then
'.Send
End If
End With
Else
MsgBox "The active worksheet cannot be blank"
Exit Sub
End If
End Sub
Ich bekomme immer einen Run time error 1004 und bei Klick auf Debug wird"xSht.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xFolder, Quality:=xlQualityStandard" gelb markiert.
Könnte das bitte jemand testen?