+ Reply to Thread
Results 1 to 2 of 2

PrintToExcelAndPDF file

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-29-2012
    Location
    Hyderbad
    MS-Off Ver
    Excel 2003
    Posts
    123

    PrintToExcelAndPDF file

    Option Explicit
    Sub PrintToExcelAndPDF()

    'Dim pdfjob As PDFCreator.clsPDFCreator
    Dim sPDFName As String
    Dim sPDFPath As String
    Dim sSheetsToPrint As String
    Dim sSheets() As String
    Dim lSheet As Long
    Dim lTtlSheets As Long
    Dim bRestart As Boolean
    Dim rowCtr As Integer
    Dim pcp_name As String
    Dim maxRow As Integer
    Dim pivotFile As String
    Dim templateFile As String
    Dim LastRow As Integer
    Dim LastCol As Integer
    Dim dirPath As String
    Dim atRisk As String

    '/// Record the sheets you want to print here! ///
    '/// Use sheet names separated by commas only ///
    sSheetsToPrint = "ERS"

    ' SET source file and destination file names
    pivotFile = "DATA ER template.xlsx"
    templateFile = "Medicaid Census Template - COPY.xlsx"

    '***********Start Logic***********

    Sheets("LIST").Select
    With ActiveSheet
    maxRow = .Range("A4").End(xlDown).Row
    End With

    MsgBox maxRow & ""

    For rowCtr = 5 To 8
    'Select PCP from the List
    Sheets("LIST").Select
    pcp_Group = ActiveSheet.Range("A" & rowCtr).Value


    sPDFName = Group
    sPDFPath = ActiveWorkbook.Path & Application.PathSeparator

    'Set Slicers to pcp_name

    ActiveWorkbook.SlicerCaches("Slicer_GROUP").VisibleSlicerItemsList = Array("[group].[GROUP].&[" & _
    pcp_Group & "]")





    'OPEN REPORT TEMPLATE
    Application.Workbooks.Open (sPDFPath & templateFile)
    Windows(templateFile).Activate

    'DELETE SFR SHEET FOR ALL PCPS. TBD CHECK IF PCP IS AT RISK, THEN COPY SFR DATA
    Application.DisplayAlerts = False
    'Sheets("SFR").Delete
    Application.DisplayAlerts = True

    'DELETE SFR ROW FROM LIST OF REPORTS FOR NON AT RISK PCPS
    'Sheets("LIST OF REPORTS").Select
    'If atRisk <> "1" Then
    'Rows("2:2").Select
    'Selection.Delete Shift:=xlUp
    'End If


    'COPY all reports from Pivot Tables to Report Template

    'Split the sheets into an array
    sSheets() = Split(sSheetsToPrint, ",")

    lTtlSheets = 0

    'ITERATE OVER ALL SHEETS
    For lSheet = LBound(sSheets) To UBound(sSheets)

    'SWITCH to PowerPivot
    ThisWorkbook.Activate

    If Not IsEmpty(Application.Sheets(sSheets(lSheet)).UsedRange) Then

    Sheets(sSheets(lSheet)).Select
    Range("b12").Select

    'MsgBox ActiveCell.Value

    If IsEmpty(ActiveCell.Value) Then
    Application.DisplayAlerts = False
    Windows(templateFile).Activate
    Sheets(sSheets(lSheet)).Delete
    Application.DisplayAlerts = True
    lTtlSheets = lTtlSheets + 1

    Else
    'FIND LAST ROW ON DATA SHEET

    With ActiveSheet
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row '.Range("A12").SpecialCells(xlCellTypeLastCell).Row
    LastCol = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column '.Range("A12").SpecialCells(xlCellTypeLastCell).Column
    End With


    lTtlSheets = lTtlSheets + 1

    'COPY CELLS
    Range(Cells(12, 2), Cells(LastRow, LastCol)).Select
    Selection.Copy

    '=============================================

    'PASTE in the Template
    Windows(templateFile).Activate
    Sheets(sSheets(lSheet)).Select
    Range("B4").Select
    ActiveSheet.Paste

    'FILL SERIES in Column A
    'Range("A2").Select
    'ActiveCell.FormulaR1C1 = "1"
    'Selection.AutoFill Destination:=Range("A2:A" & LastRow - 10), Type:=xlFillSeries


    'FORMAT copies cells
    Range(Cells(4, 1), Cells(LastRow - 8, LastCol)).Select
    With Selection.Font
    .Name = "Arial Narrow"
    .Size = 10
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .ThemeColor = xlThemeColorLight1
    .TintAndShade = 0
    .ThemeFont = xlThemeFontNone
    End With

    'FORMAT_TEMPLATE_CELLS

    Columns("A:K").Select
    Columns("A:K").EntireColumn.AutoFit
    Range("A1").Select

    ' SET HEADER
    With ActiveSheet.PageSetup
    .LeftHeader = "&""Arial Narrow,Bold""COMPLEX CARE MANAGEMENT REPORT - " & pcp_name & .LeftHeader
    .LeftFooter = "&""Arial Narrow""FOR: " & UCase(Format(Now, "MMM YYYY"))
    End With

    ' SET Print Area
    ActiveSheet.PageSetup.PrintArea = Range(Cells(1, 1), Cells(LastRow - 8, LastCol)).Address
    lTtlSheets = lTtlSheets + 1
    End If

    End If
    Next lSheet


    'SWITCH to PowerPivot
    Windows(templateFile).Activate

    'MsgBox sPDFPath & sPDFName & ".xlsx"

    '===========================================================================

    MkDir sPDFPath & pcp_name
    dirPath = sPDFPath & pcp_name & Application.PathSeparator

    'PRINT PCP Name.xlsx TO PDF
    ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    dirPath & pcp_name & ".pdf" _
    , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
    :=False, OpenAfterPublish:=False


    'SAVE PCP Name.xlsx and close it
    'SAVE Report Template as PCP Name.xlsx
    ActiveWorkbook.SaveAs Filename:=dirPath & sPDFName & ".xlsx"
    ActiveWorkbook.Save
    ActiveWindow.Close

    Next
    '***********End Logic***********

    Cleanup:
    'Release objects and terminate PDFCreator
    ' Set pdfjob = Nothing
    Shell "taskkill /f /im PDFCreator.exe", vbHide
    On Error GoTo 0
    Application.ScreenUpdating = True
    Exit Sub

    EarlyExit:
    'Inform user of error, and go to cleanup section
    MsgBox "There was an error encountered. PDFCreator has" & vbCrLf & _
    "has been terminated. Please try again.", _
    vbCritical + vbOKOnly, "Error"
    Resume Cleanup

    End Sub
    Last edited by jagadeesh.rt; 11-14-2019 at 12:09 PM. Reason: Plz suggest i am unable to print Excel and pdf file

  2. #2
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: PrintToExcelAndPDF file

    How about reading the forum rules and how to embed code tags?
    And what's this all about anyway?
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 5
    Last Post: 12-23-2022, 04:02 AM
  2. [SOLVED] File dialog box to open a excel file then copy data close file
    By mmikem in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-01-2019, 09:57 AM
  3. Macro - Open File Explorer, Select File, Import certain information from file
    By Drayde in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-25-2015, 08:58 AM
  4. Save as macro that specifies file type, file location and takes file name from three cells
    By ExcelFailure in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-13-2013, 10:09 PM
  5. Open 2nd file(CSV) from cell reference, copy columns to main file & close 2nd file
    By Langchop in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-31-2013, 05:09 AM
  6. Replies: 0
    Last Post: 01-27-2013, 12:13 PM
  7. [SOLVED] Macro - Master file to import data from another open file with variable file name
    By jdodz in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-10-2012, 10:56 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1