Solved

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

Posted on 2006-07-04
7
267 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 250 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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