473,651 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Excel Automation Problem

I'm writing an Excel Automation app and I'm running to a problem early
on with what should be a basic step.

Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Dim oRng As Excel.Range
oExcel = CreateObject("E xcel.Applicatio n")
oBook = oExcel.Workbook s.Add
oSheet = oBook.Worksheet s(1)
....Populate Sheet...
oRng(Selection, ActiveCell.Spec ialCells(xlLast Cell)).Select()

My application will not compile due to the last line because
Selection, ActiveCell and xlLastCell are not declared. I have added
Excel 10.0 (2002) as a reference. What am I doing wrong?
Nov 21 '05 #1
4 3118
Hi,

I added a reference to the excel interop assembly. Here is an
example on creating as worksheet. Try to avoid using latebinding.

Dim oExcel As Microsoft.Offic e.Interop.Excel .Application

Dim oBook, oBook1 As Microsoft.Offic e.Interop.Excel .Workbook

Dim oSheet As Microsoft.Offic e.Interop.Excel .Worksheet

'Start a new workbook in Excel.

oExcel = New Microsoft.Offic e.Interop.Excel .Application

oBook = oExcel.Workbook s.Add

oBook1 = oExcel.Workbook s.Add

'Add data to cells of the first worksheet in the new workbook.

oSheet = CType(oBook.Wor ksheets(1),
Microsoft.Offic e.Interop.Excel .Worksheet)

oSheet.Range("A 1").Value = "Last Name"

oSheet.Range("B 1").Value = "First Name"

oSheet.Range("C 1").Value = "Price"

oSheet.Range("A 1:B1").Font.Bol d = True

oSheet.Range("A 2").Value = "Doe"

oSheet.Range("B 2").Value = "John"

oSheet.Range("C 2").Value = 12345.456

oSheet.Range("C 2").Cells.Numbe rFormat = "$0.00"

oSheet = CType(oBook.Wor ksheets(2),
Microsoft.Offic e.Interop.Excel .Worksheet)

oSheet.Range("A 1").Value = "Last Name"

oSheet.Range("B 1").Value = "First Name"

oSheet.Range("C 1").Value = "Price"

oSheet.Range("A 1:B1").Font.Bol d = True

oSheet.Range("A 2").Value = "Doe"

oSheet.Range("B 2").Value = "John"

oSheet.Range("C 2").Value = 12345.456

oSheet.Range("C 2").Cells.Numbe rFormat = "$0.00"

'Save the Workbook and quit Excel.

oExcel.DisplayA lerts = False

oBook.SaveAs("c :\Book1.xls")

oSheet = Nothing

oBook = Nothing

oExcel.Quit()

oExcel = Nothing

GC.Collect()

Ken
---------------------
"goonsquad" <jb******@sakon .com> wrote in message
news:e5******** *************** ***@posting.goo gle.com...
I'm writing an Excel Automation app and I'm running to a problem early
on with what should be a basic step.

Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Dim oRng As Excel.Range
oExcel = CreateObject("E xcel.Applicatio n")
oBook = oExcel.Workbook s.Add
oSheet = oBook.Worksheet s(1)
....Populate Sheet...
oRng(Selection, ActiveCell.Spec ialCells(xlLast Cell)).Select()

My application will not compile due to the last line because
Selection, ActiveCell and xlLastCell are not declared. I have added
Excel 10.0 (2002) as a reference. What am I doing wrong?
Nov 21 '05 #2
Hi
Try
oSheet.oRng(Sel ection, ActiveCell.Spec ialCells
(xlLastCell)).S elect()

Kind Regards
Jorge
-----Original Message-----
I'm writing an Excel Automation app and I'm running to a problem earlyon with what should be a basic step.

Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Dim oRng As Excel.Range
oExcel = CreateObject("E xcel.Applicatio n")
oBook = oExcel.Workbook s.Add
oSheet = oBook.Worksheet s(1)
....Populate Sheet...
oRng(Selection , ActiveCell.Spec ialCells (xlLastCell)).S elect()
My application will not compile due to the last line becauseSelection, ActiveCell and xlLastCell are not declared. I have addedExcel 10.0 (2002) as a reference. What am I doing wrong?
.

