Consulting

Results 1 to 3 of 3

Thread: Open newly merged document?

  1. #1
    VBAX Newbie
    Joined
    Jul 2018
    Posts
    2
    Location

    Open newly merged document?

    Hello! I'm new here, and this looks like the place I need to be! I see a lot of familiar friendly names from other forums. I hope someone can help me. I've got a good start on a template that creates letterhead for one of several office locations chosen on user form, and then performs a mail merge. I recorded macros to connect the data source and then perform the merge. They work. Next, I would like to show the newly merged document to the user. Currently, whatever document the user last had open remains the active document. I've searched and searched, but cannot find the answer. Can anybody help? I'm not a programmer; so please be gentle

    Public Sub macConnectDataSource()
    
      
        ActiveDocument.MailMerge.OpenDataSource Name:= _
            "\\serverPATH\SampleData.csv" _
            , ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True, _
            AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
            WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, _
            Format:=wdOpenFormatAuto, Connection:="", SQLStatement:="", SQLStatement1 _
            :="", SubType:=wdMergeSubTypeOther
            
        ActiveDocument.SaveAs "c:\temp\TempMergeForm.docx"
          
    
    End Sub
    
    
    
    Public Sub macRunMerge()
    
        With ActiveDocument.MailMerge
            .Destination = wdSendToNewDocument
            .SuppressBlankLines = True
            With .DataSource
                .FirstRecord = wdDefaultFirstRecord
                .LastRecord = wdDefaultLastRecord
            End With
            .Execute Pause:=False
            
            
        End With
    
    
    'this closes the merge form, so user can see the merged document.
    Documents("c:\temp\TempMergeForm.docx").Close SaveChanges:=wdDoNotSaveChanges
    
    
    'The merged document gets the focus if user has no other documents open
    'NOW: how to get focus on the merged form letters in event user
    'has other documents open?
    
            'ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges  !  this closes the merged form
            'ActiveDocument.SetFocus ! not supported
            'Application.ScreenRefresh ! does nothing
            'ActiveDocument.ActiveWindow.Activate ! causes document to detach
    'ActiveDocument.Activate
    
    'ArrangeWindows
    
    'End Sub
    
    'Public Sub ArrangeWindows()
    
    'Documents(1).Close wdDoNotSaveChanges
    'Documents(2).Activate
    'Documents("Form Letters18").Activate
    
        
    End Sub

  2. #2
    If the merged document is not already open, the merged document is called LettersN where N is a number that reflects the number of merges. That being the case you can step through the windows collection (backwards, as the just merged document should be the last in the stack) look for that name and activate that window e.g.

    Dim i As Integer
        For i = Windows.Count to 1 Step -1
            If Windows(i) Like "Letters?" Then
                Windows(i).Activate
                Exit For
            End If
        Next i
    Last edited by gmayor; 08-02-2018 at 09:20 PM.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Newbie
    Joined
    Jul 2018
    Posts
    2
    Location

    Yay!!!! That works perfectly!

    Thank you SO MUCH!!!


    Quote Originally Posted by gmayor View Post
    If the merged document is not already open, the merged document is called LettersN where N is a number that reflects the number of merges. That being the case you can step through the windows collection (backwards, as the just merged document should be the last in the stack) look for that name and activate that window e.g.

    Dim i As Integer
        For i = Windows.Count to 1 Step -1
            If Windows(i) Like "Letters?" Then
                Windows(i).Activate
                Exit For
            End If
        Next i

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •