Solved

opening a Word document for mail merge

Posted on 2001-07-23
4
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

697 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