Consulting

Results 1 to 6 of 6

Thread: Solved: Error while printing pdf from excel

  1. #1

    Solved: Error while printing pdf from excel

    Hello Everyone,

    With the help of this forum, I got my solution to print pdf from excel spreadsheet with variable range.
    But I got error no 41 in the line .PrintArea = lc & lr

    Can anyone help me with my attached spreadsheet. (Also can I get the same output in word document format along with pdf)

    [vba]
    Sub pdf_doc()
    Dim lr As Long, lc As Long
    lr = Cells(Rows.Count, 8).End(xlUp).Row
    lc = Cells(Columns.Count, 8).End(xlToLeft).Column
    With ActiveSheet.PageSetup
    .PrintArea = lc & lr
    .PrintTitleRows = "$1:$10"
    .CenterFooter = "&P/&N"
    .PrintQuality = 600
    .Zoom = False
    .FitToPagesWide = 1
    .FitToPagesTall = False
    End With
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "C:\Users\Public\saabx.pdf", Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
    True
    End Sub
    [/vba]

    Advance thanks
    Attached Files Attached Files
    Last edited by Aussiebear; 03-09-2013 at 02:35 AM. Reason: Corrected the tags surrounding the code

  2. #2
    VBAX Regular
    Joined
    Mar 2013
    Posts
    38
    Location
    you should do something like this:

    [VBA]Sub pdf_doc()

    Dim lr As Long, lc As Long
    Dim mycolumnletter As String

    lr = Cells(Rows.Count, 8).End(xlUp).Row
    lc = Cells(Columns.Count, 8).End(xlToLeft).Column

    If lc > 26 Then
    mycolumnletter = Chr(Int((lc - 1) / 26) + 64) & Chr(((lc - 1) Mod 26) + 65)
    Else
    mycolumnletter = Chr(lc + 64)
    End If


    With ActiveSheet.PageSetup
    .PrintArea = "a1:" & mycolumnletter & lr
    .PrintTitleRows = "$1:$10"
    .CenterFooter = "&P/&N"
    .PrintQuality = 600
    .Zoom = False
    .FitToPagesWide = 1
    .FitToPagesTall = False
    End With

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=ActiveWorkbook.Path & "saabx.pdf", Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
    True

    End Sub

    [/VBA]
    "Amat Victoria Curam"

  3. #3
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    262
    Location
    Hi Abraham, this should address the print area issue and saving to PDF

    [VBA]Sub pdf_doc()

    Dim lr As Long
    Dim lc As Long
    Dim Printrng As Range

    lr = Cells(Rows.Count, 8).End(xlUp).Row
    lc = Cells(11, Columns.Count).End(xlToLeft).Column

    Set Printrng = Range(Cells(1, 1), Cells(lr, lc))

    With ActiveSheet.PageSetup
    .PrintArea = Printrng.Address
    .PrintTitleRows = "$1:$10"
    .CenterFooter = "&P/&N"
    .PrintQuality = 600
    .Zoom = False
    .FitToPagesWide = 1
    .FitToPagesTall = False
    End With

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "C:\Users\Public\saabx.pdf", Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True

    End Sub
    [/VBA]
    Last edited by sassora; 03-09-2013 at 11:59 AM. Reason: Saving part sorted - the was a permissions issue when saving to my C drive

  4. #4
    Thank you very much for spending ur time with my query.

    Is it possible to print the same data in word document format.

    that means when I run the macro, both pdf and doc should be printed simultaneously from the excel output

  5. #5
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    262
    Location
    Since you can't save the document as a Word Document, I gather that this isn't a particularly easy thing to do well

    See http://www.pcreview.co.uk/forums/pri...d-t922308.html for some code that may be of use.

  6. #6
    Hello Sassora,

    Thanks for your valuable comment. I don't know, might be silly for you. If the spreadsheet is saved in a particular path, then can we create the process.

    Thanks

Posting Permissions

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