472,374 Members | 1,576 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

New to COM (Excel). Need a little help.

Hi all,

I am trying to write an application where I need the ability to open
an Excel spreadsheet and do basic read/write, insert rows, and
hide/unhide rows. Using win32com I have been able to get the basics
down as well as some examples displaying how to simply read and write.

But the next step appears exponential. I haven never done anything in
VB, so any and all concepts and commands are completely foreign. I
have been digging through the VB help and also bought a book
specifically for Python and COM. But I don't really have time to learn
VB before I can finish my script.

Would there happen to be any pre-existing examples of the stuff I need
to do out there? Basically I need to do the things I listed above -
insert rows and columns and hide/unhide rows. I think with a few
examples of sheet manipulation I could figure out the rest.

Thanks ahead of time,
Marc
Jul 18 '05 #1
4 7005
At 10:18 AM 10/3/2003, Marc wrote:
Hi all,

I am trying to write an application where I need the ability to open
an Excel spreadsheet and do basic read/write, insert rows, and
hide/unhide rows. Using win32com I have been able to get the basics
down as well as some examples displaying how to simply read and write.

But the next step appears exponential. I haven never done anything in
VB, so any and all concepts and commands are completely foreign. I
have been digging through the VB help and also bought a book
specifically for Python and COM. But I don't really have time to learn
VB before I can finish my script.

Would there happen to be any pre-existing examples of the stuff I need
to do out there? Basically I need to do the things I listed above -
insert rows and columns and hide/unhide rows. I think with a few
examples of sheet manipulation I could figure out the rest.


The Range object handles all of the above. I recommend examining the
various properties and methods of Range. One easy way to do this is
1 open the Excel VBA Window
2 press F2 to get the Object Browser
3 scroll the classes pane to Range then look at members insert, delete and
value.
4 Press F1 on any of these to see the help file and helpful examples.

Also look at Worksheet Classes Rows and Columns properties for Range
objects that span entire rows / columns

Assuming you have created a worksheet object (let's call it ws)

Reading & Writing (a review):
rng = ws.Range("a1:b2")
rng.Value ((None, None), (None, None)) rng.Value=((1, 2), (3, 4))
rng.Value ((1.0, 2.0), (3.0, 4.0))

Hiding: row = ws.Rows("1")
row.Hidden 0 row.Hidden=1
Insert/Delete row.Insert()
col = ws.Columns("A")
col.Delete()


Enough?

Bob Gailer
bg*****@alum.rpi.edu
303 442 2625
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003

Jul 18 '05 #2
mn******@airmail.net (Marc) wrote in message news:<43**************************@posting.google. com>...
Hi all,

I am trying to write an application where I need the ability to open
an Excel spreadsheet and do basic read/write, insert rows, and
hide/unhide rows. Using win32com I have been able to get the basics
down as well as some examples displaying how to simply read and write.

But the next step appears exponential. I haven never done anything in
VB, so any and all concepts and commands are completely foreign. I
have been digging through the VB help and also bought a book
specifically for Python and COM. But I don't really have time to learn
VB before I can finish my script.

Would there happen to be any pre-existing examples of the stuff I need
to do out there? Basically I need to do the things I listed above -
insert rows and columns and hide/unhide rows. I think with a few
examples of sheet manipulation I could figure out the rest.

Thanks ahead of time,
Marc


One trick is using Excel's Macro-Recorder to record all
you've done in Excel. After this you get the resulting macro
by Alt-F11 in the VBA-Editor.
You can copy the recorded methode-calls to Python by changing some
few things to Python-syntax.
Let's say you have reached the following point:
import win32com.client
excel = win32com.client.Dispatch("Excel.Application")
excel.Visible=1
workbook=excel.Workbooks.Add()
Now start Excel's Macro-Recorder and do want you want to do.
E.g. select row 4-9 and hide them.
Stop recording and see the VBA-result:
Rows("4:9").Select
Selection.EntireRow.Hidden = True
This will result in the following Python code: excel.Rows("4:9").Select()
excel.Selection.EntireRow.Hidden = True Et voila, line 4 to 9 are hidden
(provided that you got them unhided before in Excel).
Some days or weeks later when you have learned a little bit
about ??Microsoft's VBA-OOP-concept?? :-) you will know that you
can code this shorter: workbook.ActiveSheet.Rows("4:9").Hidden = True or quicker: excel.Rows("4:9").Hidden = True


I hope you will likes this **LEARNING by RECORDING** :-)

Regards
Peter
Jul 18 '05 #3
One other quick question. How do I close down the Excel Application?
After performing the commands:

x1App = win32com.client.dynamic.Dispatch('Excel.Applicatio n')
xlApp.Workbooks.Open(filename)

That fires up Excel which stays hidden unless I make it visible. I can
close the book and delete the COM object by doing the following:

xlBook.Close(SaveChanges=0)
del self.xlApp

But the Excel process is still running in the background. How do I
stop the Excel process? Leaving it running seems to screw up the
application the next time I run it.

Thanks again,
Marc
Jul 18 '05 #4
At 06:06 PM 10/3/2003, Marc wrote:
One other quick question. How do I close down the Excel Application?
After performing the commands:

x1App = win32com.client.dynamic.Dispatch('Excel.Applicatio n')
xlApp.Workbooks.Open(filename)

That fires up Excel which stays hidden unless I make it visible. I can
close the book and delete the COM object by doing the following:

xlBook.Close(SaveChanges=0)
del self.xlApp

But the Excel process is still running in the background. How do I
stop the Excel process? Leaving it running seems to screw up the
application the next time I run it.


Right. It does. Thank you Microsoft.
Try:
x!App.Quit()

Bob Gailer
bg*****@alum.rpi.edu
303 442 2625
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003

Jul 18 '05 #5

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

Similar topics

1
by: frankvfox | last post by:
I need a code sample using VB ASP.Net 2003 which exports a tab delimited text file resident on the server into an Excel spreadsheet which will be saved through the browser to the client's local...
9
by: Edward S | last post by:
I budget for a Project in an Excel sheet as illustrated below. The months below are usually a 2 year period i.e. 24 months, though it could be over 24 months depending upon a Project. I then...
1
by: barma16 | last post by:
I've hit a bit of a brick wall here, and could use some advice. I have an Access application whose output is a four-tab Excel spreadsheet where three of the four tabs are the result of database...
6
by: Matthew Wieder | last post by:
I have the following requirements: Build a stand-alone C# application that asks the user to click in a cell in an Excel spreadsheet, and then displays the address of that cell in the C#...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
16
by: alexia.bee | last post by:
Hi all, In some weird reason, excel instance won;t die if i remove the comment from 4 lines of setting values into struct. here is a snipcode public...
7
by: Holger Fitschen | last post by:
Hi to all, I want to use the Excel solver in a VB.Net project. The macro Sub Makro1Solver() Application.Run "Solver.xla!Auto_Open" SolverReset Worksheets(1).Select...
7
by: farhaaad | last post by:
Hi everybody, I just wanted to know if i can make a form in excel (the same as access forms), so when i enter data in excel form it goes to a table in access ? I mean when i enter a value in a...
0
by: Tony Hine | last post by:
Problem for Excel Developers One of the problems facing Excel developers moving into MS Access is actually the apparent similarity between MS Access tables and Excel spreadsheets. MS Access is...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.