PDA

View Full Version : [SOLVED:] Open newly merged document?



sandi v
08-02-2018, 08:08 AM
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:= _
"\\server (file://\\server)PATH\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

gmayor
08-02-2018, 08:07 PM
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

sandi v
08-03-2018, 08:35 AM
Thank you SO MUCH!!!



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