Solved

opening a Word document for mail merge

Posted on 2001-07-23
4
271 Views
Last Modified: 2012-06-27
This is my code for opening a document in Word. The problems are as follows: the documents open as microsoft Word form letter 1. When I want to close them I get the question if I want to save microsoft word form letter1, when I reply to this question, I get the question a second time but then with the name of the document I opened.The document opens also minimized. The user has to click the Word logo and then, click maximize with the right mouse button.

Private Sub CombineDoc(NameDoc As String)

    Dim objWord As Word.Application
    Set objWord = New Word.Application
   objWord.Documents.Open App.path & "\" & Namedoc, ReadOnly:=True
    objWord.Visible = True
    With objWord.ActiveDocument.MailMerge
    If .State = wdMainAndDataSource Then
            .Execute
    Else

    MsgBox "the document" & NameDoc & " isn't prepared for mail merge", vbInformation
    End If
    End With
End Sub

How can I change this code in order to prevent this from happening?
0
Comment
Question by:ELSJE
  • 2
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
roverm earned 100 total points
ID: 6308595
Just add the following line:

objWord.ActiveDocument.Saved = True

Then Word 'thinks' the document is already saved!
You can do this for both files.

D'Mzzl!
RoverM
0
 

Author Comment

by:ELSJE
ID: 6308747
Hi Rover, this helps in the way that I don't get asked anymore if I want to save, but still two documents are opened. Once the document I want to open and once the word form letter 1

Is it not possible to prevent this?
Is it also not possible that I see the document immediately once I click on the Word logo, in stead of clicking and after this maximizing with the right mouse button?
0
 
LVL 12

Expert Comment

by:roverm
ID: 6308945
To prevent this: I don't think so since Word thinks something has changed to the doc's ...

To maximize the correct window:
Just loop through the Documents collection: if your new doc is found, then use the WindowState property to maximize the window:

Sub MaxMyDoc()
Dim oDoc As Document
Dim sMyDoc As String
   
    sMyDoc = "MyNewDocument"
   
    For Each oDoc In Documents
        If UCase(oDoc.Name) = UCase(sMyDoc) Then 'found it
            oDoc.Activate
            ActiveWindow.WindowState = wdWindowStateMaximize
            Exit For
        End If
    Next oDoc

End Sub

D'Mzzl!
RoverM
0
 

Author Comment

by:ELSJE
ID: 6316155
I added another line
objword.activedocument.closed = true
This way the user sees only the document that he wanted to open

thanks

els
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now