?
Solved

I need help with the Excel Range collection and cell formatting using Automation.

Posted on 2006-07-04
7
Medium Priority
?
287 Views
Last Modified: 2012-05-05
Hi guys,

Question 1
I am trying to import an excel price list of books into my Delphi Application, the excel file is using custom formatting on each cell containing an ISBN number the formatting used is: (0000000000). In excel it displays the correct results ie: 0072226803.

When I access the cell using
Excel.Cells.Item[1,1]

it returns: 72226803, this throws the validation out because an ISBN number must be 10 characters in length and the first two zeros have gone AWOL.
How would I access the cells value and preserve the leading zeros? Keep in mind that the Spreadsheets differ from supplier to supplier so their ISBN columns may have different formatting, ultimately I want to access the value as its seen on the spreadsheet...

Question 2
Most of the documentation shows the Cells collection being accessed as follows:
Cells(rwIndex, colIndex).Value

My problem is that with the Excel_TLB, the only way to access an individual cell by row, col is with the Item property and this returns an OleVariant which pretty much only contains the cells content. How would I access other properties for an individual cell?



Any help on this will be greatly appreciated!
0
Comment
Question by:scandiumza
  • 3
  • 3
7 Comments
 
LVL 3

Expert Comment

by:Ubethatway
ID: 17035916
Hi,

Would a suitable solution be to simply pad the numbers with 0's up to 10 digits? e.g.:

function pad(X: String): String;
  padded := X;
 
  while (length(padded) <> 10) do
    padded := '0'+padded;

  result := padded;
end;

Remembering to convert the value to a string before passing it to the function.

Hope that helps, Mark.
0
 

Author Comment

by:scandiumza
ID: 17035951
Hey Mark,

I was thinking of padding it but the problem comes in with the validation. If, through human error, the ISBN numbers in the pricelist are the incorrect length and they get padded, they will be passed as valid when tested. These invalid entries should not appear in the application.

Thanks for the suggestion though!
0
 
LVL 10

Accepted Solution

by:
atul_parmar earned 1000 total points
ID: 17035962
Hi, try this out.

var
  s : string;
begin
  if x = Unassigned then
    x := CreateOleObject('Excel.Application');
  x.visible := True;
  x.WorkBooks.Add;
  x.WorkBooks[1].WorkSheets[1].Cells[1,1] := 12345;
  x.WorkBooks[1].WorkSheets[1].Cells[1,1].NumberFormat := '0000000000';
  s := x.WorkBooks[1].WorkSheets[1].Cells[1,1].Text;
  ShowMessage(s);
end;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Expert Comment

by:atul_parmar
ID: 17035999
var
  x : olevariant;  >>> Missed out
After you finish working with the object free it by
x := unassigned;

In your case, all you have to do is to use the olevariant or cast the object to IDispatch.
0
 

Author Comment

by:scandiumza
ID: 17036022
Now that works precisely how I need it to work!

Any chance of this working with the Excel Type Library?

Thanks atul!
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 17036055
Yep, As I said previously you will need to cast it to IDispatch

e.g.
var
x : olevariant;
myexcel : texcelapplication;
begin
  x := IDispatch(myexcel.defaultinterface);
  x.visible := True;
end;
0
 

Author Comment

by:scandiumza
ID: 17036067
Oh okay, I see now. Sorry, not too clued up with COM programming.

Thanks again!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question