Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-24-2016, 07:41 PM
JennEx JennEx is offline Sending Merge Document To Printer And Saving Windows XP Sending Merge Document To Printer And Saving Office 2013
Competent Performer
Sending Merge Document To Printer And Saving
 
Join Date: May 2010
Posts: 162
JennEx is on a distinguished road
Default Sending Merge Document To Printer And Saving

I have this Excel VBA code that simply produces a new merge document for the user to review before printing.



Code:
Sub merge2(ByVal i As Long, ByVal ws_vh As Object, ByVal rpt_od As String, objWord As Object, ByVal dest As Double)
 'Dim objWord As Object, oDoc As Object
 Dim oDoc As Object
 Dim StrSQL As String, fName As String, StrSrc As String, strFilename As String
 Dim ws_th As Worksheet
 Dim qfile As String, st_srchnfn As String, wb_qfile2 As Workbook
 'Dim wb_qfile2 As Workbook
 
 Const wdSendtToNewDocument = 0
 Const wdSendToPrinter = 1
 Const wdFormLetters = 0
 Const wdDirectory = 3
 Const wdMergeSubTypeAccess = 1
 Const wdOpenFormatAuto = 0
 qfile2 = ws_vh.Range("B4")
 
 st_srchfn = "H:\PWS\Parks\Parks Operations\Sports\Sports15\DATA1\" & qfile2
 Set wb_qfile2 = Workbooks(qfile2)
 If wb_qfile2 Is Nothing Then
     MsgBox qfile2 & " is NOT open."
 Else
     'MsgBox qfile2 & " is open"
     wb_qfile2.Close False
 End If
 
 Set ws_th = Workbooks("Sports15b.xlsm").Worksheets("TEMP_HOLD")
 
 itype = Right(ws_th.Range("A" & i), 2)
 isubresp = Left(ws_th.Range("A" & i), 3)
 
 If itype = "DR" Then
     fName = "H:\PWS\Parks\Parks Operations\Sports\Sports15\REPORTS\v1\DR15v1.docx"
 ElseIf itype = "DT" Then
     fName = "H:\PWS\Parks\Parks Operations\Sports\Sports15\REPORTS\v1\DT15v1.docx"
 ElseIf itype = "FR" Then
     fName = "H:\PWS\Parks\Parks Operations\Sports\Sports15\REPORTS\v1\FR15v1.docx"
 ElseIf itype = "FT" Then
     fName = "H:\PWS\Parks\Parks Operations\Sports\Sports15\REPORTS\v1\FT15v1.docx"
 ElseIf itype = "CR" Then
     fName = "H:\PWS\Parks\Parks Operations\Sports\Sports15\REPORTS\v1\CR15v1.docx"
 Else
     fName = "H:\PWS\Parks\Parks Operations\Sports\Sports15\REPORTS\v1\CT15v1.docx"
 End If
 
 StrSrc = "H:\PWS\Parks\Parks Operations\Sports\Sports15\DATA1\" & ws_vh.Range("B4")
 
 StrSQL = "SELECT * FROM [CORE$] WHERE [TYPE]='" & itype & "' AND [SIG_CREW]='" & isubresp & "' " & _
     "ORDER BY [STARTS] ASC, [COMPLEX] ASC, [UNIT] ASC"
 
 Set objWord = CreateObject("Word.Application")
 With objWord
     .DisplayAlerts = False
     .Visible = True
     Set oDoc = .Documents.Open(Filename:=fName, ConfirmConversions:=False, _
         ReadOnly:=True, AddToRecentFiles:=False, Visible:=True)
     With oDoc
         With .MailMerge
             .MainDocumentType = wdFormLetters
             If dest = 1 Then
                 .Destination = wdSendtToNewDocument
             Else 'dest=2
                 .Destination = wdSendToPrinter
             End If
                .SuppressBlankLines = True
                .OpenDataSource Name:=StrSrc, AddToRecentFiles:=False, LinkToSource:=False, ConfirmConversions:=False, _
                    ReadOnly:=True, Format:=wdOpenFormatAuto, Connection:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
                    "User ID=Admin;Data Source=" & StrSrc & ";Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";", _
                    SQLStatement:=StrSQL, SQLStatement1:="", SubType:=wdMergeSubTypeAccess
                .Execute Pause:=False
            End With
            .Close False
        End With
        .DisplayAlerts = True
 
        With .ActiveDocument
            If .Sections.count > 1 Then
                For Each HdFt In .Sections(.Sections.count).Headers
                    If HdFt.Exists Then
                        HdFt.Range.FormattedText = .Sections(1).Headers(HdFt.Index).Range.FormattedText
                        HdFt.Range.Characters.Last.Delete
                    End If
                Next
                For Each HdFt In .Sections(.Sections.count).Footers
                    If HdFt.Exists Then
                        HdFt.Range.FormattedText = .Sections(1).Footers(HdFt.Index).Range.FormattedText
                        HdFt.Range.Characters.Last.Delete
                    End If
                Next
            End If
            Do While .Sections.count > 1
                .Sections(1).Range.Characters.Last.Delete
                DoEvents
            Loop
            .Range.Characters.Last.Delete
        End With
    End With
 
    Set oDoc2 = objWord.ActiveDocument
    'MsgBox oDoc2.Name
    With oDoc2
        myPath = "H:\PWS\Parks\Parks Operations\Sports\Sports15\WORKORDERS\" & Format(ws_vh.Range("B2"), "ddd dd-mmm-yy")
        .SaveAs myPath & "\" & rpt_od & ".docx"
        '.Close
    End With
 
    AppActivate "Microsoft Excel"
    Set oDoc = Nothing: Set oDoc2 = Nothing ': Set objWord = Nothing
    'End If
    Workbooks.Open st_srchfn
