+ Reply to Thread
Results 1 to 9 of 9

VB Code working in 2010 but not 2003?

Hybrid View

Margate VB Code working in 2010 but... 08-08-2013, 10:34 AM
JasperD Re: VB Code working in 2010... 08-08-2013, 10:43 AM
Margate Re: VB Code working in 2010... 08-08-2013, 10:48 AM
Andy Pope Re: VB Code working in 2010... 08-08-2013, 10:53 AM
Margate Re: VB Code working in 2010... 08-08-2013, 11:35 AM
Andy Pope Re: VB Code working in 2010... 08-08-2013, 11:42 AM
Margate Re: VB Code working in 2010... 08-08-2013, 01:02 PM
Andy Pope Re: VB Code working in 2010... 08-09-2013, 05:47 AM
Margate Re: VB Code working in 2010... 08-09-2013, 10:33 AM
  1. #1
    Forum Contributor
    Join Date
    01-18-2013
    Location
    England
    MS-Off Ver
    2016 (Mac)
    Posts
    149

    Unhappy VB Code working in 2010 but not 2003?

    Hello Everyone,

    I built a sheet in Excel 2010 that contains the following VB code:

    Private Sub Workbook_Open()
    Application.CommandBars.FindControl(ID:=2521).Enabled = False
    End Sub

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Application.CommandBars.FindControl(ID:=2521).Enabled = True
    End Sub
    this works fine in 2010 but now the sheet needs to run in Excel 2003. The problem
    is that when I open the sheet in 2003 I am met with the following error message:

    Run-time error '91':
    Object variable or With block variable not set

    I am just trying to disable the toolbar printer icon so that I cannot be pressed!
    In 2010 the above code fades the icon out disabling it.

    I only want the user to print using a button that has a macro attached to it already. The reason is because if they print using the print button on the toolbar every page will print! The macro that is attached to the print button that I have created will print only a specified page break range! If they print from the tool bar button 47 pages will print instead of just 1!

    Any help is much appreciated.

    Regards,

    Margate
    Last edited by Margate; 08-08-2013 at 10:50 AM. Reason: Provide more guidance in order to help find a solution.

  2. #2
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: VB Code working in 2010 but not 2003?

    If you want to prevent the user from printing the file, why not just like this...?
    Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Cancel = True
    End Sub
    (the reason why it works in 2010 and not in 2003 is that the id numbers are different -- in 2003 the code number for print is 4)

    Please click the * below if this helps
    Please click the * below if this helps

  3. #3
    Forum Contributor
    Join Date
    01-18-2013
    Location
    England
    MS-Off Ver
    2016 (Mac)
    Posts
    149

    Re: VB Code working in 2010 but not 2003?

    Hello JasperD,
    I forgot to mention in my post that I only want the user to print using the button that has a macro attached to it. The reason is because if they print using the print button every page will print! The macro that is attached to the print button will print only a specified page break range! If they print from the tool bar button 47 pages will print instead of just 1!

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,480

    Re: VB Code working in 2010 but not 2003?

    You will get that if the control is not visible on a commandbar.
    Cheers
    Andy
    www.andypope.info

  5. #5
    Forum Contributor
    Join Date
    01-18-2013
    Location
    England
    MS-Off Ver
    2016 (Mac)
    Posts
    149

    Re: VB Code working in 2010 but not 2003?

    Hello Andy Pope,
    With the printer disabled in the toolbar the sheet would still print out with no problem in Excel 2010. I am looking for a way to disable the print icon in the toolbar so that the user can print only by using the button that I want them to use. Is the code above not adaptable for use in Excel 2003?

    Thank you for your help.

    Margate

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,480

    Re: VB Code working in 2010 but not 2003?

    Why not set the print area if all you want is to restrict what is printed?

    In xl2003 I'm sure you could still use CTRL+P to print even if the button was visible and disabled.

  7. #7
    Forum Contributor
    Join Date
    01-18-2013
    Location
    England
    MS-Off Ver
    2016 (Mac)
    Posts
    149

    Re: VB Code working in 2010 but not 2003?

    I would do that but there are around 20 macros on the sheet that each print a different area of the page. Macro1 will print page 1 Macro2 will print page 3 etc. Page 2 (in this example) is dead space!

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,480

    Re: VB Code working in 2010 but not 2003?

    Why not extend JasperD's suggestion.

    If your print macros set the value of a public variable you can then check that and print the relevant section or simple cancel the print.

  9. #9
    Forum Contributor
    Join Date
    01-18-2013
    Location
    England
    MS-Off Ver
    2016 (Mac)
    Posts
    149

    Re: VB Code working in 2010 but not 2003?

    Solved!,
    I ended up using the following code:

    Private Sub Workbook_BeforePrint(Cancel As Boolean)
        Cancel = True
        MsgBox "Please use the Print buttons located on the worksheet. ", vbExclamation, "Print Canceled"
    End Sub
    
    Sub PrintPageOne()
        Application.EnableEvents = False
        ActiveWindow.SelectedSheets.PrintOut From:=1, To:=1, Copies:=1, Collate:=True
        Application.EnableEvents = True
    End Sub
    When the print button is pressed the print will be cancelled and an alert box will appear requesting the user to use the on page buttons...

    Thank you for your help

+ 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. VBA code created in Excel 2003, not working in Excel 2010, Getting error message 400.
    By rajeshapril14 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-24-2012, 05:19 AM
  2. coding previously working in Excel 2003 version not working in Excel 2010
    By petercalbra in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-11-2012, 10:51 AM
  3. Excel 2003 macro not working in 2010
    By davides in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-24-2012, 07:04 PM
  4. Save As not working in 2010 but worked in 2003
    By mkremeier in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-04-2012, 02:40 AM
  5. Code From 2003 to 2010 excel not working
    By realniceguy5000 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-20-2011, 12:41 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