everytime I click the button, new word application and the document will open, what I want is open new word application and the document once and do some actions without reopen new word application and the document.
Try the following way .
Code:
Option Explicit
Dim doc As Word.Application
Dim x As String
Private Sub Command1_Click()
doc.Selection.Find.Execute Text1.Text
If doc.Selection.Find.Execute = True Then
x = MsgBox("text box data is available in word doc" & " Do you want to search again", vbYesNo)
If x = vbYes Then
doc.ActiveDocument.Close
Set doc = New Word.Application
doc.Visible = True
doc.Documents.Open "C:\TestProject\myDoc.doc"
doc.Selection.Find.Execute Text1.Text
ElseIf x = vbNo Then
Unload Me
Else
MsgBox ("text box data is not availble in word doc")
End If
End If
End Sub
Private Sub Form_Load()
Set doc = New Word.Application
doc.Visible = True
doc.Documents.Open "C:\TestProject\myDoc.doc"
End Sub