Solved

opening a Word document for mail merge

Posted on 2001-07-23
4
270 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

744 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

13 Experts available now in Live!

Get 1:1 Help Now