Hi,
I would like to know if a Created Object can be
destroyed or released.
Set appWord = New Word.Application
'
'
'
(I need something here that would
make sure that there is no 'instance'
of WORD Application)
Printable View
Hi,
I would like to know if a Created Object can be
destroyed or released.
Set appWord = New Word.Application
'
'
'
(I need something here that would
make sure that there is no 'instance'
of WORD Application)
Set it to Nothing
Code:Set appWord = Nothing
I am applying FORMATTING to a Word document. The
first time thru the procedure everything goes ok.
But, the second time I get an Automation Error.
I always have to re-run the application so that I do
not get the automation error.
Help!
Does the above method help?
No, but it helped me narrow down the problem. Thank you.
I do not think the problem is from creating an object. Would it help if I post the code. It is not long.
code always helps...:D
The procedure below inserts a Text file (MyFile.txt) into
a Word document (MyFile.doc), then it searches for the
word "Status" in "MyFile.doc" and re-formats the word!
Private Sub Command1_Click()
Dim appWord As Word.Application
Dim MyDoc As Document
Dim strDoc As String
If appWord Is Nothing Then
Set appWord = New Word.Application
Else
Set appWord = GetObject(, "Word.Application")
End If
With appWord
.WindowState = wdWindowStateNormal
.Visible = True
.Documents.Add
strDoc = .ActiveDocument.Name
Set MyDoc = .Documents(strDoc)
End With
With MyDoc
.Range.InsertFile App.Path & "\" & "MyFile.txt"
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Status"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Font.Size = 12
Selection.Font.Name = "Arial Narrow"
Selection.Font.Bold = wdToggle
If Selection.Font.Underline = wdUnderlineNone Then
Selection.Font.Underline = wdUnderlineSingle
Else
Selection.Font.Underline = wdUnderlineNone
End If
Selection.Font.ColorIndex = wdRed
End With
MyDoc.SaveAs App.Path & "\" & "MyFile.doc"
Set appWord = Nothing
End Sub
Selection.Find.ClearFormatting
What does this do?
It serches for "Status"
finds it
Make it font size 12
underline it
change its color to red
It is doing what iit is supposed to do, but only
once.
if I hit the command1 button a second time, only then
will I get the Automation Error!
Yea, I've loaded it up and unfortunately I can't get past it either. Once is fine. I even added an application.quit, changed variables, closed myDoc and set it to nothing and nothing I do makes any difference. Soon as you close the vbApp and reopen it is fine again for one time only.
I will play with it a bit more.
Just curious...why would you want to do it again as it will just do the same thing. Have you tried it a second time with a different file or is that what you are after?
Yes, I also tried the .close the .quit , but it didn't
solve the problem. The problem is somewhere in the Interface
between VB and Word.
Thanks for your help. I hope you'll be able
to figure it out. I tried long!!!
Because I want to append to the document and search for
the same word multiple of times.
I can kill the error by On Error "LEAVE" or Resume Next
but then I would have a dead command (procedure).
Code:'this works ..it was the selection.find statement
'and your reference to it.
Option Explicit
Private Sub Command1_Click()
Dim appWord As Word.Application
Dim MyDoc As Word.Document
Dim strDoc As String
Set appWord = New Word.Application
With appWord
.WindowState = wdWindowStateNormal
.Visible = True
.Documents.Add
strDoc = .ActiveDocument.Name
Set MyDoc = .Documents(strDoc)
.Selection.Find.ClearFormatting
End With
With MyDoc
.Range.InsertFile App.Path & "\" & "MyFile.txt"
With appWord.Selection.Find
.Text = "Status"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
appWord.Selection.Find.Execute
appWord.Selection.Font.Size = 12
appWord.Selection.Font.Name = "Arial Narrow"
appWord.Selection.Font.Bold = wdToggle
If appWord.Selection.Font.Underline = wdUnderlineNone Then
appWord.Selection.Font.Underline = wdUnderlineSingle
Else
appWord.Selection.Font.Underline = wdUnderlineNone
End If
appWord.Selection.Font.ColorIndex = wdRed
.SaveAs App.Path & "\" & "MyFile.doc"
.Close
End With
appWord.Quit 'you may want to take this line out
Set appWord = Nothing
Set MyDoc = Nothing
End Sub
Yes, it worked. Thanks a lot.
P.S. I don't understand the logic: How can an
application be the child of a document?