?
Solved

WINWORD.EXE is not closing -- file lock not being released

Posted on 2003-02-25
23
Medium Priority
?
983 Views
Last Modified: 2012-08-14
The following code runs when we send out what is called "Closing Instructions" fax.

For some reason the WINWORD.EXE is not being closed (I go into Task Manager and it is still there)

Also, the file is not being closed on the Server.

The Word file in question is a template file (.DOT).

Looking at the code below, is there any reason why WINWORD.EXE would be left open or the file lock not released, or the file not closed, etc.?

We use Office automation heavily in our Access application.  I really need an answer to this!


~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sub sbGenerateClosingInstructions(strDocType As String)
    'The fax that has names and titles in it.
    Dim strFaxRaw As String
   
    Dim strTempUniqueCode As String
   
    Dim strCover As String
   
    Dim strUpdatedGrantApp As String

    Dim strToField As String
    Dim strSubject As String
    Dim strFile1 As String, strFile2 As String
    Dim fPrintOut As Boolean
    Dim strTempInText As String
    Dim strTempFieldName As String
    Dim FBoldSetting As Boolean
    Dim FItalicSetting As Boolean
    Dim FUnderlineSetting As Boolean
    Dim intFontSize As Long
    Dim intBookmarkCount As Long
   
    Dim strFaxOutTable As String
    'Const strFaxOutTable As String = "qryPrintQuery"
   
    Dim strCertifiedFaxDocumentToSend As String
    Dim strCertifiedFaxCovertToSend As String
    Dim strCIFaxPath As String
    Dim strCI As String
    Dim lngEntryID As Variant
    Dim intCounter As Long
       
    Set recTemp = New Recordset
   
    Dim strLetterName As String
    Dim intRecordCount As Long
    Dim dblOrigRandomID As Double
    Dim dblNewRandomID As Double
    Dim varRandomID As Variant
   
    Dim strFaxType As String
    Dim fGoes
    Dim strOutFile As String, strOutPath As String, strOutFile2 As String
    Dim TimeIn As Date
    Dim bk As Bookmark
   
    intCounter = 0
   
    TimeIn = Now
       
    recTemp.CursorLocation = adUseClient
       
    strFaxOutTable = "SELECT tblEntries.EntryID, tblEntries.Buyer, " & _
    "tblEntries.BuyerFirstName, tblEntries.BuyerLastName, tblEntries.EscrowNumber, tblEntries.CurAddr, " & _
    "tblEntries.City, tblEntries.State, tblEntries.Zip, FormatMoney([Grant]) AS GrantAmount, " & _
    "FormatMoney([SSF]) AS SSFAmount, [Lender] & ""'s"" AS MC, tblEntries.TentativeClosingDate AS TCD, " & _
    "tblEntries.TitleAgency, tblEntries.[Closing Agent] AS CA, tblEntries.TitleFax, tblEntries.LO, tblEntries.Lender, " & _
    "tblEntries.LenderFax, [SellerAddress] & IIf(nz([SellerAddress],"""")="""","""","", "") & [SellerCity] & IIf(nz([SellerCity],"""")="""","""","", "") & [SellerState] & "" "" & [SellerZip] AS PropertyAddress FROM tblEntries WHERE (((tblEntries.EntryID)= " & [EntryID] & "));"
       
    'Open tblFaxOutHolder
    recTemp.Open strFaxOutTable, CurrentProject.Connection, adOpenForwardOnly, adLockReadOnly
   
    'Count of records (faxes to send)
    intRecordCount = recTemp.RecordCount
   
    If intRecordCount > 0 Then
   
        'LOOP through all records in tblFaxOutHolder
        Dim objWord1 As New Word.Application
   
        Do While recTemp.EOF = False
               
            strCertifiedClosingInstructionsToSend = "P:\Documents\fmci2page.dot"
                   
            objWord1.Documents.Add strCertifiedClosingInstructionsToSend
                   
            'LOOP THROUGH CLOSING INSTRUCTIONS BOOKMARKS
            For Each bk In objWord1.ActiveDocument.Bookmarks
                ' if the name is the same as a bookmark name, increment counter + 1
                strTempInText = bk.Name
           
                ParseBookmark strTempInText, strTempFieldName, FBoldSetting, FItalicSetting, FUnderlineSetting, intFontSize, intBookmarkCount
           
                bk.Select
                Select Case Nz(recTemp.Fields(strTempFieldName).Value, "")
                    Case ""
                        'objWord1.Selection.Delete
                        objWord1.Selection.EndKey
                       
                       
                    Case Else
                        objWord1.Selection.Font.Bold = FBoldSetting
                        objWord1.Selection.Font.Italic = FItalicSetting
                        objWord1.Selection.Font.Underline = FUnderlineSetting
                        objWord1.Selection.Font.Size = intFontSize
                        If strTempFieldName = "TitleFax" Then
                            objWord1.Selection.TypeText ConvertRawPhone(recTemp.Fields(strTempFieldName).Value)
                        Else
                            objWord1.Selection.TypeText recTemp.Fields(strTempFieldName).Value
                        End If
                End Select
            Next
       
            strTempUniqueCode = UniqueCode
       
            strCIFaxPath = "K:\coverpge\adv\fmci2page" & "-" & strTempUniqueCode & ".doc"
       
            objWord1.ActiveDocument.SaveAs strCIFaxPath
           
            If strDocType = "Print" Then
                objWord1.ActiveDocument.PrintOut
            End If
           
            If strDocType <> "Create" Then
                objWord1.ActiveDocument.Close
            End If
           
            If strDocType = "Create" Then
                objWord1.Application.Visible = True
            End If
           
       
            strFaxRaw = Nz(recTemp("TitleFax"), "")
       
            'strFaxRaw = "8012260878"
       
            strToField = "OC-" & Nz(recTemp("EntryID"), "") & "-" & Nz(recTemp("CA"), "") & "-" & Nz(recTemp("TitleAgency"), "")
            strSubject = recTemp("EntryID") & "-OC-" & recTemp("Buyer")
       
            strCI = "adv\fmci2page" & "-" & strTempUniqueCode & ".doc"
           
            If strFaxRaw = "" And strDocType = "Fax" Then
                MsgBox "This must have a fax number for the title agency before you can send out a fax."
                Exit Sub
            End If
                   
            If strDocType = "Fax" Then
                fGoes = GenFMText(strTempUniqueCode & ".txt", "J:\HighPriority\", strFaxRaw, strCI, strToField, , , , , , strSubject)
            End If
           
            DoEvents
       
            intCounter = intCounter + 1
            recTemp.MoveNext
        Loop
       
        If strDocType = "Fax" Then
            DoEvents
            ConfirmOff
            DoCmd.RunSQL "UPDATE tblEntries SET CIFaxYN = True, CIFaxDate = #" & Now() & " #, CIFaxConfYN = False, CIFaxConfDate = Null WHERE EntryID=" & Me.EntryID & ";"
            'DoCmd.RunSQL "INSERT INTO tblComments ( CommentType, Comment, EntryID ) SELECT ""CIFax"", ""Closing Instructions Faxed"", EntryID FROM tblEntries WHERE EntryID=" & Me.EntryID & ";"
            ConfirmOn
            fnAppendComment Me.EntryID, "CIFax", "Closing Instructions Faxed"
           End If
           
        DoCmd.Save acForm, "frmPrintWordDocs"
               
        Me.Refresh
    End If
   
    If strDocType = "Print" Then
        MsgBox "Closing Instructions Printed"
    End If
   
    If strDocType = "Create" Then
        MsgBox "Closing Instructions Created"
    End If
   
    If strDocType = "Fax" Then
        MsgBox "Closing Instructions Faxed"
    End If
   
   
    If strDocType <> "Create" Then
        objWord1.Quit
        Set objWord1 = Nothing
        Set recTemp = Nothing
    End If
           
End Sub
0
Comment
Question by:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
23 Comments
 
LVL 13

Expert Comment

by:cri
ID: 8019597
Difficult to debug w/o having the whole environment.

a) Which Word version and service release do you run ?