Nov 21 '05 #3
* jb******@sakon. com (goonsquad) scripsit:
My application will not compile due to the last line because
Selection, ActiveCell and xlLastCell are not declared. I have added


Make sure the enum containing 'xlLastCell' is imported.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #4
Thanks guys!

Using the Excel Interop Assembly I was able to finally get it right.

oRng = oSheet.Range(oE xcel.Selection,
oExcel.Selectio n.End(Microsoft .Office.Interop .Excel.XlDirect ion.xlToRight))
"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:ut******** ******@TK2MSFTN GP14.phx.gbl...
* jb******@sakon. com (goonsquad) scripsit:
My application will not compile due to the last line because
Selection, ActiveCell and xlLastCell are not declared. I have added


Make sure the enum containing 'xlLastCell' is imported.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
27264
by: Ange T | last post by:
Hi there, I'm having pain with the VB behind an Access form. The form is used to create reports in Excel based on the details entered in the form. This has always worked without error on my machine (NT4, Access 2k), however as soon as I attempt to create anything on another machine (NT4, Access 2k) which most users will be working from, I receive an automation error. The problem line with the code is:
6
14550
by: Frank X | last post by:
Excel 2002 introduced a capability to add custom worksheet functions to Excel direct from a COM/ActiveX object. I can use C# to develop a COM object which I can use fine from Excel/VBA, however I can't see it using the Excel Tools/Add-Ins - Automation button. Now this appears to be because it is not an *ActiveX* COM object, apparently in order to be an ActiveX object the COM object needs to have a 'CLSID'/Programmable key entry in the...
17
6331
by: Mansi | last post by:
I need to do some research on how to use excel automation from c#. Does anyone know of any good books related to this subject? Thanks. Mansi
6
9342
by: Steve Richter | last post by:
I am getting error in a vbscript: ActiveX component cant create object: Excel.Application. The vbscript code is: Dim objExcel Set objExcel = CreateObject("Excel.Application") I am pretty sure it is a permission issue because the script works when I point the browser directly at the .htm file on the c: drive: c:\inetpub\wwwroot\DemoSite\VbScriptTest.htm
8
2175
by: jack | last post by:
Access is denied. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.UnauthorizedAccessException: Access is denied. ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET...
12
2415
by: D. Shane Fowlkes | last post by:
This most likely belongs in another forum but I thought I'd start here. I have a COM Object written in VB6. The DLL will access MS Excel and use it's Object Library to write a customized report and saves it to a folder. The DLL even writes to a log for each step it takes so we can troubleshoot the problems (if any). This works fine on one machine but not another. The folder the DLL is trying to write to has full permissions assigned...
1
7627
by: Robin Tucker | last post by:
Heres and interesting problem: I have a VB.NET program that creates reports via. Word Automation. This all works fine. What I want to do as part of this report generation process is to embed a graph/chart within the report, once again from VB.NET. I have chosen to use Excel.Chart.8. So, to embed I do this from VB.NET: Dim theNewShape As InlineShape = m_Document.InlineShapes.AddOLEObject (ClassType:="Excel.Chart", _
6
598
by: a.theil | last post by:
Please help! I need a simple excel automation, just 2 write some files into excel. I do: Dim oXL As Excel.Application Dim oWB As Excel.Workbook Dim oSheet As Excel.Worksheet Dim oRng As Excel.Range
6
2065
by: Gunawan | last post by:
Dear All, I have create an excel (COM Object) using this code Excel.Application xls = new Excel.Application(); but I can not remove it from memory although I have using close and quit wb.Close(false, false, 0); xls.Quit();
15
5342
by: =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?= | last post by:
Hi All, We are in the process of Upgrade Excel 2003 (Office 2003) to Excel 2007 (Office 2007) for one of web application. This web application is using Excel (Pivot Table) reports. With Excel 2003 application (Reports) works ok. But when Office 2003 is upgraded to Office 2007 Excel reports stops working. It Log the error “Exception from HRESULT: 0x800A03EC.” In database.
0
8347
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8792
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7294
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6157
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2696
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1905
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.