VBA新手:Excel VBA PDF到电子邮件

问题描述 投票:0回答:2

我对VBA很了解,但似乎很快就抓住了这个过程。然而,我撞墙了...

我正在尝试编写一个代码,将工作表转换为PDF,然后将该PDF通过电子邮件发送给我并复制到另一个人。一旦代码工作,所有这些都将分配给操作按钮/触发器。

这是我到目前为止所拥有的:

Option Explicit

Sub SendExcelFileAsPDF()



Dim OutlookApp As Outlook.Application
Dim emItem As Object
Dim Receipt As String, Subject As String
Dim Message As String, Fname As String

Dim Recipient As Outlook.Recipient
Recipient = "[email protected]"
Subject = "Weekly Critical Items" & " " & Range("L1")
Message = Range("D2") & Range("J2") & "Weekly Critical Items submitted" & 
Range("L1") & " " & "in PDF Format"
Message = Message & vbNewLine & vbNewLine & "Offload Ops"
Fname = Application.DefaultFilePath & "/" & ActiveWorkbook.Name & ".pdf"


ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Fname

Set OutlookApp = New Outlook.Application

Set emItem = OutlookApp.CreateItem(olMailItem)
With emItem
.To = Recipient = "[email protected]"
.Subject = Subject
.Body = Message
.Attachements.Add Fname
.Send
End With
Set OutlookApp = Nothing

End Sub

收件人行是我遇到问题的地方。当我运行调试器时,它给我一个运行时错误'91:对象变量或没有设置块变量,我不知道为什么会这样?我从Youtube视频中编辑了这段代码,这些视频确切地说明了我需要做的事情,当然,它在视频中起作用,但在这种情况下不起作用。

我在公司的交易账户上,如果这有任何区别。我在另一个堆栈问题上看到了可能不同的问题。

有什么建议?

谢谢!

excel vba excel-vba outlook pdf-generation
2个回答
1
投票

我会将收件人作为字符串调整并更新.to分配:

更改

Dim Recipient As Outlook.Recipient

.To = Recipient = "[email protected]"

Dim Recipient As string

.To = Recipient

0
投票

这条线

.To =收件人=“[email protected]

应该只是

.To =收件人

© www.soinside.com 2019 - 2024. All rights reserved.