Error 5 Invalid procedure or argument

victorel21

New Member
Joined
Jul 8, 2021
Messages
25
Office Version
  1. 365
Platform
  1. Windows
HI everyone Im trying to create a pdf and send but im encountering an error i havent seen before.

Could any one help? Below the code.

Sub Accounts()

Dim MonOutlook As Object
Dim oBjmail As Object
Dim olFormatHTML As Object
Dim strNomClasseur As String
Dim strmsg3 As String
Dim myFile3 As Variant
Dim ws3 As Worksheet
Dim strfile5 As String
Dim strfile4 As String

'On Error GoTo errHandler

Sheets("PurchaseOrder").Activate
PO = Range("F3")

Sheets("Accounts").Activate
Set ws3 = ActiveSheet

strfile4 = "Account_amounts" & PO & ".pdf"

strfile5 = ThisWorkbook.Path & "\" & strfile2

Debug.Print strfile5

myFile3 = Application.GetSaveAsFilename _
(InitialFileName:=strfile4, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")

If myFile3 <> "False" Then

ws3.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile3, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False

Set MonOutlook = CreateObject("Outlook.Application")
Set oBjmail = MonOutlook.CreateItem(0)
Set olFormatHTML = MonOutlook.CreateItem(1)
strNomClasseur = strNomFichier
With oBjmail
.display
End With

Signature = oBjmail.HTMLbody

strmsg3 = "<BODY>Greetings,<br /><br> Please see the attached amounts for accounts.<br /><br>Regards.</BODY>"
titre3 = "Accounts_amounts" & PO

With oBjmail

.Attachments.Add strfile5
.Subject = titre3
.HTMLbody = strmsg3 & vbNewLine & Signature
.BodyFormat = 2
.display
End With

Set MonOutlook = Nothing
Set oBjmail = Nothing

Sheets("PurchaseOrder").Activate

'Else

'exitHandler:
'Exit Sub
'errHandler:
'MsgBox "Could not create PDF file"
'Resume exitHandler
'Sheets("PurchaseOrder").Activate
End If

End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
The strfile2 variable is neither declared nor valued, so empty. Therefore the strfile5 variable doesn't carry a valid filename. That way the attempt to add an attachement to your email will error.
 
Upvote 0
The strfile2 variable is neither declared nor valued, so empty. Therefore the strfile5 variable doesn't carry a valid filename. That way the attempt to add an attachement to your email will error.
Thank you!,

I have fixed this but im still getting the same error. The lines in bold are the ones that the code highlight as the error.

Sub Accounts()

Dim MonOutlook As Object
Dim oBjmail As Object
Dim olFormatHTML As Object
Dim strNomClasseur As String
Dim strmsg3 As String
Dim myFile3 As Variant
Dim ws3 As Worksheet
Dim strfile5 As String
Dim strfile4 As String

'On Error GoTo errHandler

Sheets("PurchaseOrder").Activate
PO = Range("F3")

Sheets("Accounts").Activate
Set ws3 = ActiveSheet

strfile4 = "Account_amounts" & PO & ".pdf"

strfile5 = ThisWorkbook.Path & "\" & strfile4

Debug.Print strfile5

myFile3 = Application.GetSaveAsFilename _
(InitialFileName:=strfile4, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")

If myFile3 <> "False" Then

ws3.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=myFile3, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False


Set MonOutlook = CreateObject("Outlook.Application")
Set oBjmail = MonOutlook.CreateItem(0)
Set olFormatHTML = MonOutlook.CreateItem(1)
strNomClasseur = strNomFichier
With oBjmail
.display
End With

Signature = oBjmail.HTMLbody

strmsg3 = "<BODY>Greetings,<br /><br> Please see the attached amounts for accounts.<br /><br>Regards.</BODY>"
titre3 = "Accounts_amounts" & PO

With oBjmail

.Attachments.Add strfile5
.Subject = titre3
.HTMLbody = strmsg3 & vbNewLine & Signature
.BodyFormat = 2
.display
End With

Set MonOutlook = Nothing
Set oBjmail = Nothing

Sheets("PurchaseOrder").Activate

'Else

'exitHandler:
'Exit Sub
'errHandler:
'MsgBox "Could not create PDF file"
'Resume exitHandler
'Sheets("PurchaseOrder").Activate
End If

End Sub
 
Upvote 0
Worksheet was very hidden, the solution was making it visible, problem seems to be solved.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,804
Messages
6,121,652
Members
449,045
Latest member
Marcus05

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top