Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open protected workbook from Access 1

Status
Not open for further replies.

geralf

Technical User
Apr 7, 2003
44
MX
Hi,

I'll try this here since it uses bothAccess and Excel.

I have automated transfer of records from Access to Excel, using he Excel Object Library. I use the GetObject method to get access to a specific Excel file. This file is protected with a password (which is known). How can I pass this to the workbook object, so the user don't get bothered with the 'password-dialog' box ?

I'm using Access 2000/Excel2000 and Win2000.

Any help greatly appreciated



Regards
Gerhard
 
The Excel model exposes all the necessary objects to do what you need. This example shows how to open a password-protected workbook, unprotect the workbook and all the sheets (if all the sheets use the same password). If you only have the 'Open' password to deal with then the function could be simplified:
Code:
Function OpenProtectedWorkBook(ByVal strWorkBookFile As String, _
                                ByVal strWorkBookOpenPWD As String, _
                                ByVal strWorkBookProtectPWD As String, _
                                ByVal strSheetProtectPWD As String) As Boolean
On Error GoTo ErrHandler

  Dim xl As Excel.Application
  Dim wb As Excel.Workbook
  Dim sht As Excel.Worksheet
  
  Set xl = New Excel.Application
  
  Set wb = xl.Workbooks.Open(filename:=strWorkBookFile, _
    Password:=strWorkBookOpenPWD, ReadOnly:=False, AddToMru:=False)
  
  wb.Unprotect strWorkBookProtectPWD
  
  For Each sht In wb.Sheets
      sht.Unprotect strSheetProtectPWD
  Next sht
  
  OpenProtectedWorkBook = True
  xl.Visible = True
    
ExitHere:
  Exit Function
ErrHandler:
  Select Case Err
    Case 1004           'Invalid password
      MsgBox "Password(s) invalid, please try again"
      xl.Quit   'Clean up app.
      Resume ExitHere
    Case Else
      MsgBox "Error: " & Err & " - " & Err.Description
      xl.Quit   'Clean up app.
      Resume ExitHere
  End Select
End Function

VBSlammer
redinvader3walking.gif

Unemployed in Houston, Texas
 
Excellent!

Just what I needed. I have one additional question. I have a function that returns trur or false, whether an instance of Excel is running. How do I open a workbook in the same instance?

Thanks for your help!


Regards
Gerhard
 
You can still use your GetObject or CreateObject logic to instantiate xl. Instead of this:
Code:
Set xl = New Excel.Application

Use something like this:
Code:
On Error Resume Next
Set xl = GetObject(, "Excel.Application")
If Err <> 0 Then
  Err.Clear
  Set xl = CreateObject(&quot;Excel.Application&quot;)
End If
On Error GoTo ErrHandler

VBSlammer
redinvader3walking.gif

Unemployed in Houston, Texas
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top