|
-
Mar 2nd, 2005, 05:08 AM
#1
Thread Starter
Lively Member
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.
-
Mar 2nd, 2005, 12:04 PM
#2
Thread Starter
Lively Member
Re: protecting the spreadsheet causes xlConn fail
Can you determine if a sheet is password protected before
opening in Excel?
-
Mar 2nd, 2005, 12:18 PM
#3
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Mar 2nd, 2005, 01:12 PM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Mar 3rd, 2005, 04:27 AM
#5
Thread Starter
Lively Member
Re: protecting the spreadsheet causes xlConn fail
Thanks for your help. Can you show me what that would look like?
Fire
-
Mar 3rd, 2005, 06:13 AM
#6
Thread Starter
Lively Member
Re: protecting the spreadsheet causes xlConn fail
Here is the code so far.....
VB Code:
Public Sub ConnectToServer(objConn, objSrv)
Dim strSrvName, strDBSName As String ' Declare Server Name and DB Name
strSrvName = "****" 'Assign name to string
strDBSName = "****"
Set objSrv = New SQLDMO.SQLServer
objSrv.Connect "BRCINT01", "****", "******"
' Instantiate an ADO Connection object and open it to the '
' designated database on the designated server for the '
' designated database. '
Set objConn = New ADODB.Connection
With objConn
.Provider = "sqloledb"
.ConnectionString = "data source = " & strSrvName & _
";" & "Initial Catalog = " & strDBSName & ";" & _
"user id = *****; Password=******"
.Open
End With
End Sub
_________________________________________________________________________________________________________
Public Sub GetXLRecordset(strXLFileName, strSQL, rsXl As ADODB.Recordset, xlConn As ADODB.Connection)
Dim strDefaultPath As String
'Populate a recordset from an excel spreadsheet.
'This subroutine relies on the user correctly passing the name of the sheet withing
'the selected workbook.
'If the workbook is protected it will not open.
'On Error GoTo RECORDSETERROR
With xlConn
.Provider = "Microsoft.Jet.OLEDB.4.0"
strDefaultPath = splitFilePath(strXLFileName)
.ConnectionString = "Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=" & strXLFileName & ";DefaultDir=c:\mypath;"
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strXLFileName & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"
.ConnectionString = "Data Source=" & strXLFileName & _
"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=c:\txtFilesFolder\;Extensions=asc,csv,tab,txt;"
.Open
End With
With rsXl
.ActiveConnection = xlConn
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.Source = strSQL
.Open
End With
Exit Sub
RECORDSETERROR:
MsgBox "Unable to get records from Excel workbook: " & strXLFileName & vbCr & _
vbCr & _
"Please check the format of the selected spreasheet and that no-one has the spreadsheet open. Error number: " & Err.Number
End Sub
Last edited by RobDog888; Mar 3rd, 2005 at 12:11 PM.
-
Mar 3rd, 2005, 12:16 PM
#7
Re: protecting the spreadsheet causes xlConn fail
Try testing your connectstring this way.
VB Code:
With xlConn
.Provider = "Microsoft.Jet.OLEDB.4.0"
strDefaultPath = splitFilePath(strXLFileName)
.ConnectionString = "Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=" & strXLFileName & ";DefaultDir=c:\mypath;"
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strXLFileName & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"
.ConnectionString = "Data Source=" & strXLFileName & _
"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=c:\txtFilesFolder\;Extensions=asc,csv,tab,txt;"
Debug.Print .ConnectionString '<--
.Open
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Mar 4th, 2005, 04:53 AM
#8
Thread Starter
Lively Member
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...
-
Mar 4th, 2005, 10:56 AM
#9
Re: protecting the spreadsheet causes xlConn fail
Code for unprotecting and then protecting a worksheet !!
VB Code:
With Sheet1 ' < This should be the name of the YOUR worksheet
.Unprotect '(password - if required)
'do stuff here !
.Protect '(password - if required)
End With
-
Mar 4th, 2005, 11:40 AM
#10
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Mar 5th, 2005, 12:38 PM
#11
Re: protecting the spreadsheet causes xlConn fail
 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.
-
Mar 7th, 2005, 05:20 AM
#12
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|