excel - 使用 ActiveWorkbook.Path 在电子邮件中插入图像
问题描述
我的电子表格中有 Excel VBA 代码,其中包含姓名和电子邮件地址列表、创建 PowerPoint 证书并将每个人的证书通过电子邮件发送。
如果我给它一个特定的路径,我可以在电子邮件的末尾添加一个徽标,例如
C:\用户\用户\桌面\文件夹\img.png
但如果我说
ActiveWorkbook.Path & '\img.png'
它插入一个空框。
Public Function generateCerts()
Dim CurrentFolder As String
Dim fileName As String
Dim myPath As String
Dim UniqueName As Boolean
Dim sh As Worksheet
Dim myPresentation As PowerPoint.Presentation
Dim PowerPointApp As PowerPoint.Application
Dim shp As PowerPoint.Shape
Dim outlookApp As Outlook.Application
Dim myMail As Outlook.MailItem
Set outlookApp = New Outlook.Application
Set PowerPointApp = CreateObject("PowerPoint.Application")
Set myPresentation = PowerPointApp.Presentations.Open(ActiveWorkbook.Path & "\Certificate3.pptx")
Set shp = myPresentation.Slides(1).Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, Left:=0, Top:=250, Width:=825, Height:=68)
shp.TextFrame.TextRange.Font.Size = 36
shp.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignCenter
Set sh = Sheets("CertNames")
For Each rw In sh.Rows
If rw.Row > 1 Then
If sh.Cells(rw.Row, 1).Value = "" Then
Exit For
End If
shp.TextFrame.TextRange.Text = sh.Cells(rw.Row, 1) & " " & sh.Cells(rw.Row, 2).Value & " " & sh.Cells(rw.Row, 3).Value
fileName = ActiveWorkbook.Path & "\" & sh.Cells(rw.Row, 2).Value & " " & sh.Cells(rw.Row, 3).Value & " " & rw.Row & ".pdf"
myPresentation.ExportAsFixedFormat fileName, _
ppFixedFormatTypePDF, ppFixedFormatIntentPrint, msoCTrue, ppPrintHandoutHorizontalFirst, _
ppPrintOutputSlides, msoFalse, , ppPrintAll, , False, False, False, False, False
Set myMail = outlookApp.CreateItem(olMailItem)
myMail.Attachments.Add fileName
myMail.SentOnBehalfOfName = ""
' myMail.BCC = ""
myMail.To = sh.Cells(rw.Row, 4).Value
myMail.Subject = "Thank you for attending"
myMail.HTMLBody = "Hello" & " " & sh.Cells(rw.Row, 1).Value & ","
myMail.HTMLBody = myMail.HTMLBody & "<p>Thank you for participating in <b><i>Session 7
myMail.HTMLBody = myMail.HTMLBody & "<p>Support</p>"
myMail.HTMLBody = myMail.HTMLBody & "<img src='ActiveWorkbook.Path & '\img.png''"
myMail.Display
' myMail.Send
End If
Next rw
myPresentation.Saved = True
PowerPointApp.Quit
End Function
解决方案
将路径声明为字符串,以便您可以检查它:
Dim imagePath As String
imagePath = ActiveWorkbook.Path & "\img.png"
既然您知道它是正确的,请像这样使用它:
myMail.HTMLBody = myMail.HTMLBody & "<img src='" & imagePath & "'>"
推荐阅读
- angular - 警告检测到循环依赖 - Angular 8
- python - Python 正则表达式:分别替换每个匹配项
- r - 从R中的时间序列中提取某些月份
- c# - 使用 Console.ReadLine().Split() 在循环中填充字符串数组
- c - 如何将 SCSI 请求放入 Linux 内核的队列中?
- java - 覆盖现有的 java 注解
- regex - 正则表达式 Google 表格有条件
- php - 将每个上传的文件名写入 SQL 数据库(PHP、MySQL)
- c++ - 有没有办法生成一个序列,其中包括总和为整数平方的平方?
- java - itextpdf-7:htmltopdf:找不到标签 wbr 的工作人员