Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
joe joe is offline
external usenet poster
 
Posts: 4
Default Reference the active sheet

If I run the following code and I have an open spreadsheet with existing
sheets, I get another instance of Excel w/no sheets. How can I reference
the active spreadsheet(w/o knowing the filename?)

Dim oXL as object
Dim xlWorksheet as object

Set oXL = GetObject("", "Excel.Application")
Set xlWorkSheet = oXL.Worksheets.Add

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Reference the active sheet

If you don't know the filename, you cannot control with instance
of Excel GetObject is going to return.



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"joe" wrote in message
...
If I run the following code and I have an open spreadsheet with
existing
sheets, I get another instance of Excel w/no sheets. How can I
reference
the active spreadsheet(w/o knowing the filename?)

Dim oXL as object
Dim xlWorksheet as object

Set oXL = GetObject("", "Excel.Application")
Set xlWorkSheet = oXL.Worksheets.Add

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6
Default Reference the active sheet

Then perhaps I shouldn't be running GetObject at all? I just need a
reference (from Word VBA) to the active Excel document, without knowing
its name. Is this possible?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Reference the active sheet

Then perhaps I shouldn't be running GetObject at all? I just need a
reference (from Word VBA) to the active Excel document, without knowing
its name. Is this possible?


"Chip Pearson" wrote in message
...
If you don't know the filename, you cannot control with instance of Excel
GetObject is going to return.



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"joe" wrote in message
...
If I run the following code and I have an open spreadsheet with existing
sheets, I get another instance of Excel w/no sheets. How can I reference
the active spreadsheet(w/o knowing the filename?)

Dim oXL as object
Dim xlWorksheet as object

Set oXL = GetObject("", "Excel.Application")
Set xlWorkSheet = oXL.Worksheets.Add

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Reference the active sheet

Jimmy,

GetObject is used to get a reference to an existing running
instance of Excel. It will fail and return Nothing if there is no
running instance of Excel. If there is more than one instance
running, you have no control over which instance to which you
will get a reference. Perhaps code like the following will work:

Dim XL As Excel.Application
Dim WB As Excel.Workbook
On Error Resume Next
Set XL = GetObject(,"Excel.Application")
If XL Is Nothing Then
Set XL = CreateObject("Excel.Application")
End If
If Not XL.ActiveWorkbook Is Nothing Then
Set WB = XL.ActiveWorkbook
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com





"Jimmy" wrote in message
ink.net...
Then perhaps I shouldn't be running GetObject at all? I just
need a
reference (from Word VBA) to the active Excel document, without
knowing
its name. Is this possible?


"Chip Pearson" wrote in message
...
If you don't know the filename, you cannot control with
instance of Excel GetObject is going to return.



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"joe" wrote in message
...
If I run the following code and I have an open spreadsheet
with existing
sheets, I get another instance of Excel w/no sheets. How can
I reference
the active spreadsheet(w/o knowing the filename?)

Dim oXL as object
Dim xlWorksheet as object

Set oXL = GetObject("", "Excel.Application")
Set xlWorkSheet = oXL.Worksheets.Add

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!









  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Reference the active sheet

If I know only one document is running, will
Set XL = GetObject(,"Excel.Application")
return a reference to that doc?
"Chip Pearson" wrote in message
...
Jimmy,

GetObject is used to get a reference to an existing running instance of
Excel. It will fail and return Nothing if there is no running instance of
Excel. If there is more than one instance running, you have no control
over which instance to which you will get a reference. Perhaps code like
the following will work:

Dim XL As Excel.Application
Dim WB As Excel.Workbook
On Error Resume Next
Set XL = GetObject(,"Excel.Application")
If XL Is Nothing Then
Set XL = CreateObject("Excel.Application")
End If
If Not XL.ActiveWorkbook Is Nothing Then
Set WB = XL.ActiveWorkbook
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com





"Jimmy" wrote in message
ink.net...
Then perhaps I shouldn't be running GetObject at all? I just need a
reference (from Word VBA) to the active Excel document, without knowing
its name. Is this possible?


