Regex ct+1,19)=橙色。文本 橙色,折叠,折叠 温德 以 下一个currowshtsearchitem currowshtextract=0 "大厦", 对于currowshtsearchitem=7到7 设置橙色=oDoc.范围 用橘子,找到 .Text=shtSearchItem.Cells(currowshtsarchitem,1).Text .MatchCase=False .MatchWholeWord=True While.Execute=True currowshtextract=currowshtextract+1 单元格(currowshtextract+1,9)=橙色.Text 橙色,折叠,折叠 温德 以 下一个currowshtsearchitem currowshtextract=0 “地板 对于currowshtsearchitem=8到8 设置橙色=oDoc.范围 用橘子,找到 .Text=shtSearchItem.Cells(currowshtsarchitem,1).Text .MatchCase=False .MatchWholeWord=True While.Execute=True currowshtextract=currowshtextract+1 单元格(currowshtextract+1,12)=橙色.Text 橙色,折叠,折叠 温德 以 下一个currowshtsearchitem currowshtextract=0 “房间# 对于currowshtsearchitem=9到9 设置橙色=oDoc.范围 用橘子,找到 .Text=shtSearchItem.Cells(currowshtsarchitem,1).Text .MatchCase=False .MatchWholeWord=True While.Execute=True currowshtextract=currowshtextract+1 单元格(currowshtextract+1,13)=橙色.Text 橙色,折叠,折叠 温德 以 下一个currowshtsearchitem currowshtextract=0 “天线 对于currowshtsearchitem=10到10 设置橙色=oDoc.范围 用橘子,找到 .Text=shtSearchItem.Cells(currowshtsarchitem,1).Text .MatchCase=False .MatchWholeWord=True While.Execute=True currowshtextract=currowshtextract+1 单元格(currowshtextract+1,21)=橙色.Text 橙色,折叠,折叠 温德 以 下一个currowshtsearchitem currowshtextract=0 使用oDoc.Content.Find .Text=“*安装说明” .Replacement.Text=“安装说明” .Forward=True .Wrap=wdFindContinue .Format=False .MatchCase=False .MatchWholeWord=False .MatchAllWordForms=False .MatchSoundsLike=False .MatchWildcards=True .Execute Replace:=wdReplaceAll 以 奥多克,救命 "装置", 对于currowshtsearchitem=5到5 设置橙色=oDoc.范围 用橘子,找到 .Text=shtSearchItem.Cells(currowshtsarchitem,1).Text .MatchCase=False .MatchWholeWord=True 而oRange.Find.Execute=True 橙色。选择 myPara=oDoc.Range(0,oWord.Selection.Parations(1).Range.End).Parations.Count currowshtextract=currowshtextract+1 shtExtract.Cells(currowshtextract+1,14)=左(oDoc.段落(myPara).范围,Len(oDoc.段落(myPara).范围)-1) 橙色,折叠,折叠 温德 以 下一个currowshtsearchitem 单元格。替换内容:=“安装说明:”,替换:=”,请看_ :=xlPart,SearchOrder:=xlByRows,MatchCase:=False,SearchFormat:=False_ ReplaceFormat:=False 列(“U:U”)。选择 选择。替换内容:=“m*”,替换:=“m”,查看:=xlPart_ SearchOrder:=xlByRows,MatchCase:=False,SearchFormat:=False_ ReplaceFormat:=False 如果你没有笔的话 奥沃德,退出 如果结束 如果你没有笔的话 奥沃德,退出 如果结束 '释放对象引用 设置oWord=无 设置oDoc=无 出口接头 错误处理程序: MsgBox“Word导致了一个问题。”&Err.Description,vbCritical,“Error:”&Err.Number 如果你没有笔的话 奥沃德,退出 如果结束 端接头
假设您的活动工作表在A列中有型号、天线等,并且找到的详细信息将输出到B列:Regex ct+1,19)=橙色。文本 橙色,折叠,折叠 温德 以 下一个currowshtsearchitem currowshtextract=0 "大厦", 对于currowshtsearchitem=7到7 设置橙色=oDoc.范围 用橘子,找到 .Text=shtSearchItem.Cells(currowshtsarchitem,1).Text .MatchCase=False .MatchWholeWord=True While.Execute=True currowshtextract=currowshtextract+1 单元格(currowshtextract+1,9)=橙色.Text 橙色,折叠,折叠 温德 以 下一个currowshtsearchitem currowshtextract=0 “地板 对于currowshtsearchitem=8到8 设置橙色=oDoc.范围 用橘子,找到 .Text=shtSearchItem.Cells(currowshtsarchitem,1).Text .MatchCase=False .MatchWholeWord=True While.Execute=True currowshtextract=currowshtextract+1 单元格(currowshtextract+1,12)=橙色.Text 橙色,折叠,折叠 温德 以 下一个currowshtsearchitem currowshtextract=0 “房间# 对于currowshtsearchitem=9到9 设置橙色=oDoc.范围 用橘子,找到 .Text=shtSearchItem.Cells(currowshtsarchitem,1).Text .MatchCase=False .MatchWholeWord=True While.Execute=True currowshtextract=currowshtextract+1 单元格(currowshtextract+1,13)=橙色.Text 橙色,折叠,折叠 温德 以 下一个currowshtsearchitem currowshtextract=0 “天线 对于currowshtsearchitem=10到10 设置橙色=oDoc.范围 用橘子,找到 .Text=shtSearchItem.Cells(currowshtsarchitem,1).Text .MatchCase=False .MatchWholeWord=True While.Execute=True currowshtextract=currowshtextract+1 单元格(currowshtextract+1,21)=橙色.Text 橙色,折叠,折叠 温德 以 下一个currowshtsearchitem currowshtextract=0 使用oDoc.Content.Find .Text=“*安装说明” .Replacement.Text=“安装说明” .Forward=True .Wrap=wdFindContinue .Format=False .MatchCase=False .MatchWholeWord=False .MatchAllWordForms=False .MatchSoundsLike=False .MatchWildcards=True .Execute Replace:=wdReplaceAll 以 奥多克,救命 "装置", 对于currowshtsearchitem=5到5 设置橙色=oDoc.范围 用橘子,找到 .Text=shtSearchItem.Cells(currowshtsarchitem,1).Text .MatchCase=False .MatchWholeWord=True 而oRange.Find.Execute=True 橙色。选择 myPara=oDoc.Range(0,oWord.Selection.Parations(1).Range.End).Parations.Count currowshtextract=currowshtextract+1 shtExtract.Cells(currowshtextract+1,14)=左(oDoc.段落(myPara).范围,Len(oDoc.段落(myPara).范围)-1) 橙色,折叠,折叠 温德 以 下一个currowshtsearchitem 单元格。替换内容:=“安装说明:”,替换:=”,请看_ :=xlPart,SearchOrder:=xlByRows,MatchCase:=False,SearchFormat:=False_ ReplaceFormat:=False 列(“U:U”)。选择 选择。替换内容:=“m*”,替换:=“m”,查看:=xlPart_ SearchOrder:=xlByRows,MatchCase:=False,SearchFormat:=False_ ReplaceFormat:=False 如果你没有笔的话 奥沃德,退出 如果结束 如果你没有笔的话 奥沃德,退出 如果结束 '释放对象引用 设置oWord=无 设置oDoc=无 出口接头 错误处理程序: MsgBox“Word导致了一个问题。”&Err.Description,vbCritical,“Error:”&Err.Number 如果你没有笔的话 奥沃德,退出 如果结束 端接头,regex,excel,vba,ms-word,Regex,Excel,Vba,Ms Word,假设您的活动工作表在A列中有型号、天线等,并且找到的详细信息将输出到B列: Sub GetDocData() Dim xlSheet As Worksheet, r As Long Dim WordApp As New Word.Application, WordDoc As Word.Document Set xlSheet = ActiveSheet WordApp.Visible = True With WordApp .Visible = False With .Dialogs(
Sub GetDocData()
Dim xlSheet As Worksheet, r As Long
Dim WordApp As New Word.Application, WordDoc As Word.Document
Set xlSheet = ActiveSheet
WordApp.Visible = True
With WordApp
.Visible = False
With .Dialogs(wdDialogFileOpen)
If .Show = -1 Then
Set WordDoc = WordApp.ActiveDocument
End If
End With
With WordDoc
With .Range
For r = 1 To xlSheet.UsedRange.Cells.SpecialCells(xlCellTypeLastCell).Row
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Text = ""
.Forward = True
.Format = False
.Wrap = wdFindContinue
.Text = xlSheet.Range("A" & r) & ":"
.Execute
End With
If .Find.Found = True Then
.Collapse wdCollapseEnd
.End = .Paragraphs.First.Range.End - 1
xlSheet.Range("B" & r).Value = .Text
End If
Next
End With
End With
End With
End Sub
看看正则表达式。这看起来很有道理。有趣的代码。我试着运行它,但似乎什么也没发生。我可能没有按照您的假设正确设置excel表。现在,根据excel电子表格的设置方式,我将输出放在不同的列中。例如AP名称进入A列或AP模型进入T列。要查找的搜索词位于另一个工作表上,并在查找、复制然后粘贴到相应列之前在For循环中引用。有没有一种方法可以简单地修改oRange.MoveEnd语句,使其直接到达行的末尾?我一直在尝试这样修改它:oRange.MoveEnd Unit:=wdLine,Count:=1,但每当我这样做时,我都会得到“Error 4120:Bad Parameter”,您是否阅读了我答案中代码之前的段落?您没有给出任何迹象表明Excel数据是以第一列以外的任何形式组织的,甚至您的“.Cells(CurrRowShtSearchItem,1)”也暗示了这一点。显然,如果工作簿结构不同,则需要相应地修改代码。要使用列而不是行,我发布的代码中只有三行需要编辑。不需要额外的代码。
Sub LocateSearchItem()
Dim shtSearchItem As Worksheet
Dim shtExtract As Worksheet
Dim oWord As Word.Application
Dim WordNotOpen As Boolean
Dim oDoc As Word.Document
Dim fd As Office.FileDialog
Dim FilePath As String
Dim oRange As Word.Range
Dim LastRow As Long ' last row with data in shtSearchItem
Dim CurrRowShtSearchItem As Long ' current row in shtSearchItem
Dim CurrRowShtExtract As Long ' current row in shtExtract
Dim myPara As Long
On Error Resume Next
Set oWord = GetObject(, "Word.Application")
If Err Then
Set oWord = New Word.Application
WordNotOpen = True
End If
On Error GoTo Err_Handler
oWord.Visible = True
oWord.Activate
Set fd = Application.FileDialog(msoFileDialogOpen)
With fd
.Filters.Add "Word Files", "*.docx", 1
.Title = "Choose a Word File"
.AllowMultiSelect = False
If .Show = True Then
FilePath = .SelectedItems(1)
End If
End With
Set oDoc = oWord.Documents.Open(FilePath)
With oDoc.Content.Find
.Text = "Notes"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
With oDoc.Content.Find
.Text = "^p"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
With oDoc.Content.Find
.Text = "^l"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
oDoc.Save
Set shtSearchItem = ThisWorkbook.Worksheets(7)
If ThisWorkbook.Worksheets.Count < 2 Then
ThisWorkbook.Worksheets.Add After:=shtSearchItem
End If
Set shtExtract = ThisWorkbook.Worksheets(5)
' AP name
For CurrRowShtSearchItem = 2 To 2
Set oRange = oDoc.Range
With oRange.Find
.Text = shtSearchItem.Cells(CurrRowShtSearchItem, 1).Text
.Font.Name = "Helvetica"
.MatchCase = False
.MatchWholeWord = True
While .Execute = True
oRange.MoveEnd wdCharacter, 5
CurrRowShtExtract = CurrRowShtExtract + 1
shtExtract.Cells(CurrRowShtExtract + 1, 1) = oRange.Text
oRange.Collapse wdCollapseEnd
Wend
End With
Next CurrRowShtSearchItem
CurrRowShtExtract = 0
'AP model
For CurrRowShtSearchItem = 3 To 3
Set oRange = oDoc.Range
With oRange.Find
.Text = shtSearchItem.Cells(CurrRowShtSearchItem, 1).Text
.MatchCase = False
.MatchWholeWord = True
While .Execute = True
oRange.MoveEnd wdCharacter, 6
CurrRowShtExtract = CurrRowShtExtract + 1
shtExtract.Cells(CurrRowShtExtract + 1, 20) = oRange.Text
oRange.Collapse wdCollapseEnd
Wend
End With
Next CurrRowShtSearchItem
CurrRowShtExtract = 0
'Mounting Height
For CurrRowShtSearchItem = 4 To 4
Set oRange = oDoc.Range
With oRange.Find
.Text = shtSearchItem.Cells(CurrRowShtSearchItem, 1).Text
.MatchCase = False
.MatchWholeWord = True
While .Execute = True
oRange.MoveStart wdCharacter, 16
oRange.MoveEnd wdCharacter, 11
CurrRowShtExtract = CurrRowShtExtract + 1
shtExtract.Cells(CurrRowShtExtract + 1, 22) = oRange.Text
oRange.Collapse wdCollapseEnd
Wend
End With
Next CurrRowShtSearchItem
CurrRowShtExtract = 0
'Clip Type
For CurrRowShtSearchItem = 6 To 6
Set oRange = oDoc.Range
With oRange.Find
.Text = shtSearchItem.Cells(CurrRowShtSearchItem, 1).Text
.MatchCase = False
.MatchWholeWord = True
While .Execute = True
CurrRowShtExtract = CurrRowShtExtract + 1
shtExtract.Cells(CurrRowShtExtract + 1, 19) = oRange.Text
oRange.Collapse wdCollapseEnd
Wend
End With
Next CurrRowShtSearchItem
CurrRowShtExtract = 0
'Building
For CurrRowShtSearchItem = 7 To 7
Set oRange = oDoc.Range
With oRange.Find
.Text = shtSearchItem.Cells(CurrRowShtSearchItem, 1).Text
.MatchCase = False
.MatchWholeWord = True
While .Execute = True
CurrRowShtExtract = CurrRowShtExtract + 1
shtExtract.Cells(CurrRowShtExtract + 1, 9) = oRange.Text
oRange.Collapse wdCollapseEnd
Wend
End With
Next CurrRowShtSearchItem
CurrRowShtExtract = 0
'Floor
For CurrRowShtSearchItem = 8 To 8
Set oRange = oDoc.Range
With oRange.Find
.Text = shtSearchItem.Cells(CurrRowShtSearchItem, 1).Text
.MatchCase = False
.MatchWholeWord = True
While .Execute = True
CurrRowShtExtract = CurrRowShtExtract + 1
shtExtract.Cells(CurrRowShtExtract + 1, 12) = oRange.Text
oRange.Collapse wdCollapseEnd
Wend
End With
Next CurrRowShtSearchItem
CurrRowShtExtract = 0
'Room #
For CurrRowShtSearchItem = 9 To 9
Set oRange = oDoc.Range
With oRange.Find
.Text = shtSearchItem.Cells(CurrRowShtSearchItem, 1).Text
.MatchCase = False
.MatchWholeWord = True
While .Execute = True
CurrRowShtExtract = CurrRowShtExtract + 1
shtExtract.Cells(CurrRowShtExtract + 1, 13) = oRange.Text
oRange.Collapse wdCollapseEnd
Wend
End With
Next CurrRowShtSearchItem
CurrRowShtExtract = 0
'Antenna
For CurrRowShtSearchItem = 10 To 10
Set oRange = oDoc.Range
With oRange.Find
.Text = shtSearchItem.Cells(CurrRowShtSearchItem, 1).Text
.MatchCase = False
.MatchWholeWord = True
While .Execute = True
CurrRowShtExtract = CurrRowShtExtract + 1
shtExtract.Cells(CurrRowShtExtract + 1, 21) = oRange.Text
oRange.Collapse wdCollapseEnd
Wend
End With
Next CurrRowShtSearchItem
CurrRowShtExtract = 0
With oDoc.Content.Find
.Text = "*Installation Instructions"
.Replacement.Text = "Installation Instructions"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
oDoc.Save
'Installation
For CurrRowShtSearchItem = 5 To 5
Set oRange = oDoc.Range
With oRange.Find
.Text = shtSearchItem.Cells(CurrRowShtSearchItem, 1).Text
.MatchCase = False
.MatchWholeWord = True
While oRange.Find.Execute = True
oRange.Select
myPara = oDoc.Range(0, oWord.Selection.Paragraphs(1).Range.End).Paragraphs.Count
CurrRowShtExtract = CurrRowShtExtract + 1
shtExtract.Cells(CurrRowShtExtract + 1, 14) = Left(oDoc.Paragraphs(myPara).Range, Len(oDoc.Paragraphs(myPara).Range) - 1)
oRange.Collapse wdCollapseEnd
Wend
End With
Next CurrRowShtSearchItem
Cells.Replace What:="Installation Instructions:", Replacement:="", LookAt _
:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Columns("U:U").Select
Selection.Replace What:="m*", Replacement:="m", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
If WordNotOpen Then
oWord.Quit
End If
If WordNotOpen Then
oWord.Quit
End If
'Release object references
Set oWord = Nothing
Set oDoc = Nothing
Exit Sub
Err_Handler:
MsgBox "Word caused a problem. " & Err.Description, vbCritical, "Error: " & Err.Number
If WordNotOpen Then
oWord.Quit
End If
End Sub
Sub GetDocData()
Dim xlSheet As Worksheet, r As Long
Dim WordApp As New Word.Application, WordDoc As Word.Document
Set xlSheet = ActiveSheet
WordApp.Visible = True
With WordApp
.Visible = False
With .Dialogs(wdDialogFileOpen)
If .Show = -1 Then
Set WordDoc = WordApp.ActiveDocument
End If
End With
With WordDoc
With .Range
For r = 1 To xlSheet.UsedRange.Cells.SpecialCells(xlCellTypeLastCell).Row
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Text = ""
.Forward = True
.Format = False
.Wrap = wdFindContinue
.Text = xlSheet.Range("A" & r) & ":"
.Execute
End With
If .Find.Found = True Then
.Collapse wdCollapseEnd
.End = .Paragraphs.First.Range.End - 1
xlSheet.Range("B" & r).Value = .Text
End If
Next
End With
End With
End With
End Sub