End Sub
What would I need to send the newly created document to the printer while saving a copy of the report.

I have tried, but I have failed. What I have tried to do here is use the same code, but use a variable to determine the destination route. If the user wishes to simply view the report, a pushbutton in the userform sets dest = 1 and calls merge2. If the user wishes to print and save the report, a different pushbutton on the userform sets dest = 2 and calls merge 2
Reply With Quote
  #2  
Old 05-24-2016, 11:48 PM
macropod's Avatar
macropod macropod is offline Sending Merge Document To Printer And Saving Windows 7 64bit Sending Merge Document To Printer And Saving Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi Jenn,

I'd suggest:
ByVal dest as Long
and, instead of:
conditionally changing the destination, always use:
wdSendtToNewDocument

Also, instead of:
With .ActiveDocument
...
End With

Set oDoc2 = objWord.ActiveDocument
'MsgBox oDoc2.Name
With oDoc2
...
End with
use:
Set oDoc2 = objWord.ActiveDocument
With oDoc2
...
End with
and, where you have:
.SaveAs myPath & "\" & rpt_od & ".docx"
introduce the dest test at this point to conditionally save (via SaveAs) or print (via .Printout) the document, then close it without saving.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 05-25-2016, 05:06 AM
JennEx JennEx is offline Sending Merge Document To Printer And Saving Windows XP Sending Merge Document To Printer And Saving Office 2013
Competent Performer
Sending Merge Document To Printer And Saving
 
Join Date: May 2010
Posts: 162
JennEx is on a distinguished road
Default

Brilliant! Exactly what I was looking for and easy to understand. The .Printout is a new command for me, so with it available, your solution makes perfect sense to me.

Thanks for all your help!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Printer Problems-Need to reload printer each day jekronenfeld Windows 1 11-24-2014 12:35 AM
Sending Merge Document To Printer And Saving Help Sending Mail Merge Document dazwm Mail Merge 8 01-28-2014 01:00 PM
Sending Merge Document To Printer And Saving Problem with saving document after paste some contents from another document expert4knowledge Word 3 11-26-2013 03:53 AM
MS Word 2003, printer queue in 'printer properties' shows 1 job; no job in printer benhuxham Word 0 07-25-2011 06:58 PM
Sending Merge Document To Printer And Saving Select printer to document JosL Office 3 03-07-2009 12:40 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 06:39 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft