Results 1 to 14 of 14

Thread: Excel VBA - OLEObject Error When WorkBook Is Set As "Shared"

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Excel VBA - OLEObject Error When WorkBook Is Set As "Shared"

    I have a command button on a worksheet (not on a UserForm). It is a save button, and it used to be simply enabled/disabled depending on whether it was eligible for use.

    It has been discovered, however, the folks are not using the save button. Rather then are saving their spreadsheets using the build-in Excel Save functions. The problem is that my button does more than just save their work. It also takes pieces of information off of their spreadsheet and puts it on a summary spreadsheet that goes to management in the form of a report. There in lies the problem when they do not use my save button.

    So, one suggestion was to make it really big and change the back/fore colors on it to make it standout. I did that, and here is the resulting code.
    vb Code:
    1. With cmdSaveHFR
    2.     .Enabled = True
    3.     .BackColor = vbRed
    4.     .ForeColor = vbYellow
    5.     .Height = 43.5
    6.     .Left = 8.25
    7.     .Top = 178.5
    8.     .Width = 108.5
    9.     .Caption = "Click ME To Save HFR To Disc And To Update HFR Dbase File"
    10. End With
    11.  
    12. 'this puts it back to normal
    13.  
    14. With cmdSaveHFR
    15.      .Enabled = False
    16.      .BackColor = &HE0E0E0
    17.      .ForeColor = &HC00000
    18.      .Height = 19.5
    19.      .Left = 8.25
    20.      .Top = 189.75
    21.      .Width = 108.75
    22.      .Caption = "Save HFR"
    23. End With
    This works just fine until I set the spreadsheet with the button on it to "Shared" - when it is shared, and it must be shared as it is a menu type spreadsheet used by several different people in three different departments, my button sizing code generates a 1004 error, Unable to set the Height property of the OLEObject Class (if I comment out .Height =, then it will go right through the .Left .Top and .Width properties with this same error.)

    So, my question is why can I change the buttons height, left, top and width when the workbook is NOT "Shared", but get an OLEObject error when attempting to make the same change when the workbook is "Shared"?

  2. #2
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Excel VBA - OLEObject Error When WorkBook Is Set As "Shared"

    I think with a shared workbook, you can only modify data but you cannot modify any structure or object.

    To prevent user uses the Excel built-in Save button or menu you can do it like this:

    In ThisWorkbook code module:
    Code:
    Option Explicit
    
    Public bAllowSave As Boolean
    
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
       If bAllowSave = False Then
          MsgBox "Please Click button [Save HFR] to Save the workbook.", _
                 vbExclamation, "Cannot Save Now"
          Cancel = True
       End If
    End Sub
    In the code module of the sheet that contains the button [cmdSaveHFR] :
    Code:
    Option Explicit
    
    Private Sub cmdSaveHFR_Click()
       ThisWorkbook.bAllowSave = True
       
       '-- your extra code before saving here
       
       ThisWorkbook.Save
       ThisWorkbook.bAllowSave = False
    End Sub
    However, remember that those won't prevent users to disable/stop macros then save with Excel built-in Save function.
    Last edited by anhn; Apr 13th, 2009 at 09:44 AM.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Excel VBA - OLEObject Error When WorkBook Is Set As "Shared"

    Quote Originally Posted by anhn View Post
    I think with a shared workbook, you can only modify data but you cannot modify any structure or object
    Ahhhhh....well, that is a pain - but thanks for the info.

    I'll play around with your suggestions and be back.

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Excel VBA - OLEObject Error When WorkBook Is Set As "Shared"

    @hack: If you remember this thread

    http://www.vbforums.com/showpost.php...6&postcount=28
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Excel VBA - OLEObject Error When WorkBook Is Set As "Shared"

    Quote Originally Posted by koolsid View Post
    The date on that is January 2008 - I can't remember what I did this morning.

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Excel VBA - OLEObject Error When WorkBook Is Set As "Shared"

    Quote Originally Posted by Hack View Post
    The date on that is January 2008 - I can't remember what I did this morning.
    Yeah I know that is a very old thread

    You cannot modify the height/width etc of an ole object in shared mode.

    There in lies the problem when they do not use my save button.
    The link that I gave is the solution to this particular problem
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Excel VBA - OLEObject Error When WorkBook Is Set As "Shared"

    I tried this
    Code:
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
    If SaveAsUI = False Then
    
        Cancel = True
    
        MsgBox "You cannot save this workbook.  Use Save As"
    
    End If
    
    End Sub
    I put a break on the line "Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)" so I could walk myself through this.

    I clicked the "Save" icon on the tool bar. I clicked File/Save and File/Save As from the dropdown menu and completed the save each of the three times. At no time was this routine ever hit. It just didn't run.

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Excel VBA - OLEObject Error When WorkBook Is Set As "Shared"

    Where did you place the code... You have to place it in the workbook code area... I just tried it and it worked...

    See picture attached....
    Attached Images Attached Images   
    Last edited by Siddharth Rout; Apr 13th, 2009 at 11:30 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  9. #9

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Excel VBA - OLEObject Error When WorkBook Is Set As "Shared"

    The BeforeSave thing was what I was playing with and I got no stoppage and no msgbox.
    Attached Files Attached Files

  10. #10
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Excel VBA - OLEObject Error When WorkBook Is Set As "Shared"

    Your code didn't run perhaps you were in Design mode (macro was disable/stop)

    Back to your original design in post#1, you can get rid of cmdSaveHFR and alow user to use built-in Save function with:
    Code:
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
       If SaveAsUI = True Then '-- with an unSaved (new) workbook this is true
          Cancel = True '-- do not allow SaveAs on shared workbook
          Exit Sub
       End If
       
       MsgBox "Your extra code"
       '-- your extra code before save here
    
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  11. #11
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Excel VBA - OLEObject Error When WorkBook Is Set As "Shared"

    Hi Hack

    I tried it and it works just fine...

    Is macros enabled in your workbook?

    If not set it to low

    Tools=>Macro=>Security=>Low

    Hope this helps...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  12. #12

    Thread Starter
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Excel VBA - OLEObject Error When WorkBook Is Set As "Shared"

    Quote Originally Posted by koolsid View Post
    Hi Hack

    I tried it and it works just fine...

    Is macros enabled in your workbook?

    If not set it to low

    Tools=>Macro=>Security=>Low

    Hope this helps...
    Macros are enabled in all workbooks and I can't reset the the macro security. That ability is restricted to admins.

  13. #13
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Excel VBA - OLEObject Error When WorkBook Is Set As "Shared"

    I can't reset the the macro security. That ability is restricted to admins.
    hack what is it set to?

    Low/medium/High ?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  14. #14
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel VBA - OLEObject Error When WorkBook Is Set As "Shared"

    hack what is it set to?

    Low/medium/High ?
    as he is running other macros, i doubt that security is the issue

    i assume that the code, for before_save, is in the thisworkbook code module, for the workbook that you are wanting to modify the save command

    the sample by kool in post #9, i tested and works for me
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width