Paste the copied number in Find dialog box

心不动则不痛 提交于 2020-01-05 14:59:43

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!