求WORD VBA一次性删除含关键字的行
5个回答
展开全部
Sub Macro4()
Dim i As Paragraph
Application.ScreenUpdating = False
For Each i In ActiveDocument.Paragraphs
Selection.Find.ClearFormatting
Selection.Find.Text = "所含的关键字"
Selection.Find.Execute
If Selection.Text <> "" Then
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
End If
Next
End Sub
把代码中所含的关键字几个字改成你想要的就可以了
Dim i As Paragraph
Application.ScreenUpdating = False
For Each i In ActiveDocument.Paragraphs
Selection.Find.ClearFormatting
Selection.Find.Text = "所含的关键字"
Selection.Find.Execute
If Selection.Text <> "" Then
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
End If
Next
End Sub
把代码中所含的关键字几个字改成你想要的就可以了
追问
谢谢您的代码执行非常快 能否帮偶改成是查找关键字然后执行 不用每次把关键字放在代码中进行修改后再操作 劳烦啦~
追答
Sub text()
Dim i As Paragraph
n = InputBox("请输入删除内容")
Application.ScreenUpdating = False
For Each i In ActiveDocument.Paragraphs
Selection.Find.ClearFormatting
Selection.Find.text = n
Selection.Find.Execute
If Selection.text = n Then
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
End If
Next
End Sub
刚才代码还有点问题,现在应该好了
展开全部
把word文件(支持多文件)和包含VBA程序的EXCEL放在同一文件夹下,
更换这一句中"fffad"为你指的关键词 If InStr(1, docRange, "fffad") > 0 Then docRange.Delete
VBA程序如下:
Sub test()
Dim FN As String, In_Count, col As Long
Dim myDOC, myApp
Dim docApp As Word.Application
Dim docRange As Word.Range
Set myApp = CreateObject("Word.Application")
myApp.Visible = False
myApp.DisplayAlerts = wdAlertsNone
FN = Dir(ThisWorkbook.Path & "\*.doc")
Do Until FN = ""
FN = ThisWorkbook.Path & "\" & FN
Set myDOC = myApp.Documents.Open(FN)
In_Count = In_Count + 1
col = myApp.ActiveDocument.Paragraphs.Count
For i = 1 To col
Application.ScreenUpdating = False
With myApp.ActiveDocument
On Error Resume Next
Set docRange = .Paragraphs(i).Range
If InStr(1, docRange, "fffad") > 0 Then docRange.Delete
End With
Next i
myApp.ActiveDocument.Close True
FN = Dir()
Loop
myApp.Quit
Set docRange = Nothing
Set myApp = Nothing
End Sub
更换这一句中"fffad"为你指的关键词 If InStr(1, docRange, "fffad") > 0 Then docRange.Delete
VBA程序如下:
Sub test()
Dim FN As String, In_Count, col As Long
Dim myDOC, myApp
Dim docApp As Word.Application
Dim docRange As Word.Range
Set myApp = CreateObject("Word.Application")
myApp.Visible = False
myApp.DisplayAlerts = wdAlertsNone
FN = Dir(ThisWorkbook.Path & "\*.doc")
Do Until FN = ""
FN = ThisWorkbook.Path & "\" & FN
Set myDOC = myApp.Documents.Open(FN)
In_Count = In_Count + 1
col = myApp.ActiveDocument.Paragraphs.Count
For i = 1 To col
Application.ScreenUpdating = False
With myApp.ActiveDocument
On Error Resume Next
Set docRange = .Paragraphs(i).Range
If InStr(1, docRange, "fffad") > 0 Then docRange.Delete
End With
Next i
myApp.ActiveDocument.Close True
FN = Dir()
Loop
myApp.Quit
Set docRange = Nothing
Set myApp = Nothing
End Sub
追问
FN = Dir(ThisWorkbook.Path & "\*.doc")
以上这句运行错误’424’ 通不过
追答
我调试了没有错误啊,VBA所在的EXCEL要先保存一下,并和word放在同一个文件夹内。
因为只有保存了,ThisWorkbook.Path 才生效
有问题再Hi我好了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
请将以下代码复制贴入WORD VBA宏中 选择运行即可 应该是你想要的代码
Sub 删除查找含关键词的行()
Dim KeyWord As String
KeyWord = InputBox("请输入关键词(词长不限,中英均可):", "关键词设置", "")
If KeyWord = "" Then Exit Sub
Selection.HomeKey wdStory
Do
With Selection.Find
.Text = KeyWord
.Wrap = wdFindStop
.Forward = True
End With
If Selection.Find.Execute = False Then Exit Do
Selection.Paragraphs(1).Range.Select
If (Left(Selection.Text, Len(KeyWord))) = KeyWord Then
Selection.Paragraphs(1).Range.delete
Selection.HomeKey unit:=wdLine
End If
Loop
Selection.HomeKey unit:=wdStory
End Sub
Sub 删除查找含关键词的行()
Dim KeyWord As String
KeyWord = InputBox("请输入关键词(词长不限,中英均可):", "关键词设置", "")
If KeyWord = "" Then Exit Sub
Selection.HomeKey wdStory
Do
With Selection.Find
.Text = KeyWord
.Wrap = wdFindStop
.Forward = True
End With
If Selection.Find.Execute = False Then Exit Do
Selection.Paragraphs(1).Range.Select
If (Left(Selection.Text, Len(KeyWord))) = KeyWord Then
Selection.Paragraphs(1).Range.delete
Selection.HomeKey unit:=wdLine
End If
Loop
Selection.HomeKey unit:=wdStory
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给个具体例子啊
你可以用instr 进行判断
然后用rows(),delete删除行
你可以用instr 进行判断
然后用rows(),delete删除行
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
delete
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询