Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close Word after automated mail merge? 1

Status
Not open for further replies.

SnakeCharmer

Technical User
Mar 17, 2003
9
GB
I have the following module to run off a mailmerge and was wondering how I could modify it to close (without saving) the original letter thats opened and the merged doc thats created.

Thanks!

Function MergeNewPacks()
Dim objWord As Word.Document
Set objWord = GetObject("H:\***.doc", "Word.Document")
objWord.Application.Visible = True
objWord.MailMerge.OpenDataSource _
Name:="H:\***.mdb", _
LinkToSource:=True, _
Connection:="QUERY New Primary NQTs", _
SQLStatement:="SELECT * FROM [New Primary NQTs]"
objWord.MailMerge.Destination = wdSendToNewDocument
objWord.MailMerge.Execute
objWord.Application.Options.PrintBackground = False
objWord.Application.ActiveDocument.PrintOut
End Function
 
Hi SnakeCharmer,

I think this should do it for you:

objWord.Quit SaveChanges:=wdDoNotSaveChanges
Set objWord = Nothing

Bill

 
Thanks Bill

Unfortunately I get a **run-time error 438: Object doesn't support this property or method** when I run the module with the extra lines. Any ideas!?

Jake
 
Sorry was quoting from memory, forgot the Application bit:

objWord.Application.Quit SaveChanges:=wdDoNotSaveChanges
Set objWord = Nothing

Bill
 
Thats great, thanks Bill! (Sorry I should really have noticed myself)

Just one other thing - I also need to close the VB window thats brought up, but I'm not sure how to refer to it. I tried using acDefault as it's the active window after Word closes, but I couldn't get it to work. (Sorry if I'm asking obvious q's - I'm pretty new to VB)

Cheers

Jake
 
Hi SnakeCharmer,

I don't get this additional window.

Click on the additional window.

Press Ctrl + Alt + Delete at the same time.

What does the highlighted text say in the Close Program window. Click Cancel.

Bill



 
Hi Bill

The task is listed as:

****
Microsoft Visual Basic - Primary Pool [break] - [Primary Pack Letter (Code)]
****

Where Primary Pool = db name and
Primary Pack Letter = the mailmerge module

Thanks

Jake
 
Hi,

Here's a procedure that closes unwanted windows. Paste this into a new module:

Option Explicit

Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg As Long, wParam As Any, lParam As Any) As Long

Public Const WM_CLOSE = &H10
Global ghWnd As Long
Global gCurrenthWnd As Long
Global gTitle As String

Public Function EnumWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
On Error Resume Next
Dim slength As Long, TitleBar As String, retval As Long
Static winnum As Integer
winnum = winnum + 1
slength = GetWindowTextLength(hWnd) + 1
If slength > 1 Then
TitleBar = Space(slength)
retval = GetWindowText(hWnd, TitleBar, slength)
If InStr(TitleBar, gTitle) Then
If hWnd <> ghWnd Then
Call SendMessage(hWnd, WM_CLOSE, 0&, 0&)
End If
End If
End If
EnumWindowsProc = 1
End Function

Paste this at the point in your existing code that you want to close the unwanted window:

gTitle = &quot;Microsoft Visual Basic -&quot;
Call EnumWindows(AddressOf EnumWindowsProc, 0)

This will close any windows starting with Microsoft Visual Basic -

I haven't included the full title just in case you change yoy DB name etc.

It's not the easiest thing to do, closing a window from Access, but I think this should do it. Strange though that I didn't get this extra window. Any, hope it works for you.

Bill

 
Hi Bill

Thanks, that works, unfortunately a bit too well!

It closes the VB window, but also closes the DB itself...?

Cheers

Jake
 
Hi SnakeCharmer,

I should have noticed this before, somewhere in your code you've instructed VB to go into Break Mode.

In one or more lines of your code you should see a dark red circle (Break) to the immediate left of the Code Window. Click on the circle(s) to remove them. This should stop the VB window opening.

Let me know how you get on.

Bill




 
Hi Bill

I've resolved the problem and heart-felt apologies are in order!

In the Macro that calls the code I had mistakenly opened the module instead of just running the code.

Thanks for all your help though - the close windows procedure is great and I'm bound to use it somewhere else.

Jake
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top