"Chip Pearson" wrote in message
...
If you don't know the filename, you cannot control with instance of
Excel GetObject is going to return.



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"joe" wrote in message
...
If I run the following code and I have an open spreadsheet with
existing
sheets, I get another instance of Excel w/no sheets. How can I
reference
the active spreadsheet(w/o knowing the filename?)

Dim oXL as object
Dim xlWorksheet as object

Set oXL = GetObject("", "Excel.Application")
Set xlWorkSheet = oXL.Worksheets.Add

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!








  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Reference the active sheet

Jimmy,

No, GetObject will not return a reference to the active workbook.
It returns a reference to the Excel Application. Look at the code
I posted. The variable WB is set to the active workbook.



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"Jimmy" wrote in message
ink.net...
If I know only one document is running, will
Set XL = GetObject(,"Excel.Application")
return a reference to that doc?
"Chip Pearson" wrote in message
...
Jimmy,

GetObject is used to get a reference to an existing running
instance of Excel. It will fail and return Nothing if there is
no running instance of Excel. If there is more than one
instance running, you have no control over which instance to
which you will get a reference. Perhaps code like the
following will work:

Dim XL As Excel.Application
Dim WB As Excel.Workbook
On Error Resume Next
Set XL = GetObject(,"Excel.Application")
If XL Is Nothing Then
Set XL = CreateObject("Excel.Application")
End If
If Not XL.ActiveWorkbook Is Nothing Then
Set WB = XL.ActiveWorkbook
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com





"Jimmy" wrote in message
ink.net...
Then perhaps I shouldn't be running GetObject at all? I just
need a
reference (from Word VBA) to the active Excel document,
without knowing
its name. Is this possible?


"Chip Pearson" wrote in message
...
If you don't know the filename, you cannot control with
instance of Excel GetObject is going to return.



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"joe" wrote in message
...
If I run the following code and I have an open spreadsheet
with existing
sheets, I get another instance of Excel w/no sheets. How
can I reference
the active spreadsheet(w/o knowing the filename?)

Dim oXL as object
Dim xlWorksheet as object

Set oXL = GetObject("", "Excel.Application")
Set xlWorkSheet = oXL.Worksheets.Add

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!










  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Reference the active sheet

Flogging a dead horse here, if I have 7 excel workbooks open but only 1 is
active, there is no way from Word VBA that I can get a reference to that
active workbook/sheet if I don't have the name?
"Chip Pearson" wrote in message
...
Jimmy,

GetObject is used to get a reference to an existing running instance of
Excel. It will fail and return Nothing if there is no running instance of
Excel. If there is more than one instance running, you have no control
over which instance to which you will get a reference. Perhaps code like
the following will work:

Dim XL As Excel.Application
Dim WB As Excel.Workbook
On Error Resume Next
Set XL = GetObject(,"Excel.Application")
If XL Is Nothing Then
Set XL = CreateObject("Excel.Application")
End If
If Not XL.ActiveWorkbook Is Nothing Then
Set WB = XL.ActiveWorkbook
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com





"Jimmy" wrote in message
ink.net...
Then perhaps I shouldn't be running GetObject at all? I just need a
reference (from Word VBA) to the active Excel document, without knowing
its name. Is this possible?


"Chip Pearson" wrote in message
...
If you don't know the filename, you cannot control with instance of
Excel GetObject is going to return.



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"joe" wrote in message
...
If I run the following code and I have an open spreadsheet with
existing
sheets, I get another instance of Excel w/no sheets. How can I
reference
the active spreadsheet(w/o knowing the filename?)

Dim oXL as object
Dim xlWorksheet as object

Set oXL = GetObject("", "Excel.Application")
Set xlWorkSheet = oXL.Worksheets.Add

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!








  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Reference the active sheet

As shown in the code I posted, you can get the ActiveWorkbook
with code like

Set WB = XL.ActiveWorkbook


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Jimmy" wrote in message
ink.net...
Flogging a dead horse here, if I have 7 excel workbooks open
but only 1 is active, there is no way from Word VBA that I can
get a reference to that active workbook/sheet if I don't have
the name?
"Chip Pearson" wrote in message
...
Jimmy,

