I'm having trouble finding a type mismatch in a code i didn't make myself. The purpose of the code is to make a PDF file for the chart on sheet 1 (Named 'Chart'). While debugging it steps out at the format for strTime but i can't find anything wrong with it. The code works perfect in another file with the same purpose and use. The strTime works and the files are identical. Haven't found the answer on stack or elsewhere.
I'm fairly new to VBA so let me know if i missed anything obvious.
Thank you in advance!
Dim WSA As Chart
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler
ActiveWorkbook.Charts(1).Activate
Set wbA = ThisWorkbook
Set WSA = ActiveSheet
strTime = Format(DateAdd("ww", -1, Now), "ww")
'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"
'replace spaces and periods in sheet name
strName = Replace(WSA.Name, " ", "")
strName = Replace(strName, ".", "_")
'create default name for saving file
strFile = "Weekly overview" & " " & strTime & ".pdf"
strPathFile = strPath & strFile
' user can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
'export to PDF if a folder was selected
If myFile <> "False" Then
WSA.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
includedocproperties:=False, _
ignoreprintareas:=False, _
openafterpublish:=True
'confirmation message with file info
MsgBox "PDF file has been created: " _
& vbCrLf _
& myFile
End If
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file" & vbCrLf & Error$
Resume exitHandler