问题
Assume I have a list of random numbers like 2,4,6. After searching for 2 I want word to copy number 4 and find 4 in the doc. And then the next number 6. I got everything I need but how to paste the copied number in Find dialog box.
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "2"
.Replacement.Text = Selection.Characters
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
After copying "2" I want the word to search for "2" in other document. And should return back to this sheet and go for next number which is "4" and loop. Actually my macro finds "2" in the source doc (where "2" is question number) copies the question following "2.", then pastes in other doc and returns to copy "4". I had everything set but I could not copy "4" and paste "4" in 'find dialog' to copy 4th question,
The source doc looks like this and those on the left are the number I need to find
回答1:
The following code sample demonstrates how this can be done. There's a lot to take in...
Create objects for the two documents. Since the question doesn't provide context, it's assumed the documents will be opened. The code loops through the cells in the table and, if the cell is not empty, picks up the content. This is then used in the Find.
Since no context is provided in the question, Execute
is missing as well as perhaps needing to reset the search range. But this does show how to get each number from the table cells and use them in Word's Find, as stated in the question.
Dim docSource as Word.Document
Dim docTarget as Word.Document
Dim tbl as Word.Table, cel as Word.Cell
Dim rngCell as Word.Range, rngTarget as Word.Range
Dim searchText as String, lenText as Long
Set docSource = Documents.Open("path to document with random numbers")
Set docTarget = Documents.Open("path to document to be searched")
Set tbl = docSource.Tables(1)
Set rngTarget = docTarget.Content
For Each cel in tbl.Range.Cells
searchText = cel.Range.Text
lenText = Len(searchText)
If lenText > 1 Then 'If cell is not "empty"
searchText = Mid(searchText, 1, lenText - 2) 'remove cell structures
With rngTarget.Find
.Replacement.ClearFormatting
.Text = searchText
.Replacement.Text = Selection.Characters
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
End If
Next
来源:https://stackoverflow.com/questions/57395627/paste-the-copied-number-in-find-dialog-box