Print Excel file from command button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • araman
    New Member
    • Jan 2008
    • 53

    Print Excel file from command button

    Hello,

    Is it possible to print an excel file from a command button?

    Thanks
    Lance
  • nico5038
    Recognized Expert Specialist
    • Nov 2006
    • 3080

    #2
    Check out the Shell command.
    This wil allow starting every application, for excel use the excel .exe and the steering parm with the filename to print it.

    Nic;o)

    Comment

    • araman
      New Member
      • Jan 2008
      • 53

      #3
      Originally posted by nico5038
      Check out the Shell command.
      This wil allow starting every application, for excel use the excel .exe and the steering parm with the filename to print it.

      Nic;o)
      thanks, I will see if that works

      Comment

      • jaxjagfan
        Recognized Expert Contributor
        • Dec 2007
        • 254

        #4
        I forget all the specifcs but within Access should be something like this:

        Dim xlapp As Excel.Applicati on
        xlapp.Workbooks .Open "c:\myworkbook. xls"
        xlapp.ActiveShe et.PrintOut
        xlapp.Workbooks .Close

        This would assume you wanted to print the active sheet (the one in view when you first open excel.

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          Originally posted by araman
          Hello,

          Is it possible to print an excel file from a command button?

          Thanks
          Lance
          Place the following code in the Click() Event of a Command Button, make your own substitutions, then fire away. The code has been tested and is fully operational. Any questions, please feel free to ask
          [CODE=vb]
          '1st ==> Set a Reference to the Microsoft Excel X.X Object Library

          Dim strPathToExcel As String, strSpreadsheetN ame As String
          Dim strWorksheetNam e As String

          Dim ExcelApp As New Excel.Applicati on
          Dim ExcelBook As New Excel.Workbook
          Dim ExcelSheet As New Excel.Worksheet

          '******* Substitute your own values ********
          strPathToExcel = "C:\Test\" '*
          strSpreadsheetN ame = "Employees. xls" '*
          strWorksheetNam e = "Employees" '*
          '************** *************** ************'*

          'Let's not see what is going on
          ExcelApp.Visibl e = False

          Set ExcelBook = ExcelApp.Workbo oks.Open(strPat hToExcel & strSpreadsheetN ame)
          Set ExcelSheet = ExcelBook.Works heets(strWorksh eetName)

          ExcelSheet.Prin tOut

          ExcelApp.Quit
          Set ExcelApp = Nothing[/CODE]

          Comment

          • araman
            New Member
            • Jan 2008
            • 53

            #6
            Originally posted by ADezii
            Place the following code in the Click() Event of a Command Button, make your own substitutions, then fire away. The code has been tested and is fully operational. Any questions, please feel free to ask
            [CODE=vb]
            '1st ==> Set a Reference to the Microsoft Excel X.X Object Library

            Dim strPathToExcel As String, strSpreadsheetN ame As String
            Dim strWorksheetNam e As String

            Dim ExcelApp As New Excel.Applicati on
            Dim ExcelBook As New Excel.Workbook
            Dim ExcelSheet As New Excel.Worksheet

            '******* Substitute your own values ********
            strPathToExcel = "C:\Test\" '*
            strSpreadsheetN ame = "Employees. xls" '*
            strWorksheetNam e = "Employees" '*
            '************** *************** ************'*

            'Let's not see what is going on
            ExcelApp.Visibl e = False

            Set ExcelBook = ExcelApp.Workbo oks.Open(strPat hToExcel & strSpreadsheetN ame)
            Set ExcelSheet = ExcelBook.Works heets(strWorksh eetName)

            ExcelSheet.Prin tOut

            ExcelApp.Quit
            Set ExcelApp = Nothing[/CODE]


            Thank you very much,

            that is what i was looking for


            Lance

            Comment

            • araman
              New Member
              • Jan 2008
              • 53

              #7
              Originally posted by araman
              Thank you very much,

              that is what i was looking for


              Lance
              i get an error at

              Dim ExcelApp As New Excel.Applicati on

              That says
              compile error
              user defined type not defined

              Comment

              • ADezii
                Recognized Expert Expert
                • Apr 2006
                • 8834

                #8
                Originally posted by araman
                i get an error at

                Dim ExcelApp As New Excel.Applicati on

                That says
                compile error
                user defined type not defined
                You probably forgot to set a Reference to the Microsoft Excel X.X Object Library

                Comment

                • araman
                  New Member
                  • Jan 2008
                  • 53

                  #9
                  Originally posted by ADezii
                  You probably forgot to set a Reference to the Microsoft Excel X.X Object Library
                  Thanks for the reply.

                  How would I do that?

                  Comment

                  • araman
                    New Member
                    • Jan 2008
                    • 53

                    #10
                    Originally posted by araman
                    Thanks for the reply.

                    How would I do that?
                    ok figured out how to set the reference.

                    Now it stops saying the file cannot be found ...the path it show is the location of excel+the path to the file 'c:\excel\c:\do cuments\file.xl s cannot be found'

                    Comment

                    • jaxjagfan
                      Recognized Expert Contributor
                      • Dec 2007
                      • 254

                      #11
                      Originally posted by araman
                      ok figured out how to set the reference.

                      Now it stops saying the file cannot be found ...the path it show is the location of excel+the path to the file 'c:\excel\c:\do cuments\file.xl s cannot be found'
                      Post your code

                      the "c:" cannot exist twice in the path. You don't have to give the path to excel, just the excel spreadsheet

                      Comment

                      • araman
                        New Member
                        • Jan 2008
                        • 53

                        #12
                        Originally posted by jaxjagfan
                        Post your code

                        the "c:" cannot exist twice in the path. You don't have to give the path to excel, just the excel spreadsheet

                        I removed what's in bold before i got your reply and it worked. any downside to that?

                        Set ExcelBook = ExcelApp.Workbo oks.Open(strPathToExcel & strSpreadsheetN ame)

                        Comment

                        • jaxjagfan
                          Recognized Expert Contributor
                          • Dec 2007
                          • 254

                          #13
                          Originally posted by araman
                          I removed what's in bold before i got your reply and it worked. any downside to that?

                          Set ExcelBook = ExcelApp.Workbo oks.Open(strPathToExcel & strSpreadsheetN ame)
                          What was meant by the code you were given.
                          strPathToExcel is just the path to the document
                          strSpreadsheetN ame is just the name of the document.

                          If you put it all in one string and removed the other - it's not a problem.

                          Comment

                          • araman
                            New Member
                            • Jan 2008
                            • 53

                            #14
                            Originally posted by jaxjagfan
                            What was meant by the code you were given.
                            strPathToExcel is just the path to the document
                            strSpreadsheetN ame is just the name of the document.

                            If you put it all in one string and removed the other - it's not a problem.

                            I see, I thought strPathtoExcel was to the excel.exe. That make sense now.

                            Thanks to all for the help

                            Comment

                            Working...