b) Operating system version/service release for server/client ?

c) Did you try your code on a non-networked PC ?


BTW: Perhaps of use http://www.helenfeddema.com/CodeSamples.htm

0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 8019701
a)  Word Version:  Word 2000 (no service release is installed)

b)  OS Version:  2000 Professional, Service Pack 3

c)  No, I have not tried the app on a non-networked PC.



0
 
LVL 4

Expert Comment

by:zombieooo
ID: 8019831
Do you have Outlook open with MSWord as your e-mail editor?  This will cause the situation described as well.  Check in Outlook under Tools, Options, Mail Format Tab, and uncheck Use Microsoft Word as ...

Joel
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 5

Author Comment

by:Tom Knowlton
ID: 8019921
I checked...a few people had MS Word as the default editor.

I have since unchecked the box for that.

I'll wait a while and see what happens.
0
 
LVL 2

Accepted Solution

by:
Phetu earned 800 total points
ID: 8020255
Hi,

I would use this:

dim objWord1 as object
Set objWord1 = CreateObject("word.application")

instead of your
Dim objWord1 as New word.application

And at the end of your code you should do:

Set objWord1 = Nothing

Hope it will help

Phetu
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 8020569
Phetu:

Does your way help prevent memory leaks, etc. or is it just another way of doing the same thing?

