VBAbyMBA
VBAbyMBA

Reputation: 826

How to open a word document and find text in it without showing the document?

From the following code TCN.docx file had opened successfully using

Application.ScreenUpdating = False

instead of

Visible:= False

Sub workonTCN()
Dim odoc As Document
Dim path As String
path = "C:\Users\Bilal\Desktop\TCN.docx"

     Set odoc = Documents.Open(filename:=path)

     Application.ScreenUpdating = False

     Selection.Find.ClearFormatting
     Selection.Find.Font.Bold = True
     With Selection.Find
        .Text = "TI"
        .Forward = True
        .Wrap = wdFindStop
        .Format = True
    End With
    Selection.Find.Execute
If Selection.Find.Found = True Then
    Selection.MoveRight Unit:=wdCell
    Selection.COPY
Else
End If
    Windows("ROUGH").Activate
      odoc.Close wdDoNotSaveChanges
      Selection.PasteAndFormat (wdPasteDefault)
End sub

How do I apply range to find text without Selection.Find?

Upvotes: 0

Views: 2231

Answers (1)

Cindy Meister
Cindy Meister

Reputation: 25663

To use Range.Find instead of Selection.Find:

  1. Declare an object variable for the Range at the beginning of the Sub: Dim rng As Word.Range
  2. Assign the range of the main story of the document to it: Set rng = odoc.Content
  3. Substitute rng for Selection in the rest of your code (except for the last line that does the pasting)

Note that you may be able to remove the line for activating the Window where you want to paste the information.

Upvotes: 2

Related Questions