我有另一个用户遇到的类似问题。 Xidgel解决了这个问题。该代码为他们工作,但似乎不适用于我。我想在Word文档中搜索具有
的任何内容3个字母,“ x”,4个数字。 例如。 ADZx4391
并将其复制到Excel工作表中。
Sub CopyTPNumber()
'Create variables
Dim Word As New Word.Application
Dim WordDoc As New Word.Document
Dim r As Word.Range
Dim Doc_Path As String
Dim WB As Excel.Workbook
Dim WB_Name As String
Doc_Path = "Libraries\Documents\EXPORTCOMMENTS.docx"
Set WordDoc = Word.Documents.Open(Doc_Path)
' Set WordDoc = ActiveDocument
' Create a range to search.
Set r = WordDoc.Content
'Find text and copy it
With r
.Find.ClearFormatting
With .Find
With .Find
.Text = "[A-Z]{3}x[0-9]{4}"
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.Execute
End With
.Copy
End With
'Open excel workbook and paste
WB_Name = Excel.Application.GetOpenFilename(",*.xlsx")
Set WB = Workbooks.Open(WB_Name)
WB.Sheets("Sheet1").Select
Range("AB2").Select
ActiveSheet.Paste
WordDoc.Close
Word.Quit
End Sub
运行代码时,出现未定义的用户定义类型。