首页 文章

如何在Outlook中运行word中设计宏?

提问于
浏览
-1

相对较新的编码,所以我感谢你能给我的任何帮助 .

我在Word中编写了一段代码,在文档中找到“[edit]”链接,打破超链接并删除文本 . 它在Word中完美运行,我想调整此宏以在Outlook中运行 . 我已经转到工具>引用以允许Outlook访问Word对象库,并在我的“deleteeditlinks”宏之前插入以下代码:

Dim Ins As Outlook.Inspector
  Dim Document As Word.Document
  Dim Word As Word.Application
  Dim Selection As Word.Selection

  Set Ins = Application.ActiveInspector
  Set Document = Ins.WordEditor
  Set Word = Document.Application
  Set Selection = Word.Selection

最终代码如下所示:

Public Sub DeleteEditLinks()
Dim Ins As Outlook.Inspector
Dim Document As Word.Document
Dim Word As Word.Application
Dim Selection As Word.Selection

Set Ins = Application.ActiveInspector
Set Document = Ins.WordEditor
Set Word = Document.Application
Set Selection = Word.Selection

 Dim oField As Field ' breaks hyperlinks of "[edit]" links, and deletes them
  For Each oField In ActiveDocument.Fields
    If oField.Type = wdFieldHyperlink Then
      If Left(oField.Result, 4) = "edit" Then
        oField.Unlink
      End If
    End If
  Next
  Set oField = Nothing

    Dim sample
    sample = "[edit]"

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = sample
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

但这不起作用 . 如何使其适应Outlook电子邮件的文本?

谢谢您的帮助,

1 回答

  • -1

    我不确定Fields对象必须如何出现在outlook中,以及你想要实现什么样的ecxatly,但是!..我在你的代码中发现了基本问题,所以,我的解决方案可能会有所帮助 .

    • 您在其中引用ActiveDocument,因为它是Outlook对象集合的一部分 . 它不是,因此您需要引用从检查器正确创建的Document对象 . 与选择相同 .

    • 我用后期绑定(DIM oField为对象),不知道是否与早期绑定和"tools>references"选项对你也有这样的烦恼,但字常数wdFindContinue没有得到承认,所以我用数值为他们(只是GOOGLE上搜索他们) .

    所以,如果在您的目标电子邮件中有某些字段 - 下面的更新代码应该有效...如果不是这样,请写 .

    Public Sub DeleteEditLinks()
    
    Dim Ins As Outlook.Inspector
    Dim Document As Object
    Dim oField As Object
    Dim sample As String
    
    Set Ins = Application.ActiveInspector
    Set Document = CreateObject("Word.Document")
    Set Document = Ins.WordEditor
    
    For Each oField In Document.Fields
      If oField.Type = 88 Then
        If Left(oField.Result, 4) = "edit" Then
          oField.Unlink
        End If
      End If
    Next
    Set oField = Nothing
    
    sample = "[edit]"
    
    Document.Application.Selection.Find.ClearFormatting
    Document.Application.Selection.Find.Replacement.ClearFormatting
    
    With Document.Application.Selection.Find
        .Text = sample
        .Replacement.Text = ""
        .Forward = True
        .Wrap = 1
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=2
    End With
    
    
    End Sub
    

相关问题