[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

opening a Word document for mail merge

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
ELSJE
Asked:
ELSJE
  • 2
  • 2
1 Solution
 
rovermCommented:
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
 
ELSJEAuthor Commented:
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
 
rovermCommented:
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
 
ELSJEAuthor Commented:
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.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now