Consulting

Results 1 to 19 of 19

Thread: Write in a protected closed workbook

  1. #1
    VBAX Tutor gnod's Avatar
    Joined
    Apr 2006
    Posts
    257
    Location

    Write in a protected closed workbook

    hi,

    this is the code i use to write in a closed workbook written by tstom

    [VBA]
    Sub Write2ClosedBookSingleCellRange(WorkbookFullName As String, SQL As String, NewValue)
    Dim conn As New Connection, rs As New Recordset

    conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & WorkbookFullName & _
    ";Extended Properties=""Excel 8.0;HDR=NO;"""
    rs.Open SQL, conn, 1, 3
    rs.Fields(0).Value = NewValue
    rs.Update: rs.Close: conn.Close
    End Sub
    [/VBA]

    how can i unprotect if the sheet in the workbook is protected?


  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Open it and unprotect it in the code.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Tutor gnod's Avatar
    Joined
    Apr 2006
    Posts
    257
    Location
    is it possible not to open the closed workbook but it can unprotect it via VBA code?

    here's the template i use modifying the closed workbook. i need to modify all the excel file under the "For Modify" folder without opening the file but the problem is if the sheet is protected..

    thanks..

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Don't think so because the unprotect is an Excel function, not something in ADO.

    Why can't you open it? You can write to it, so ...
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Tutor gnod's Avatar
    Joined
    Apr 2006
    Posts
    257
    Location
    because i already deploy my annual budget template and i want the user just to click a button to modify the data for mid-year budget without opening the workbook..

    thanks for your reply..

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    How about a database?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You can always open the workbook, make it non-visible, update it, and close it again.

    This could all be driven from your button and the user would be none the wiser.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Tutor gnod's Avatar
    Joined
    Apr 2006
    Posts
    257
    Location
    Quote Originally Posted by xld
    How about a database?
    i use the excel sheet as a database which i'm trying to modify it and put it in one folder ("For Modify")

  9. #9
    VBAX Tutor gnod's Avatar
    Joined
    Apr 2006
    Posts
    257
    Location
    Quote Originally Posted by xld
    You can always open the workbook, make it non-visible, update it, and close it again.

    This could all be driven from your button and the user would be none the wiser.
    but my mid-yr budget template is almost 4MB. It will slow down the process of opening, non-visible, update and close.. and also the no. of files under the For Modify folder will consist of 30-40 excel files..

    can you give me a sample on your process? thanks..

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    So why is the workbook protected?

    And what process is that?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  11. #11
    VBAX Tutor
    Joined
    Aug 2007
    Posts
    273
    Location
    you chould check to see if the files is protected, and only open it if it is.

  12. #12
    VBAX Tutor gnod's Avatar
    Joined
    Apr 2006
    Posts
    257
    Location
    Quote Originally Posted by xld
    So why is the workbook protected?
    the worksheet is protected so that I and the department I belong can modify the data in the worksheet but the user who will do the budget can only view..

    Quote Originally Posted by xld
    And what process is that?
    a sample of opening, make non-visible, update, and close..


    thanks..

  13. #13
    I agree with Bob this is screaming for a DB solution in Access or some other PROPER DB
    2+2=9 ... (My Arithmetic Is Mental)

  14. #14
    VBAX Tutor gnod's Avatar
    Joined
    Apr 2006
    Posts
    257
    Location
    Quote Originally Posted by unmarkedhelicopter
    I agree with Bob this is screaming for a DB solution in Access or some other PROPER DB
    thanks, i will enhance my budget template for the next annual budget but for now i need to think for a workaround because the annual budget template is already distributed and the budget is done..

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location

    Post

    Quote Originally Posted by gnod
    a sample of opening, make non-visible, update, and close..

    [vba]

    Set oWB = Workbooks.Open("wb_path_and_name")

    Windows(oWB.Name).Visible = False

    With OWB.Worksheets(1)
    .Unprotect
    .Range("A1").Value = some_value
    'and more
    .Protect
    End With

    oWB.Close savechanges:= False
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  16. #16
    gnod,

    I have a similar post that just was resolved. Form me I unprotected the sheet, did my VBA stuff, then protected it before exiting VBA. I think we have similar problems. Here is what I did in my code:

        'Allow VBA to write to protected cells
        ActiveSheet.Unprotect "password"
    
        ' Do stuff like process a checkbox click command
    
        'Protect the cells again
        ActiveSheet.Protect "password"
    Obviously in the case above the sheet had been protected with the password "password". Hope this helps...

    Jeff

  17. #17
    VBAX Tutor gnod's Avatar
    Joined
    Apr 2006
    Posts
    257
    Location
    Quote Originally Posted by sparafucile1
    gnod,

    I have a similar post that just was resolved. Form me I unprotected the sheet, did my VBA stuff, then protected it before exiting VBA. I think we have similar problems. Here is what I did in my code:

     
        'Allow VBA to write to protected cells
        ActiveSheet.Unprotect "password"
     
        ' Do stuff like process a checkbox click command
     
        'Protect the cells again
        ActiveSheet.Protect "password"
    Obviously in the case above the sheet had been protected with the password "password". Hope this helps...

    Jeff
    but the problem is i need to unprotect the sheet while the workbook is close.. thanks..

  18. #18
    VBAX Tutor gnod's Avatar
    Joined
    Apr 2006
    Posts
    257
    Location
    Quote Originally Posted by xld
    [vba]
    oWB.Close savechanges:= False
    [/vba]
    why is it False?
    thanks for the sample..

  19. #19
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    That was a loigic error my part, you should save it and close it.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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