【问题标题】:Outlook VBA add hyperlink of chosen file in dialogOutlook VBA 在对话框中添加所选文件的超链接
【发布时间】:2020-06-29 15:31:22
【问题描述】:

我正在尝试在我的 Outlook 中添加功能(使用 VBA,我猜是最简单的),以添加一个简单的文件对话框,该对话框采用所选文件的路径并将它们作为超链接添加到电子邮件正文中。

这样做的目的是让同事之间共享网络文件,而不是将它们附加到电子邮件中,但同样容易做到。

到目前为止,这是我的代码,我什至无法打开对话框,而且我已经很好地尝试获取 COMDLG32.ocx,到目前为止我似乎无法进行任何工作。

Sub Main2()

Dim CDLG As Object
Set CDLG = CreateObject("MSComDlg.CommonDialog")
With CDLG
.DialogTitle = "Get me a File!"
.Filter = _
  "Documents|*.doc|Templates|*.dot|Text Files|*.txt"
.ShowOpen
MsgBox .FileName
End With
Set CDLG = Nothing

End Sub

先谢谢了,希望有人能告诉我这是怎么做的!

只为那些需要它的人;操作系统 Windows 10,Office 2010 H&B(是的,我知道它已经过时了:))

【问题讨论】:

  • 我在 Outlook 2010 上测试了您的代码,它可以正常工作,文件对话框按预期打开,并且 msgbox 显示任何选定文件的路径。因此,请提供有关您的问题的更多信息
  • 我收到运行时错误-2147221005 (800401f3),除非您安装了 Visual Studio...?这可能就是您可以加载对话框的原因...
  • 确实我已经安装了VS。我正在从办公室回来的路上。如果您没有收到有效的答复,我明天会查看您的问题。
  • @niton 是的,我意识到了这一点,但我希望通过文件对话框将其自动化......如果你明白我的意思

标签: vba outlook


【解决方案1】:

似乎没有直接的方法可以在Outlook 2010 VBA 中打开FileDialog

以下宏(受related post 启发)利用Excel 来规避这一点:

Public Function promptFileName(title As String, filter As String) As String
    '  requires project reference to "Microsoft Excel 14.0 Object Library"
    Dim xlObj As Excel.Application
    Dim fd As Office.FileDialog
    Dim name As String
    Dim vItem As Variant
    Dim filterArray() As String
    Dim i As Integer

    Set xlObj = New Excel.Application
    xlObj.Visible = False
    Set fd = xlObj.Application.FileDialog(msoFileDialogOpen)

    name = ""
    With fd
        .title = title
        .ButtonName = "Ok"
        .Filters.Clear
        filterArray = Split(filter, "|")

        For i = LBound(filterArray) To UBound(filterArray) - 1 Step 2
            .Filters.Add filterArray(i), filterArray(i + 1), 1 + i \ 2
        Next i

        If .Show = -1 Then
            For Each vItem In .SelectedItems
                name = vItem
                Exit For
            Next
        End If
    End With
    xlObj.Quit
    Set xlObj = Nothing
    promptFileName = name
End Function

Private Sub testPromptFile
    Dim name as String
    name = promptFileName("a test", "Text Files (*.txt)|*.txt|All Files (*.*)|*.*")
    MsgBox name
End Sub

Outlook 2013 及更高版本为此目的提供 Office.FileDialog 类。

【讨论】:

  • 谢谢 - 这似乎是一个解决方案。但是,当我调用它时,它返回Argument not optional,我添加了引用,并尝试将其称为子 - 尚未尝试添加超链接,因为我认为它使用 Word 编辑器... :(
  • 看看我回答中的测试电话。这应该可以帮助您入门。
  • 像梦一样工作 - 现在开始处理超链接部分......感谢您的帮助,@Axel
【解决方案2】:

您可以使用 Outlook VBA 按一个按钮。

Sub ExecuteMso_strId()

Dim objItem As Object
Dim strId As String

' Text appears when hovering over icon
'  when adding buttons to a Quick Access toolbar or a ribbon
strId = "HyperlinkInsert"

On Error Resume Next
Set objItem = ActiveInspector.currentItem
On Error GoTo 0

If Not objItem Is Nothing Then
    ActiveInspector.CommandBars.ExecuteMso (strId)
Else
    ActiveExplorer.CommandBars.ExecuteMso (strId)
End If

End Sub

使用它,您无法像使用 Excel 那样访问参数。

【讨论】:

    猜你喜欢
    • 2011-01-28
    • 2021-11-28
    • 2011-12-08
    • 2019-02-07
    • 2022-01-17
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    相关资源
    最近更新 更多