PDA

View Full Version : Solved: Close Winword application from Access



slchslch
07-12-2005, 02:28 AM
Experts,
below is my global function which take 2 parameter(workorderno & strcaller) and save the data as a ms word file in C drive. Problem is after created the document file, the "WINWORD.exe" still loaded in the memory and wont be release. WHY? HELP!

Public objWord As Word.Document
Function SaveLogFile(strWorkorderNo As String, strCaller As String)

'Auto Point to Mail Merge Path
strGeneralPath = "C:\"
'Auto Generate Log File Name
strSaveName = CStr(strWorkorderNo) & " Work Order" & _
" Requested By " & CStr(strCaller) & ".doc"
strMailMergeFullPath = strGeneralPath & "Mail Merge\Hexagon Mail Merge Template.doc"

'Check for existence of mail merge in above Mail Merge Path, and exit if not found
strTestMailMergePath = Nz(Dir(strMailMergeFullPath))
Debug.Print "Mail Merge Path: " & strTestMailMergePath
If strTestMailMergePath = "" Then
MsgBox strMailMergeFullPath & " mail merge document not found; can't create letter"
Exit Function
Else
Set objWord = GetObject(strMailMergeFullPath, "Word.Document")
End If

' Open mail merge, write data into mail merge document
' Make Word invisible.
objWord.Application.Visible = False

With objWord.MailMerge
.Destination = wdSendToNewDocument
.Execute
End With
'Check for existence of previously saved letter in documents folder,
'and create another version of save name if found
i = 2
intSaveNameFail = True
Do While intSaveNameFail
strSaveNamePath = strGeneralPath & "Fax Workorder Log File\" & strSaveName
Debug.Print "Proposed save name and path: " & vbCrLf & strSaveNamePath
strTestSaveNamePath = Nz(Dir(strSaveNamePath))
Debug.Print "Save Name Document Path: " & strTestSaveNamePath
If strTestSaveNamePath = strSaveName Then
Debug.Print "Save name already used: " & strSaveName

'Create new save name with incremented number
intSaveNameFail = True
strSaveName = CStr(strWorkorderNo) & " Work Order" & " on " & _
CStr(strCaller) & " Version " & CStr(i) & ".doc"
strSaveNamePath = strGeneralPath & "Fax Workorder Log File\" & strSaveName
Debug.Print "New save name and path: " & vbCrLf & strSaveNamePath
i = i + 1
Else
Debug.Print "Save name not used: " & strSaveName
intSaveNameFail = False
End If
Loop

'Save new Mail Merge Document as Log File
strSaveName = strGeneralPath & "Fax Workorder Log File\" & strSaveName
With objWord.MailMerge
.Application.Documents(1).SaveAs (strSaveName)
End With
'.Application.Documents(2).Close wdDoNotSaveChanges
objWord.Activate
objWord.Close SaveChanges:=wdDoNotSaveChanges
Set objWord = Nothing
End Function

Killian
07-12-2005, 04:55 AM
If the object is declared public, it's possible that it's referenced somewhere else in the project.
You might want to take a bit more control by setting a ref to the word application as well , checking for existing documents and act accordinglySub test()

Dim objWord As Word.Document
Dim objWordApp As Word.Application

Set objWord = GetObject(strMailMergeFullPath, "Word.Document")
Set objWordApp = objWord.Application
objWordApp.Visible = False

MsgBox "Do stuff with the referenced document"

objWord.Close False
If objWordApp.Documents.Count > 0 Then
objWordApp.Visible = True
Else
objWordApp.Quit False
End If

Set objWord = Nothing
Set objWordApp = Nothing

End Sub

mdmackillop
07-12-2005, 09:33 AM
Retitled to match question content.

xCav8r
07-12-2005, 03:51 PM
You're missing this:

objWord.quit

xCav8r
07-13-2005, 04:40 PM
Whoops, I mean this:

objWordApp.quit

Killian
07-14-2005, 04:37 AM
It's there... if the documents count isn't >0

xCav8r
07-14-2005, 06:50 AM
I'm sorry, I meant to explicitly direct my comments to slchslch. You provided nice sample code, but I'm not sure that he/she understood that the answer to his/her question was buried in it. :winking2:


"WINWORD.exe" still loaded in the memory and wont be release. WHY? HELP!

slchslch
07-14-2005, 05:50 PM
sorry guys....sorry to reply ur post lately....the day after i posted this msg i have figure out the solution....thx anyway//...for sure i understand the solution which provided by killian ...thx !...another question for u guys...have any1 of u guys use Winfax as ur faxing software b4 tgh with the vba code.?

Killian
07-21-2005, 04:58 AM
np

I haven't used WinFax with VBA but I'll be over in an office that uses it early next week so I'll try to check out if there's any VBA object model available.
If ur in a hurry, take a look at your VBE:Tools>References list to see if you can find anything to do with WinFax in there. If so, add it, hit F2, search the OM for WinFax and see what you get to work with.

slchslch
07-21-2005, 05:50 PM
hmm...thx a lot for ur suggestion...i hav act got the solution dy...and everything run correctly as what i aspected....for ur information u can visit to www.getfaxing.com to get lot of tips and info abt winfax on how to send fax fomr vba....meanwhile..that site got lot of experts who will assits u tru out the winfax vba development...:D