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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

open excel sheet vba

Status
Not open for further replies.

cmz21

Programmer
May 5, 2005
110
0
0
US
Hello,
How can I open a password protected excel using vba. Can this be done using the GetObject method? I was using xlApp.Workbooks.open, but would like to try using GetObject.

Thanks
 
Hi.
Assuming that you intend to run Your code from a standard excel file, if you want to open another excel file called, say, "Myfile.xls" which has 11111111 as password, this is the code.
Workbooks.Open Filename:="Myfile.xls, password:="11111111"
Hope this helps.
Bye
Nick
 
Hello,
I am running the code from an Access Macro. I have tried using variations of the following code -


Set xlApp = CreateObject("Excel.Application")
xlApp.Workbooks.Open FileName:="c:filepath\filename", Password:="whatever"
Set xlBook = xlApp.Workbooks.Add
Set xlSheet_MetLife = xlBook.Worksheets.Add

This code creates a new workbook instead of using the other workbook. If I omit the createobject line, it gives me an object variable not set error.

I have also tried -


Set xlApp = GetObject("c:\filepath\filename.xls")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet_MetLife = xlBook.Worksheets.Add


but this won't let me set the password.

Any suggestions would be appreciated.

cmz






'Set xlApp = GetObject("c:\filepath\filename.xls")
xlApp.Workbooks.Open FileName:="c:filepath\filename", Password:="whatever"
Set xlBook = xlApp.Workbooks.Add
'Set xlBook = xlApp.Workbooks.Open FileName:="c:\filepath\filename.xls", Password:="whatever"
Set xlSheet_MetLife = xlBook.Worksheets.Add


 
I was able to get it working using the following code. The only problem I have now is killing all objects. I seem to have one which I cannot identify that keeps an instance of Excel running in the background.

Thanks for all help



Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Set xlApp = CreateObject("Excel.Application")
Set xlBook = Workbooks.Open(FileName:="c:\filepath\filename", Password:="password")
Set xlSheet = xlBook.Worksheets.Add
 
so have you set xlApp to nothing ??



Rgds, Geoff

Three things are certain. Death, taxes and lost data. DPlank is to blame

Please read FAQ222-2244 before you ask a question
 
I figure out the problem - some of my objects were not fully qualified.

Thanks for your help.

cmz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top