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
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