Thanks,

Tom
0
 
LVL 2

Expert Comment

by:Phetu
ID: 8020600
Hi,

It prevent Word (even Excel) to stay in memory when you close the application. I've ran into a similar problem than yours and this solution solve my problem.

Phetu
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 8020615
Phetu:

Excellent!  Let me try this out, then.

I'll let you know.

Tom
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 8020653
What is the difference between:

dim objWord1 as object
Set objWord1 = CreateObject("word.application")


and


Dim objWord1 as New word.application

???
0
 
LVL 2

Expert Comment

by:Phetu
ID: 8020666
It's the same thing except that one is working correct!

Phetu
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 8020718
LOL...  *okay*
0
 
LVL 2

Expert Comment

by:Phetu
ID: 8020769
Ok...ok.

To give you a good answer is that the both ways should work perfectly but because that "Dim objWord1 as New word.application" is not working properly the workaround is to try the instruction that i gave you!

Does it make more sense now!

Phetu :))
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 8020794
Yes.

Incidentally, I tried out the "OLD" code (the way I was doing it) and faxed several letters.  The WINWORD.EXE process cleaned-up just fine every single time!

It seems to be in a network environment where one .DOT file is being used by multiple people.

Tom
0
 
LVL 2

Expert Comment

by:Phetu
ID: 8020857
I'm not sure that your problem is caused by the .DOT file because it's a common problem that people had with Excel and Word.

You'll see.

Pehtu
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 8020881
You sound pretty sure about this.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 8020895
0
 
LVL 2

Expert Comment

by:Phetu
ID: 8021075
They are talking about the visible property of the Word application. It's because when you start a Word instance in VBA you do not have to make it visible to the user. And if another instance is started "manually" by the user, the wrong instance is activated (the VBA one). So if the user close this instance, your vba program won't find the instance that it creates.

But your problem is not this at all!!! It remain in memory. They are not talking about that!

Phetu
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 8021107
Okay.
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 8021162
Thanks,

Tom
0
 
LVL 2

Expert Comment

by:Phetu
ID: 8021220
Just to make you feel more relax, take a look at this.

Phetu
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 8026038
???
0
 
LVL 2

Expert Comment

by:Phetu
ID: 8031131
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 8034528
Got it.

Glad to see this worked for somebody else!

I stil haven't had time to try this out yet.

Tom
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question