Results 1 to 15 of 15

Thread: Destroy an Object

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Posts
    116

    Exclamation

    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)
    0101011001000010
    01101111011011100110110001101001011011100110010101110010

  2. #2
    Guest
    Set it to Nothing
    Code:
    Set appWord = Nothing

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Posts
    116

    Exclamation FORMATTING IS CAUSING AUTOMATION ERROR!

    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!
    0101011001000010
    01101111011011100110110001101001011011100110010101110010

  4. #4
    Guest
    Does the above method help?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Posts
    116
    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.
    0101011001000010
    01101111011011100110110001101001011011100110010101110010

  6. #6
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    code always helps...
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Posts
    116
    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


    0101011001000010
    01101111011011100110110001101001011011100110010101110010

  8. #8
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Selection.Find.ClearFormatting

    What does this do?
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Posts
    116
    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!
    0101011001000010
    01101111011011100110110001101001011011100110010101110010

  10. #10
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    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.

    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  11. #11
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    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?
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Posts
    116
    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!!!
    0101011001000010
    01101111011011100110110001101001011011100110010101110010

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Posts
    116

    Exclamation To : HeSaidJoe

    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).
    0101011001000010
    01101111011011100110110001101001011011100110010101110010

  14. #14
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    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
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Posts
    116

    Thumbs up GREAT JOB!!!

    Yes, it worked. Thanks a lot.

    P.S. I don't understand the logic: How can an
    application be the child of a document?
    0101011001000010
    01101111011011100110110001101001011011100110010101110010

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width