vba - How to assign a recipient to .To?
问题描述
I convert a worksheet into a PDF and am trying to have that PDF emailed to me and copied to another person. All of this will be assigned to an action button/trigger.
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 = "xxxxx.xxxxx@fedex.com"
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 = "xxxxx.xxxxx@fedex.com"
.Subject = Subject
.Body = Message
.Attachements.Add Fname
.Send
End With
Set OutlookApp = Nothing
End Sub
The recipient line is where I am having issues. When I run the debugger, it's giving
Run-Time error '91: Object variable or with block variable not set
解决方案
我会将收件人变暗为字符串并更新 .to 分配:
改变
Dim Recipient As Outlook.Recipient
.To = Recipient = "dennis.aikens@fedex.com"
至
Dim Recipient As string
.To = Recipient
推荐阅读
- javascript - 使用lodash在数组列表中按字母排序名称
- android - AsyncTask 可以从后台线程执行吗?
- javascript - 垂直动画javascript
- java - 使用 Spring Boot 服务器进行 Android 登录身份验证
- google-app-engine - Django 与 GAE 上的 Cloud SQL 灵活
- visual-studio - 如何使用 Visual Studio 2010 Express Edition 创建 VBSCRIPT(.vbs) 文件?
- java - 如何选择 datetime mysql 类型为 java.time.LocalDateTime?
- regex - 正则表达式之前不捕获?
- python - ACCESS_REFUSED - 使用身份验证机制 AMQPLAIN 拒绝登录。有关详细信息,请参阅代理日志文件
- javascript - 为什么 let 关键字不适用于 eval()