open specific Excel Worksheet name

I need to open a particular sheet in an excel workbook.  I tried:
    Set wbk = appExcel.Workbooks.Open(strImportFile)
    Set wks = wbk.Worksheets(rstImportSpec!SheetName)
where SheetName = 'Main Page' but I get a 'Type Mismatch' error
However  Set wks = wbk.Worksheets(1) works perfectly.  The problem is sometimes the sheets are not all the same in all workbooks but the particular Sheet Name always exists

what am I doing wrong?

Michael
LVL 7
UniqueDataAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TextReportCommented:
try

Set wks = wbk.Sheets(rstImportSpec!SheetName)

Cheers, Andrew
0
Malik1947Commented:
use this to access a specific sheet

Sheet name is a string.


Set wks = wbk.Sheets(SheetName)

Open in new window

0
Rey Obrero (Capricorn1)Commented:
dim xlObj as object, xlSht as object
set xlObj=createobject("excel.application")
      xlObj.workbooks.open(strImportFile)

set xlSht=xlobj.activeworkbook.worksheets(rstImportSpec("Sheetname"))
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

MikeTooleCommented:
It should work, make sure that the names are the same:

    Set wbk = appExcel.Workbooks.Open(strImportFile)
Debug.Print wkb.Worksheets(1).name,  rstImportSpec!SheetName
stop
    Set wks = wbk.Worksheets(rstImportSpec!SheetName)
0
Rey Obrero (Capricorn1)Commented:
slight correction

dim xlObj as object, xlSht as object
set xlObj=createobject("excel.application")
      xlObj.workbooks.open(strImportFile)

set xlSht=xlobj.activeworkbook.worksheets(cstr(rstImportSpec("Sheetname")))
0
Rey Obrero (Capricorn1)Commented:
or in your code

Set wks = wbk.Worksheets(cstr(rstImportSpec!SheetName))
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
UniqueDataAuthor Commented:
Sheets did not work.  It looks like the trick was cstr as cap suggested.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.