Results 1 to 12 of 12

Thread: protecting the spreadsheet causes xlConn fail

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    69

    Question protecting the spreadsheet causes xlConn fail

    Hi.

    I need to protect a spreadsheet and protecting the spreadsheet causes the .open of xlConn to fail.

    I need to check the protection status of the spreadsheet in vb and if protection is enabled, disable it, passing it the sheets unprotect password. Should be relatively simple using xlobjects.


    Can anyone help?
    Last edited by Firestart; Mar 7th, 2005 at 09:09 AM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    69

    Re: protecting the spreadsheet causes xlConn fail

    Can you determine if a sheet is password protected before
    opening in Excel?

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: protecting the spreadsheet causes xlConn fail

    You can not determine anything of a workbook until it is opened.
    So are we talking about the workbook having a
    password or just a particular sheet?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: protecting the spreadsheet causes xlConn fail

    If you just want to check if a sheet is Protected after the workbook is open then you can use
    the .Protect and .UnProtect for the sheets objects and the .Locked and .Protect for the range object.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    69

    Re: protecting the spreadsheet causes xlConn fail

    Thanks for your help. Can you show me what that would look like?

    Fire

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    69

    Re: protecting the spreadsheet causes xlConn fail

    Here is the code so far.....


    VB Code:
    1. Public Sub ConnectToServer(objConn, objSrv)
    2.  
    3. Dim strSrvName, strDBSName As String ' Declare Server Name and DB Name
    4.  
    5.     strSrvName = "****" 'Assign name to string
    6.     strDBSName = "****"
    7.    
    8.     Set objSrv = New SQLDMO.SQLServer
    9.     objSrv.Connect "BRCINT01", "****", "******"
    10.  
    11.     ' Instantiate an ADO Connection object and open it to the '
    12.     ' designated database on the designated server for the    '
    13.     ' designated database.                                    '
    14.     Set objConn = New ADODB.Connection
    15.     With objConn
    16.         .Provider = "sqloledb"
    17.         .ConnectionString = "data source = " & strSrvName & _
    18.                 ";" & "Initial Catalog = " & strDBSName & ";" & _
    19.                 "user id = *****; Password=******"
    20.  
    21.         .Open
    22.     End With
    23.  
    24. End Sub
    25. _________________________________________________________________________________________________________
    26. Public Sub GetXLRecordset(strXLFileName, strSQL, rsXl As ADODB.Recordset, xlConn As ADODB.Connection)
    27.  
    28. Dim strDefaultPath As String
    29. 'Populate a recordset from an excel spreadsheet.
    30.  
    31. 'This subroutine relies on the user correctly passing the name of the sheet withing
    32. 'the selected workbook.
    33.  
    34. 'If the workbook is protected it will not open.
    35.  
    36. 'On Error GoTo RECORDSETERROR
    37.  
    38.     With xlConn
    39.         .Provider = "Microsoft.Jet.OLEDB.4.0"
    40.         strDefaultPath = splitFilePath(strXLFileName)
    41.         .ConnectionString = "Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=" & strXLFileName & ";DefaultDir=c:\mypath;"
    42.         .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strXLFileName & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"
    43.         .ConnectionString = "Data Source=" & strXLFileName & _
    44.         "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=c:\txtFilesFolder\;Extensions=asc,csv,tab,txt;"
    45.         .Open
    46.     End With
    47.        
    48.     With rsXl
    49.         .ActiveConnection = xlConn
    50.         .CursorLocation = adUseClient
    51.         .CursorType = adOpenStatic
    52.         .Source = strSQL
    53.         .Open
    54.     End With
    55.    
    56.     Exit Sub
    57.    
    58. RECORDSETERROR:
    59.     MsgBox "Unable to get records from Excel workbook: " & strXLFileName & vbCr & _
    60.             vbCr & _
    61.             "Please check the format of the selected spreasheet and that no-one has the spreadsheet open. Error number: " & Err.Number
    62.  
    63.    
    64. End Sub
    Last edited by RobDog888; Mar 3rd, 2005 at 12:11 PM.

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: protecting the spreadsheet causes xlConn fail

    Try testing your connectstring this way.
    VB Code:
    1. With xlConn
    2.     .Provider = "Microsoft.Jet.OLEDB.4.0"
    3.     strDefaultPath = splitFilePath(strXLFileName)
    4.     .ConnectionString = "Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=" & strXLFileName & ";DefaultDir=c:\mypath;"
    5.     .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strXLFileName & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"
    6.     .ConnectionString = "Data Source=" & strXLFileName & _
    7.         "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=c:\txtFilesFolder\;Extensions=asc,csv,tab,txt;"
    8.     Debug.Print .ConnectionString '<--
    9.     .Open
    10. End With
    You need to concatinate your connectionstring or else only the last line assignment will be what
    it contains. Check the debug.print output.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    69

    Re: protecting the spreadsheet causes xlConn fail

    Hey,

    I am a bit of a novice. How would I run this as a macro? I dont know how to make this recognised as a macro on the 'play list'.

    Sounds silly I know...

  9. #9
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: protecting the spreadsheet causes xlConn fail

    Code for unprotecting and then protecting a worksheet !!

    VB Code:
    1. With Sheet1         ' < This should be the name of the YOUR worksheet
    2.     .Unprotect '(password - if required)
    3.      
    4.     'do stuff here !
    5.  
    6.     .Protect '(password - if required)
    7. End With

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: protecting the spreadsheet causes xlConn fail

    From the look of your connectionstring, its to correct. Checkout connectionstrings.com
    for the correct one to use.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: protecting the spreadsheet causes xlConn fail

    Quote Originally Posted by Firestart
    Hey,

    I am a bit of a novice. How would I run this as a macro? I dont know how to make this recognised as a macro on the 'play list'.

    Sounds silly I know...

    As soonn as you record a Macro, and stop the recording, whatever you recorded will be in the playlist. You can then edit that or play it back to make sure that it is correct. When it is, you can copy/paste it into your VB code, where it will require some modifications, but you can get the general idea of how to perform your task.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    69

    Question Re: protecting the spreadsheet causes xlConn fail

    David,

    I think I may have not communicated my question clearly. The code that I have inserted above in itself is not a macro, because it doesn't appear on my recording list. How do I make it a macro? In other words, How do I get
    Public Sub ConnectToServer(objConn, objSrv)

    Public Sub GetXLRecordset(strXLFileName, strSQL, rsXl As ADODB.Recordset, xlConn As ADODB.Connection)

    To run as a macro?
    Im here until I get good at this,.....the more help I get, the less silly questions you'll get! Thanks

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