+ Reply to Thread
Results 1 to 7 of 7

How to select a printer and number of pages within a macro

  1. #1
    Registered User
    Join Date
    07-08-2019
    Location
    London, England
    MS-Off Ver
    Office 2013
    Posts
    12

    Angry How to select a printer and number of pages within a macro

    I use Excel Macros for most of my repetitive tasks but have struggled to write one that will select different printers and then print a specified number of pages.
    Typically I fill out a form in Excel and when complete, save a pfd copy in a specific folder using the name in a cell on the spreadsheet and then print anywhere from 1-10 hard copies.

    I would normally record my actions and then tweak the macro but can't seem to change the printer, save the file as a cell reference or get the 'Copies' to look up a cell in the spreadsheet. Despite making the changes whilst recording the macro I only get the following which does not tell me where the file is being saved or what the name is.

    Sub Save_and_print()

    ActiveWindow.SelectedSheets.PrintOut From:=1, To:=1, Copies:=1, Collate _
    :=True, IgnorePrintAreas:=False

    ActiveWindow.SelectedSheets.PrintOut From:=1, To:=1, Copies:=4, Collate _
    :=True, IgnorePrintAreas:=False
    End Sub

    I then try to select the printer before each routing by changing the macro to the following:

    Sub Save_and_print()

    Application.acivePrinter = "Microsoft Print to PDF on Ne00:"

    ActiveWindow.SelectedSheets.PrintOut From:=1, To:=1, Copies:=1, Collate _
    :=True, IgnorePrintAreas:=False

    Application.acivePrinter = "Canon MG6200 series Printer on Ne01:"

    ActiveWindow.SelectedSheets.PrintOut From:=1, To:=1, Copies:=4, Collate _
    :=True, IgnorePrintAreas:=False
    End Sub

    and changing the 'Copies:=4,' to 'Copies:="L16"' which is referring to a dropdown cell using 1-10 and this is where it falls down.

    Does anyone understand what I am doing wrong as normally I can edit most parts of a recorded macro but have been struggling on printing for ages?
    Last edited by pvking; 07-08-2019 at 12:05 PM.

  2. #2
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,742

    Re: How to select a printer and number of pages within a macro

    Please Login or Register  to view this content.
    Happy with my answer * Add Reputation.
    If You are happy with solution, please use Thread tools and mark thread as SOLVED.

  3. #3
    Registered User
    Join Date
    07-08-2019
    Location
    London, England
    MS-Off Ver
    Office 2013
    Posts
    12

    Re: How to select a printer and number of pages within a macro

    Hi KOKOSEK and I changed the

    Copies=Range("L16").value

    to

    Copies:=Range("L16").value

    and it returned the cell value from L16 as

    Range("L16").Value="2 Pages"

    so was perfect. However I get a Run-time error '1004': Method 'PrintOut' of object 'Sheets' failed which does not make sense??

    I have done a workaround the first section which was printing to pdf and changed that to saving as a pdf so nailed that. This is the total macro to date

    Sub Save_and_print()
    '
    ' Save as pdf
    '
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "\\WDMYCLOUD\Euromatic\Invoices\" & Range("G3") & ".pdf", Quality:=xlQualityMinimum, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, From:=1, To:=1, _
    OpenAfterPublish:=True
    '
    ' Set the Printer
    '
    Application.acivePrinter = "Canon MG6200 series Printer on Ne01:"
    '
    ' Print desired number of copies
    '
    ActiveWindow.SelectedSheets.PrintOut From:=1, To:=1, Copies:=Range("L16").Value, Collate _
    :=True, IgnorePrintAreas:=False
    End Sub


    Any Advice would be fantastic

  4. #4
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,742

    Re: How to select a printer and number of pages within a macro

    I was expect that in L16 you've got a number only.
    If in L16 you got text like 'X pages', try like:

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    07-08-2019
    Location
    London, England
    MS-Off Ver
    Office 2013
    Posts
    12

    Re: How to select a printer and number of pages within a macro

    Very perceptive and hit the nail on the head as I was using a dropdown with 1 Page, 2 Pages..... and just changed it to a numerical value and works perfect - amazing!

    It still doesn't like setting the printer as I get a runtime error '438' Object doesn't support this property or method so for the moment just put ' in front and hope the last printer is the correct one. To find the correct wording of the 'printer', I used:

    Sub GetPrinterName()
    MsgBox Application.ActivePrinter
    End Sub


    so know it is correct.

    So this now can save a pdf in the same name as a cell in the spreadsheet and print off hard copied again to what is selected in the dropdown. The final plan is to make it idiot proof and get the macro to look to see if the file already exists before saving as it can overwrite without asking at the moment.

    Well done KOKOSEK

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: How to select a printer and number of pages within a macro

    Probably just a typo.

    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  7. #7
    Registered User
    Join Date
    07-08-2019
    Location
    London, England
    MS-Off Ver
    Office 2013
    Posts
    12

    Re: How to select a printer and number of pages within a macro

    Eagle-eyes Bakerman2 well spotted and all is perfect. We are doing tests as we speak and so far is looking good. The polished Macro is:
    Sub Save_and_print()
    '
    ' Save pdf as cell reference
    '
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "\\WDMYCLOUD\Euromatic\Invoices\" & Range("G3") & ".pdf", Quality:=xlQualityMinimum, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, From:=1, To:=1, _
    OpenAfterPublish:=True
    '
    ' Set the active Printer
    '
    Application.ActivePrinter = "Canon MG6200 series Printer on Ne01:"
    '
    ' Print desired number of copies from a cell
    '
    ActiveWindow.SelectedSheets.PrintOut From:=1, To:=1, Copies:=Range("K4").Value, Collate _
    :=True, IgnorePrintAreas:=False
    End Sub

    The only comment is per the earlier thread and making it idiot proof because if a pdf file with the same name already exists in the directory then the macro will overwrite without warning. I tried to write a line starting

    If FileExists("\\WDMYCLOUD\Euromatic\Invoices\" & Range("G3") & ".pdf") then MsgBox...

    but this fails and have no idea how to put up a warning message box even though I understand partly the

    MsgBox(Prompt,[Buttons As VbMsgBoxStyle = vbOkOnly],[Title],[HelpFile],[Context]) As vbMsgBoxResult

    Can anyone help on this so I can publish in case anyone else want to use it?
    Last edited by pvking; 07-10-2019 at 06:17 AM.

+ 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: 1
    Last Post: 06-30-2017, 10:29 AM
  2. Macro to first try default printer, otherwise select one
    By ErikB85 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-09-2017, 06:50 PM
  3. VBA or Macro to select a specific printer profile?
    By DanGres in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-14-2015, 02:39 PM
  4. [SOLVED] Macro that select the printer
    By robrobet in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-13-2015, 03:43 PM
  5. Macro to Select Printer
    By JoeyDvivre in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-23-2010, 02:03 AM
  6. Select Printer Macro
    By mdma in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-20-2008, 04:49 AM
  7. [SOLVED] User select printer in print macro
    By sarah_tennessee in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-07-2006, 03:00 PM

Tags for this Thread

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