Results 1 to 4 of 4
  1. #1
    baba is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Nov 2011
    Posts
    43

    Can We save Excel Workbook Report also as PDF Report using vba


    Code:
    Set Wkb1 = xlApp.Workbooks.Open(CurrentProject.Path & "\Book1.xlsx")
    Wkb1.Save // To save the Excel workbook after updating and writing contents.
    I use the above vba code snippet to save contents in an excel workbook and save it in my Current Folder.

    Is there a way to save the same Excel workbook Book1.xlsx as a pdf through MsAccess vba preserving the same
    positioning and formatting of different columns and contents in the Excel Workbook.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Although never tried, inclined to say yes. I have code behind a workbook that saves worksheet as PDF. This should be adaptable to run from Access to act on the Excel object that Access opened in VBA.

    Here is my workbook code:
    Code:
    Private Sub btnPDF_Click()
    Dim FName As Variant
    If ActiveWindow.SelectedSheets.Count > 1 Then
        MsgBox "More than the GEOReport sheet is selected, printing canceled."
    Else
        Call PrintSetup
        'Test If the Microsoft Add-in is installed
        If Dir(Environ("commonprogramfiles") & "\Microsoft Shared\OFFICE" _
             & Format(Val(Application.Version), "00") & "\EXP_PDF.DLL") = "" Then
             MsgBox "Error: MS Add-in not found. Contact Administrator"
        Else
            ChDrive (Left(ActiveWorkbook.Path, 1))
            ChDir (Left(ActiveWorkbook.Path, 1) & ":\")
            'Open the GetSaveAsFilename dialog to enter a file name for the pdf
            FName = Application.GetSaveAsFilename(gstrStateNum & "GeoReport.pdf", _
                                                    FileFilter:="PDF Files (*.pdf), *.pdf", _
                                                    Title:="Create PDF")
            'If you cancel this dialog Exit the function
            If FName <> False Then
                'test if PDF already exists in the folder
                If Dir(IIf(Not FName Like "*.pdf", FName = FName & ".pdf", FName) & "") <> "" Then
                    If MsgBox("Overwrite existing file?", vbYesNo) = vbNo Then
                        FName = InputBox("Enter file name.")
                    End If
                End If
                If Not FName Like "*.pdf" Then FName = FName & ".pdf"
                'Now the file name is correct we Publish to PDF
                On Error Resume Next
                ActiveSheet.ExportAsFixedFormat _
                        Type:=xlTypePDF, _
                        FileName:=FName, _
                        Quality:=xlQualityStandard, _
                        IncludeDocProperties:=True, _
                        IgnorePrintAreas:=False, _
                        OpenAfterPublish:=True
                On Error GoTo 0
            End If
        End If
    End If
    
    Private Sub PrintSetup()
    Dim intPages As Integer
    Dim intRows As Integer
    Dim strCell As String
    Dim i As Integer
    'number of records extracted
    intRows = Worksheets("Samples").Range("C4").Value
    'Calculate number of pages based on number of records extracted
    If intRows / 6 - Int(intRows / 6) <> 0 Then
        intPages = Int(intRows / 6) + 1
    Else
        intPages = Int(intRows / 6)
    End If
    With Worksheets("GEOReport").PageSetup
    '    .PrintTitleRows = "$1:$4"
    '    .PrintTitleColumns = ""
        .PrintArea = "$A2:$H" & intPages * 40 + 4
    '    .LeftHeader = ""
    '    .CenterHeader = ""
    '    .RightHeader = ""
    '    .CenterFooter = _
    '    "Gradation tests based on minus 3"" material." & IIf(optAASHTO, "  AASHTO Class may be inappropriate if Organic Content > 5%.", "") _
    '    & vbLf & vbLf & "Page &P of " & intPages
        .LeftFooter = "Gradation tests based on minus 3"" material."
        .RightFooter = "Page &P of " & intPages
    '    .LeftMargin = Application.InchesToPoints(0.75)
    '    .RightMargin = Application.InchesToPoints(0.25)
    '    .TopMargin = Application.InchesToPoints(1)
    '    .BottomMargin = Application.InchesToPoints(1)
    '    .HeaderMargin = Application.InchesToPoints(0.5)
    '    .FooterMargin = Application.InchesToPoints(0.5)
    '    .PrintHeadings = False
    '    .PrintGridlines = False
    '    .PrintComments = xlPrintNoComments
    '    .CenterHorizontally = False
    '    .CenterVertically = False
    '    .Orientation = xlPortrait
    '    .Draft = False
    '    .PaperSize = xlPaperLetter
    '    .FirstPageNumber = xlAutomatic
    '    .Order = xlDownThenOver
    '    .BlackAndWhite = False
    '    .Zoom = 100
    '    .PrintErrors = xlPrintErrorsDisplayed
    End With
    End Sub
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    baba is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Nov 2011
    Posts
    43
    Thanks. HOwever, I am trying to do it from MsAccess vba and the code resides in MsAccess. I found one more ...

    Code:
      Dim WBName, FilePath As String 
        WBName = ActiveWorkbook.Name 
        FilePath = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\" & WBName & ".pdf" 
         
        Sheets.Select 
        ActiveSheet.ExportAsFixedFormat _ 
        Type:=xlTypePDF, _ 
        Filename:=FilePath, _ 
        Quality:=xlQualityStandard, IncludeDocProperties:=True, _ 
        IgnorePrintAreas:=False, OpenAfterPublish:=True

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by baba View Post
    Thanks. HOwever, I am trying to do it from MsAccess vba and the code resides in MsAccess...
    June already provided you with an example.

    Code:
                ActiveSheet.ExportAsFixedFormat _
                        Type:=xlTypePDF, _
                        FileName:=FName, _
                        Quality:=xlQualityStandard, _
                        IncludeDocProperties:=True, _
                        IgnorePrintAreas:=False, _
                        OpenAfterPublish:=True
    You said you have this
    Code:
    Set Wkb1 = xlApp.Workbooks.Open(CurrentProject.Path & "\Book1.xlsx")
    So get a hold of the worksheet and activate it. Then use the code provided earlier.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 04-21-2015, 11:33 AM
  2. Save and close Excel Workbook
    By scoe in forum Access
    Replies: 5
    Last Post: 07-30-2013, 08:50 AM
  3. Replies: 2
    Last Post: 05-16-2013, 07:43 PM
  4. Replies: 10
    Last Post: 06-20-2012, 09:50 AM
  5. Replies: 6
    Last Post: 12-12-2011, 09:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums