简体   繁体   English

如何在不显示文档的情况下打开单词文档并在其中查找文本?

[英]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 从以下代码中, TCN.docx文件已成功使用打开

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 ? 我如何应用范围来查找没有Selection.Find文本?

To use Range.Find instead of Selection.Find: 要使用Range.Find而不是Selection.Find:

  1. Declare an object variable for the Range at the beginning of the Sub: Dim rng As Word.Range 在Sub的开头声明Range的对象变量: Dim rng As Word.Range
  2. Assign the range of the main story of the document to it: Set rng = odoc.Content 向其分配文档主要故事的范围: Set rng = odoc.Content
  3. Substitute rng for Selection in the rest of your code (except for the last line that does the pasting) 在其余代码rng代替Selection (最后一行粘贴时除外)

Note that you may be able to remove the line for activating the Window where you want to paste the information. 请注意,您可以删除用于激活要粘贴信息的窗口的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM