Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 53
Default How to set workbook reference - basics

I'm using late binding, and for some reason I'm getting lost trying to set a
reference to a Workbook object:

Dim xlapp As Object
Dim xlwkbs As Object
Dim xlwkb As Object
Set xlapp = CreateObject("Excel.Application")
Set xlwkbs = xlapp.Workbooks

so far so good (I think), but when I try to set a reference to a Workbook, I
get a "Subscript out of range" error

Set xlwkb = xlwkbs(strPathToFile)

What I want to do is set the reference and then pass xlwkb to another
function and do stuff like add worksheets. Do I need to set the reference
differently?

Thanks in advance.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default How to set workbook reference - basics

if strPathtoFile is something like "C:\MyFolcer\MyFile.xls" then that is
your problem.

the argument to Workbooks must be of the form Myfile.xls
and Myfile.xls must be open in excel.

--
Regards,
Tom Ogilvy

"deko" wrote in message
m...
I'm using late binding, and for some reason I'm getting lost trying to set

a
reference to a Workbook object:

Dim xlapp As Object
Dim xlwkbs As Object
Dim xlwkb As Object
Set xlapp = CreateObject("Excel.Application")
Set xlwkbs = xlapp.Workbooks

so far so good (I think), but when I try to set a reference to a Workbook,

I
get a "Subscript out of range" error

Set xlwkb = xlwkbs(strPathToFile)

What I want to do is set the reference and then pass xlwkb to another
function and do stuff like add worksheets. Do I need to set the reference
differently?

Thanks in advance.




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 53
Default How to set workbook reference - basics

if strPathtoFile is something like "C:\MyFolcer\MyFile.xls" then that is
your problem.

the argument to Workbooks must be of the form Myfile.xls
and Myfile.xls must be open in excel.


Thanks for the reply.

I'm wondering if I need to create a worksheet object first:

Set xlwks = CreateObject("Excel.Sheet")

Is this correct? And then get the workbook like this:

Set xlwkb = xlwks.Parent

I think once I have the objects set, I'll be on the right track. I might
have to go back to early binding otherwise...


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default How to set workbook reference - basics

Dim xlapp As Object
Dim xlwkbs As Object
Dim xlwkb As Object
Set xlapp = CreateObject("Excel.Application")
' Set xlwkbs = xlapp.Workbooks
strPathtoFile = "C:\MyFolder\MyFile.xls"
Set xlwkbk = xlApp.workbooks.Open(strPathtoFile)
set xlwks = xlwkbk.Worksheets("Sheet1")

or

set xlwkbk = xlApp.Workbooks.Add
set xlwks = xlwkbk.Worksheets("Sheet1")
The workbook will be added with the number of sheets set in tools options
but will have at least one worksheet as a minimum.

--
Regards,
Tom Ogilvy


"deko" wrote in message
m...
if strPathtoFile is something like "C:\MyFolcer\MyFile.xls" then that

is
your problem.

the argument to Workbooks must be of the form Myfile.xls
and Myfile.xls must be open in excel.


Thanks for the reply.

I'm wondering if I need to create a worksheet object first:

Set xlwks = CreateObject("Excel.Sheet")

Is this correct? And then get the workbook like this:

Set xlwkb = xlwks.Parent

I think once I have the objects set, I'll be on the right track. I might
have to go back to early binding otherwise...




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
DSUM Basics Midland Excel Discussion (Misc queries) 1 August 26th 09 06:15 PM
basics? n21 New Users to Excel 7 September 21st 06 12:37 PM
Hi I'm New and need help with Excel Basics abi Excel Discussion (Misc queries) 10 November 23rd 05 07:25 PM
Reference code in another workbook from a calling workbook Datasort Excel Programming 1 January 4th 05 01:13 AM
Excel VBA basics Philipp Excel Programming 3 October 11th 04 10:11 AM


All times are GMT +1. The time now is 08:12 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"