当您单击电子邮件中的超链接时,我希望打开一个 Excel 文件并运行名为“LoadCSV”的子过程。
我已经使用下面的代码尝试过此操作。邮件正文包含一个链接,单击该链接*应*调用 OpenExcel 函数,该函数*应*打开文件并运行 LoadCSV。但是,当我单击超链接时,我看到以下内容。
当我选择“是”时,什么也没有发生。
`
Sub EmailWithAttachement()
Dim OutApp As Object
Dim OutMail As Object
Dim EmailAddress As String
Dim AttachmentPath As String
Dim MailSubject As String
Dim MailBody As String
' Define email address, attachment path, and mail subject
EmailAddress = ThisWorkbook.Sheets("Write").Range("H2").Value
AttachmentPath = ThisWorkbook.Path & "\" & ThisWorkbook.Sheets("Write").Range("I2").Value & ".pdf"
MailSubject = Worksheets("Write").Range("J2").Value
' Create hyperlink with onclick event to trigger OpenExcel function
MailBody = "Please click <a href='javascript:void(0);' onclick='OpenExcel()'>here</a> to open Excel and load the CSV file."
' Check if the attachment file exists
If Dir(AttachmentPath) = "" Then
MsgBox "Attachment file not found.", vbExclamation
Exit Sub
End If
' Create Outlook application and email
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
' Create and send email
With OutMail
.To = EmailAddress
.Subject = MailSubject
.HTMLBody = MailBody
.Attachments.Add AttachmentPath
.Send ' Display email to the user
End With
' Clean up
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
' Function to open Excel and load the CSV file
Sub OpenExcel()
' Open Excel file and call LoadCSV subroutine
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Workbooks.Open "O:\SYDDEPT-Finance\Sales\Work Tools - DOS\AU PC.xlsm"
xlApp.Visible = True
xlApp.Run "LoadCSV"
End Sub`
出于安全原因,包括 Outlook 在内的电子邮件客户端会阻止任何脚本在邮件正文中运行。您能做的最好的事情就是插入文件的链接以下载(或打开)。