Vba 查找和删除文本
我用Word编写代码来查找文档中的“[编辑]”链接、断开超链接并删除文本 我想将其调整为在Outlook中运行。我已转到“工具”>“引用”以允许Outlook访问Word对象库,并在“DeleteEditLinks”宏之前插入了以下代码:Vba 查找和删除文本,vba,outlook,ms-word,Vba,Outlook,Ms Word,我用Word编写代码来查找文档中的“[编辑]”链接、断开超链接并删除文本 我想将其调整为在Outlook中运行。我已转到“工具”>“引用”以允许Outlook访问Word对象库,并在“DeleteEditLinks”宏之前插入了以下代码: Dim-Ins作为Outlook.Inspector 将文档设置为Word.Document 模糊词作为词。应用 将所选内容设置为单词。所选内容 Set-Ins=Application.ActiveInspector Set Document=Ins.Word
Dim-Ins作为Outlook.Inspector
将文档设置为Word.Document
模糊词作为词。应用
将所选内容设置为单词。所选内容
Set-Ins=Application.ActiveInspector
Set Document=Ins.WordEditor
Set Word=Document.Application
设置选择=单词。选择
最后的代码如下所示:
Public子目录DeleteEditLinks()
作为Outlook.Inspector的Dim Ins
将文档设置为Word.Document
模糊词作为词。应用
将所选内容设置为单词。所选内容
Set-Ins=Application.ActiveInspector
Set Document=Ins.WordEditor
Set Word=Document.Application
设置选择=单词。选择
字段“字段的尺寸”会断开“[编辑]”链接的超链接,并将其删除
对于ActiveDocument.Fields中的每个字段
如果oField.Type=wdFieldHyperlink,则
如果左(oField.Result,4)=“编辑”,则
断开链接
如果结束
如果结束
下一个
场集=无
暗淡样品
sample=“[edit]”
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
选择。查找
.Text=示例
.Replacement.Text=“”
.Forward=True
.Wrap=wdFindContinue
.Format=False
.MatchCase=False
.MatchWholeWord=False
.MatchWildcards=False
.MatchSoundsLike=False
.MatchAllWordForms=False
以
Selection.Find.Execute Replace:=wdReplaceAll
端接头
我如何调整它以在Outlook电子邮件的文本上运行?我不确定Fields object在Outlook中必须如何显示,也不确定您正试图实现什么,但是!。。我在您的代码中发现了一些基本问题,因此,我的解决方案可能会有所帮助
- 您在中引用ActiveDocument,因为它是Outlook对象集合的一部分。事实并非如此,因此您需要引用从inspector正确创建的文档对象。选择也是如此
- 我使用了后期绑定(Dim of ield As Object),不确定使用早期绑定和“tools>references”选项时是否也会遇到这种问题,但单词常量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
它如何“不起作用”?当然,乍一看,您需要将
ActiveDocument
替换为Document
(尽管我建议您为单词
选择不同的可变名称,以确保VBA不会将它们与关键字混淆)。除此之外,您还需要告诉我们您遇到了哪些错误,在哪些行上,或者描述运行代码的期望和实际结果。但这并不管用,这正是我所需要的,我知道我哪里出了问题。很抱歉,上面没有提供足够的信息,但您已经帮了很大的忙。非常感谢!