vba - 查找和删除文本
问题描述
我在 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 电子邮件的文本上运行?
解决方案
我不确定 Fields 对象必须如何出现在 Outlook 中,以及您想要实现的目标,但是!.. 我在您的代码中发现了基本问题,因此,我的解决方案可能会有所帮助。
- 您在其中引用 ActiveDocument,因为它是 Outlook 对象集合的一部分。它不是,所以你需要参考你从检查器中正确创建的 Document 对象。与选择相同。
- 我使用了后期绑定(Dim oField As Object),不确定是否使用早期绑定和“工具>引用”选项也有这个问题,但是单词常量 wdFindContinue 未被识别,所以我使用了它们的值(只是用谷歌搜索)。
因此,如果在您的目标电子邮件中以某种方式存在字段 - 您下面的更新代码应该可以工作......如果不是这样,请写。
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
推荐阅读
- google-earth-engine - 如何在 Earth Engine python api 中使用一些 OpenCv 代码
- json - 在 Golang 中读取大键:值 JSON 文件
- zsh - 如何让 zsh 插件有色手册页适用于 git?
- gcloud - gcloud 列出托管组中按创建时间排序的实例
- sql-server - 将数据库导出到 .bacpac,同时使用 SqlPackage 排除某些表
- r - 如何一次处理3900+地区的月度时间序列数据
- jwt - ejabberd 和 JWT
- javascript - 在使用 JS 的按钮包装器之前和之后的元素时应用 css
- javascript - 将 dhis2 地图嵌入到 javascript Web 应用程序中
- javascript - React:验证给出不想要的结果