GetObject is used to get a reference to an existing running
instance of Excel. It will fail and return Nothing if there is
no running instance of Excel. If there is more than one
instance running, you have no control over which instance to
which you will get a reference. Perhaps code like the
following will work:

Dim XL As Excel.Application
Dim WB As Excel.Workbook
On Error Resume Next
Set XL = GetObject(,"Excel.Application")
If XL Is Nothing Then
Set XL = CreateObject("Excel.Application")
End If
If Not XL.ActiveWorkbook Is Nothing Then
Set WB = XL.ActiveWorkbook
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com





"Jimmy" wrote in message
ink.net...
Then perhaps I shouldn't be running GetObject at all? I just
need a
reference (from Word VBA) to the active Excel document,
without knowing
its name. Is this possible?


"Chip Pearson" wrote in message
...
If you don't know the filename, you cannot control with
instance of Excel GetObject is going to return.



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"joe" wrote in message
...
If I run the following code and I have an open spreadsheet
with existing
sheets, I get another instance of Excel w/no sheets. How
can I reference
the active spreadsheet(w/o knowing the filename?)

Dim oXL as object
Dim xlWorksheet as object

Set oXL = GetObject("", "Excel.Application")
Set xlWorkSheet = oXL.Worksheets.Add

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!










  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,824
Default Reference the active sheet

Just to jump in to muddy the waters!

Add a line to this portion of Chip's code:

If Not XL.ActiveWorkbook Is Nothing Then
Set WB = XL.ActiveWorkbook
End If

make it look like:

If Not XL.ActiveWorkbook Is Nothing Then
Set WB = XL.ActiveWorkbook
msbox wb.name
End If

And you'll see WB is what you want.


Jimmy wrote:

Flogging a dead horse here, if I have 7 excel workbooks open but only 1 is
active, there is no way from Word VBA that I can get a reference to that
active workbook/sheet if I don't have the name?
"Chip Pearson" wrote in message
...
Jimmy,

GetObject is used to get a reference to an existing running instance of
Excel. It will fail and return Nothing if there is no running instance of
Excel. If there is more than one instance running, you have no control
over which instance to which you will get a reference. Perhaps code like
the following will work:

Dim XL As Excel.Application
Dim WB As Excel.Workbook
On Error Resume Next
Set XL = GetObject(,"Excel.Application")
If XL Is Nothing Then
Set XL = CreateObject("Excel.Application")
End If
If Not XL.ActiveWorkbook Is Nothing Then
Set WB = XL.ActiveWorkbook
End If


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com





"Jimmy" wrote in message
ink.net...
Then perhaps I shouldn't be running GetObject at all? I just need a
reference (from Word VBA) to the active Excel document, without knowing
its name. Is this possible?


"Chip Pearson" wrote in message
...
If you don't know the filename, you cannot control with instance of
Excel GetObject is going to return.



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com



"joe" wrote in message
...
If I run the following code and I have an open spreadsheet with
existing
sheets, I get another instance of Excel w/no sheets. How can I
reference
the active spreadsheet(w/o knowing the filename?)

Dim oXL as object
Dim xlWorksheet as object

Set oXL = GetObject("", "Excel.Application")
Set xlWorkSheet = oXL.Worksheets.Add

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!







--

Dave Peterson



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
using the active cell as a reference in a function JFA Excel Worksheet Functions 5 November 12th 08 01:43 AM
How to reference an active cell Tomá¹ Vognar New Users to Excel 5 March 14th 08 08:56 PM
Active row cell reference pskwaak Excel Worksheet Functions 2 March 18th 07 01:38 AM
Active Cell Reference pskwaak Excel Worksheet Functions 8 March 13th 07 02:22 AM
Active Cell Copy And Paste Sheet to Sheet A.R.J Allan Jefferys New Users to Excel 4 May 4th 06 02:04 AM


All times are GMT +1. The time now is 03:39 AM.

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

About Us

"It's about Microsoft Excel"