我找到了一个为Excel编写的宏,它具有我需要的确切功能,但我不知道如何将其转换为在Word中使用 - 我对脚本很新,我熟悉的唯一语言/语法是我在使用Procomm Plus时学习的Aspect脚本语言。我在这里找到了Excel宏:
Find specific text and delete three rows above it
Sub Delete()
Dim find As String: find = "TOT"
Dim rng As Range
Set rng = Sheets("Sheet1").Cells.find(What:=find, After:=Sheets("Sheet1").Cells(1, 1), LookIn:=xlValues, Lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True)
If Not rng Is Nothing Then
rng.EntireRow.Delete
rng.Offset(-1).EntireRow.Delete
rng.Offset(-2).EntireRow.Delete
rng.Offset(-3).EntireRow.Delete
End If
End Sub
我需要宏来查找文本“在'^'标记处检测到%输入无效”然后将其删除,以及该短语上方的两行:
Router#show environment all ;(let's call this "line -2")
^ ;(and this would be "line -1")
% Invalid input detected at ‘^’ marker ;(and "line 0")
这是Cisco路由器的控制台输出(复制/粘贴到Word),短语“在'^'标记处检测到的%无效输入”始终相同,但它上面的两行可能会有所不同,具体取决于我拥有的Cisco命令用过的还没有被认出来。
我认为解决方案可能很简单,但我被卡住了!
我找到了另一个执行类似功能的Word宏 - 它查找一个关键短语并删除包含该短语的行但是,我不知道如何扩展它以包含前两行文本:
Sub InvalidInput()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "Invalid input detected"
While .Execute
oRng.Paragraphs(1).Range.Delete
Wend
End With
End Sub
有人能够建议一些可以解决我的问题的编辑吗?非常感谢你提前。
假设您的“行”是单独的段落,请尝试:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[!^13]@^13[!^13]@^13% Invalid input detected at ‘^94’ marker^13"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End With
Application.ScreenUpdating = True
End Sub
否则,您必须提供更有意义的描述,描述您的“线条”。
这是一个远景,但在Word中的这个宏...
Public Sub RemoveUnwantedParagraphs()
Dim objParagraph As Paragraph, arrToRemove() As Long, lngIndex As Long
Dim i As Long, x As Long, strSearchFor As String
strSearchFor = "% Invalid input detected at '^' marker"
lngIndex = -1
For i = 1 To ThisDocument.Paragraphs.Count
Set objParagraph = ThisDocument.Paragraphs(i)
If Left(objParagraph.Range.Text, Len(strSearchFor)) = strSearchFor Then
For x = 2 To 0 Step -1
lngIndex = lngIndex + 1
ReDim Preserve arrToRemove(lngIndex)
arrToRemove(lngIndex) = i - x
Next
End If
Next
On Error GoTo ExitGracefully
For i = UBound(arrToRemove) To 0 Step -1
ThisDocument.Paragraphs(arrToRemove(i)).Range.Delete
Next
ExitGracefully:
End Sub
...在你运行它之前做一个忙,备份你的文件!!!
我不保证它不会无意中填满整个事物。我在Word中进行了有限的开发,但上面的代码对我有用。
唯一需要注意的是,如果您要搜索的文本包含在第1行或第2行,则可能会中断。我认为这不是预期的,但不能满足它。