2016-04-21 28 views
0

從下面的代碼TCN.docx文件已經使用成功打開如何打開word文檔並在其中查找文本而不顯示文檔?

Application.ScreenUpdating = False

代替

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 

我如何申請範圍內找到的文本,而不Selection.Find

+0

'application.screenupdating = false'? – findwindow

+0

'但找到函數不工作,直到我使TCN.Docx可見:= true'你確定嗎?我認爲這應該工作。 – newguy

+0

但我需要tcn.docx可見:= flase –

回答

2

要使用Range.Find代替Selection.Find:

  1. 在小組的開始聲明的範圍的對象變量:Dim rng As Word.Range
  2. 指定該文檔的主要故事的範圍它:Set rng = odoc.Content
  3. 替代rngSelection在你的代碼的其餘部分(除了不粘貼的最後一行)

請注意,您可能可以刪除要在其中粘貼信息的窗口來激活窗口。

+0

我已經完成代碼但是它有一個與rng.MoveRight單元有關的問題:= wdCell' –

+1

我相信你已經收到了對你開始的另一個問題的回答,@ibneAshiq? –