Solved

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

Posted on 2006-07-04
7
263 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to convert wav to mp3 in delphi 9 169
FMX enumerated colours 2 84
code issue 8 99
Making delphi communicate with a c# service 16 86
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
A short film showing how OnPage and Connectwise integration works.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

930 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now