Creosote65
Technical User
Hi all,
I'm back with a problem with which I was dealing a while ago. The following vba code initiates a Word session, performs a mail merge, prompts the user to save the output file, closes Word and returns to a previous menu. Everything runs fine the first time. However, if I don't quit and restart Access before attempting to reuse this function, I will get an error 462.
As far as I can tell, I have closed all sessions of Word, so I shouldn't get a conflict. I also have a little vb script file that detects hidden instances of Word. It doesn't show any open instances after the macro closes Word.
I can't figure what the problem might be. Any help will be appreciated.
-Creosote
1Private Sub cmbQuote_AfterUpdate()
2 Dim appWord As Word.Application
3 On Error GoTo ExportError
4 If IsNull(frmDocType) Then 'Prompt User if doc type is not chosen
5 MsgBox "You must choose an output type before choosing a quote.", vbExclamation, "No document type"
6 cmbQuote = Null
7 ElseIf frmDocType = 4 Then 'Output the quote to a text file and merge with a form letter
8 DoCmd.TransferText acExportMerge, , "qryExpAddendumHeaderEng", "c:\MailMerge\qryExpAddendumHeaderEng.txt"
9 'Open Word and create a new document
10 Set appWord = New Word.Application
11 appWord.Visible = True
12 appWord.Documents.Add Template:="P:\Template files\Addendum template EN.dot", _
13 NewTemplate:=False, DocumentType:=0
14 With ActiveDocument.MailMerge
15 .Destination = wdSendToNewDocument
16 .SuppressBlankLines = True
17 With .DataSource
18 .FirstRecord = wdDefaultFirstRecord
19 .LastRecord = wdDefaultLastRecord
20 End With
21 .Execute Pause:=False
22 End With
23 With appWord
24 '.ActiveDocument.Save noprompt:=False
25 .Documents("Document1").Activate
26 .ActiveDocument.Close savechanges:=False
27 .Quit True
28 End With
29 Set appWord = Nothing
30 DoCmd.OpenForm "frmMenuContrEng", acNormal
31 DoCmd.Close acForm, "frmChooseQuoteExpArchContrEng", acSaveNo
32
33 End If
34 ExportError:
35 If Err.Number = 462 Then
36 'Word is still open from previous export.
37 appWord.Quit False
38 Set appWord = Nothing
39 MsgBox "The export has failed. Please close Access and try again.", vbExclamation, "Export Error"
40 DoCmd.OpenForm "frmMenuContrEng"
41 DoCmd.Close acForm, "frmChooseQuoteExpArchContrEng", acSaveNo
42 Exit Sub
43 End If
44 End Sub
I'm back with a problem with which I was dealing a while ago. The following vba code initiates a Word session, performs a mail merge, prompts the user to save the output file, closes Word and returns to a previous menu. Everything runs fine the first time. However, if I don't quit and restart Access before attempting to reuse this function, I will get an error 462.
As far as I can tell, I have closed all sessions of Word, so I shouldn't get a conflict. I also have a little vb script file that detects hidden instances of Word. It doesn't show any open instances after the macro closes Word.
I can't figure what the problem might be. Any help will be appreciated.
-Creosote
1Private Sub cmbQuote_AfterUpdate()
2 Dim appWord As Word.Application
3 On Error GoTo ExportError
4 If IsNull(frmDocType) Then 'Prompt User if doc type is not chosen
5 MsgBox "You must choose an output type before choosing a quote.", vbExclamation, "No document type"
6 cmbQuote = Null
7 ElseIf frmDocType = 4 Then 'Output the quote to a text file and merge with a form letter
8 DoCmd.TransferText acExportMerge, , "qryExpAddendumHeaderEng", "c:\MailMerge\qryExpAddendumHeaderEng.txt"
9 'Open Word and create a new document
10 Set appWord = New Word.Application
11 appWord.Visible = True
12 appWord.Documents.Add Template:="P:\Template files\Addendum template EN.dot", _
13 NewTemplate:=False, DocumentType:=0
14 With ActiveDocument.MailMerge
15 .Destination = wdSendToNewDocument
16 .SuppressBlankLines = True
17 With .DataSource
18 .FirstRecord = wdDefaultFirstRecord
19 .LastRecord = wdDefaultLastRecord
20 End With
21 .Execute Pause:=False
22 End With
23 With appWord
24 '.ActiveDocument.Save noprompt:=False
25 .Documents("Document1").Activate
26 .ActiveDocument.Close savechanges:=False
27 .Quit True
28 End With
29 Set appWord = Nothing
30 DoCmd.OpenForm "frmMenuContrEng", acNormal
31 DoCmd.Close acForm, "frmChooseQuoteExpArchContrEng", acSaveNo
32
33 End If
34 ExportError:
35 If Err.Number = 462 Then
36 'Word is still open from previous export.
37 appWord.Quit False
38 Set appWord = Nothing
39 MsgBox "The export has failed. Please close Access and try again.", vbExclamation, "Export Error"
40 DoCmd.OpenForm "frmMenuContrEng"
41 DoCmd.Close acForm, "frmChooseQuoteExpArchContrEng", acSaveNo
42 Exit Sub
43 End If
44 End Sub