我对VBA脚本很新,我试图在Outlook中编写一个宏来读取一个excel文件,该文件在单独的单元格中包含完整路径和文件名,并将它们作为超链接插入电子邮件中。我已经找到了有关如何在Outlook中创建超链接的信息,但似乎无法找到关于如何让Outlook实际从Excel工作表中获取文件路径的任何信息。任何有关正确方向的帮助或指示都将不胜感激。谢谢!
编辑包含一个似乎失败的简单概念证明。错误说
编译错误:用户定义的类型未定义
这是我到目前为止所拥有的。
Sub links()
Dim objExcel As New Excel.Application
Dim exWb As Excel.Workbook
Dim ExcelFileName As String
Dim FilePath As String
ExcelFileName = "C:\links.xlsx"
Set exWb = objExcel.Workbooks.Open(ExcelFileName)
FilePath = exWb.Sheets("Sheet1").Cells(1, 1)
oMsg.TextBody = Chr(34) & FilePath & Chr(34)
End Sub
答案 0 :(得分:0)
在Outlook VBA编辑器中设置对Excel的引用。
Tools | References
Tick Microsoft Excel Object Library
将Option Explict添加到新模块。你会发现这很有帮助。
Tools | Options | Editor tab
Tick Require Variable Declaration
Option Explicit
Sub links()
Dim objExcel As New Excel.Application
Dim exWb As Excel.Workbook
Dim ExcelFileName As String
Dim FilePath As String
Dim oMsg As mailItem
ExcelFileName = "C:\links.xlsx"
Set exWb = objExcel.Workbooks.Open(ExcelFileName)
FilePath = exWb.Sheets("Sheet1").Cells(1, 1)
On Error Resume Next
Set oMsg = ActiveInspector.currentItem
On Error GoTo 0
If oMsg Is Nothing Then
Set oMsg = CreateItem(0)
oMsg.Display
End If
' This adds to existing text.
' Must display first to save a signature
'oMsg.body = Chr(34) & FilePath & Chr(34) & oMsg.body
'or
oMsg.HTMLBody = Chr(34) & FilePath & Chr(34) & oMsg.HTMLBody
ExitRoutine:
Set oMsg = Nothing
Set exWb = Nothing
Set objExcel = Nothing
End Sub