Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hey, I have a particular problem: I have these cells that use a custom formula to get a value and one cell that uses

a predefined formula. All these cells output correct value in excel. But when I grab the value of the cells, only the one with predefined formula returns the correct text value; the rest return sth like:

Win32::OLE::Variant=SCALAR(0xe0dabc)

what am I doing wrong? this is my code:

use warnings; use strict; # we are going be working with certain OLE use Win32::OLE qw(CP_UTF8); use Win32::OLE::Const 'Microsoft Excel'; use utf8; # use Win32::OLE::Enum; use CGI; # we'll be having some HTML writing use CGI qw(:standard); use CGI::Pretty qw( :html3 ); use diagnostics; my $text = ""; my $sheet; my $doc; my $book; my $language = ""; my $directory = "../maps update"; my $output_dir = "$directory/new_footers"; my $root = "C:\\{hidden for security reasons}";# input/output operatio +ns work my @values = (); # CGI Intializing for HTML Output my $q = CGI-> new; $q -> header(-charset=>'utf-8'); # create header opendir (DH, $directory) || die "can't opendir $directory: $!"; # test whether the item returned by grep is a file and its name does n +ot start with "_" # because there is one redirect input which start with "_" my @dir_list = grep { (/^EN-US_.+/) && -d "$directory/$_" } readdir(DH +); # we are working with Excel application my $excel = Win32::OLE->new('Excel.Application', 'Quit'); $excel -> {DisplayAlerts} = "False"; # This turns off the "This file a +lready exists" message. $excel -> {Visible} = 0; foreach (@dir_list){ # extract the language abbreviation m/^EN-US_(.+)$/i; $language = lc($1); print "$language\n"; $doc = "$root\\EN-US_$1\\cvg_$language.xlsx"; $book = $excel ->Workbooks->Open("$doc") || die("Unable to open $d +oc ", Win32::OLE->LastError()); # Make Excel handle UTF8. $Win32::OLE::CP = CP_UTF8; # get sheet #3 $sheet = $book->Worksheets("Sheet3"); $sheet -> activate(); #$text = $sheet -> Range("A4")->{Value}; #print "**$text**\n"; # Prepare OUT_FOOTER file open(OUT_FOOTER, "> :utf8", "$output_dir/footer_$language.html") | +| die("can't open $output_dir/footer_$language.html for writing: $!") +; binmode OUT_FOOTER, ":utf8"; # Mappings I have form excel file: # A1 and A4 is are sentences # A4 : Currently Available in Production: # (A5,B5) (A9,B9) ... (A97, B97) point to (continent, countries) # A111 and A113 are additinal sentecnes $values[0] = $sheet -> Range("A1")->{Value}; $values[1] = $sheet -> Range("A4")->{Value}; $values[2] = $sheet -> Range("A5")->{Value}; $values[3] = $sheet -> Range("B5")-> value(); $values[4] = $sheet -> Range("A9")->{Value}; $values[5] = $sheet -> Range("B9")->{Value}; $values[6] = $sheet -> Range("A13")->{Value}; $values[7] = $sheet -> Range("B13")->{Value}; $values[8] = $sheet -> Range("A59") ->{Value}; $values[9] = $sheet -> Range("B59")->{Value}; $values[10] = $sheet -> Range("A62")->{Value}; $values[11] = $sheet -> Range("B62")->{Value}; $values[12] = $sheet -> Range("A69")->{Value}; $values[13] = $sheet -> Range("B69")->{Value}; $values[14] = $sheet -> Range("A97")->{Value}; $values[15] = $sheet -> Range("B97")->{Value}; $values[16] = $sheet -> Range("A111")->{Value}; $values[17] = $sheet -> Range("A113")->{Value}; print OUT_FOOTER $q->start_html("Maps - Coverage for $language"), +# start the HTML $q->p($values[0]), $q->p($values[1]), $q->p(b($values[2]) . ": " . $values[3]), $q->p(b($values[4]) . ": " . $values[5]), $q->p(b($values[6]) . ": " . $values[7]), $q->p(b($values[8]) . ": " . $values[9]), $q->p(b($values[10]) . ": " . $values[11]), $q->p(b($values[12]). ": " . $values[13]), $q->p(b($values[14]) . ": " . $values[15]), $q->p(b($values[16])), $q->p(b($values[17])), $q->end_html; binmode STDOUT, ":utf8"; # necessary to battle the wide charachter + input warning $text = "$values[2] : $values[3]\n"; print $text; # close document and Word instance print "Closing document and Excel\n"; $book -> Save; undef $book; close OUT_FOOTER; } undef $excel; closedir DH;

Replies are listed 'Best First'.
Re: Excel OLE cell value custome pre-defined predefined function formula error
by davies (Monsignor) on Feb 05, 2011 at 14:58 UTC

    Your code doesn't run because - at least - only you have your directories and files. It's therefore impossible to reproduce your problem. The best I can do is to write some code that does work but reproduces my best guess at your situation. If you can give us working code that demonstrates your problem, I might be able to do better.

    use strict; use warnings; use Win32::OLE; my $xl = Win32::OLE->new('Excel.Application'); $xl->{Visible} = 1; my $wb = $xl->Workbooks->Add; if ($wb->Sheets->{Count} > 1) { for (2..$wb->Sheets->{Count}) { $wb->Sheets(2)->Delete; } } $xl->VBE->ActiveVBProject->VBComponents->Add(1); my $cm = $xl->VBE->ActiveVBProject->VBComponents(3)->CodeModule; my $line = int($cm->CountOfLines); $cm->InsertLines(++$line,"Function Example(n as double) as double"); $cm->InsertLines(++$line,"Example = n^2"); $cm->InsertLines(++$line,"End Function"); my $sht = $wb->Sheets(1); $sht->Range("A1")->{Formula} = "=Example(2)"; $sht->Range("A2")->{Formula} = "=2*2"; my $a1 = $sht->Range("A1")->{Value}; my $a2 = $sht->Range("A2")->{Value}; if ($a1 != 4 or $a2 != 4) {print "We've had a problem"} else {print "L +ife is good"};

    Regards,

    John Davies

      This works, I verified it.

      Thanks everyone for helping. Not sure how to attach the excel file. If there is a way please let me know

      here is the code in the VB components that I have. I can see that you produced a component on the fly and tested it, which is something I can test as well. But if you want add my custom made function to your excel file as well and test as well

      Function Concat2(myRange As Range, Optional myDelimiter As String) Dim r As Range Application.Volatile For Each r In myRange If Len(r.Text) > 0 Then Concat2 = Concat2 & r & myDelimiter End If Next r If Len(myDelimiter) > 0 Then Concat2 = Left(Concat2, Len(Concat2) - Len(myDelimiter)) End If End Function

      One thing that I just noticed which might be causing problem is Application.volatile. Let me search a bit on that to see if it is causing problems.Thanks very much for your assistance again

        This is a Perl forum, not VBA, so I'd be grateful for any guidance from those who think I shouldn't answer this here - or at least, in this much detail.

        I'm pretty sure from this that your problem is VBA, not Perl. Application.Volatile is unlikely to affect it - it merely determines when to recalculate the function. However, if you have a delimiter and a range of blank cells, you end up trying to reduce the length of a zero length string. Excel gets very bolshie at such treatment & throws an error, which is what I think you are seeing, although it may be something to do with dates, which Excel can handle in ways that confuses itself. I would rewrite your VBA thus:

        Function Concat2(myRange As Range, Optional myDelimiter As String) As +String Dim r As Range Application.Volatile For Each r In myRange If Len(r.Text) > 0 Then Concat2 = Concat2 & CStr(r.Text) & myDelimiter End If Next r If Len(myDelimiter) > 0 And Len(Concat2) > Len(myDelimiter) Then Concat2 = Left(Concat2, Len(Concat2) - Len(myDelimiter)) End If End Function

        The main differences are:
        I have defined the function as string, not let it default to variant.
        I have explicitly used r.Text, as otherwise you will not get r.Text but r.Value, which need not be the same.
        I have explicitly converted r.Text to string. This is essential. Try a simple spreadsheet with a date as the only value in the range passed to this function to see what I mean. However, I don't know how you want dates handled, so something else may be necessary.
        I have checked that I'm not doing anything daft when removing the trailing delimiter.

        Changes I haven't made:
        Calling variables "myWhatever" - I can't see what information the "my" conveys & would remove it.
        I don't think Application.Volatile serves a useful purpose - anything that would change this function's value would cause its recalculation, too - so I would remove it.

        There is, heartfelt thanks to the gods, no way to upload any file that isn't text. If you still can't solve your problem, please try to write the smallest Perl programme that runs from scratch and demonstrates the problem. By all means use my code as a starting point for creating Excel files on the fly.

        Regards,

        John

        Update: corrected minor typo.
Re: Excel OLE cell value custome pre-defined predefined function formula error
by cjb (Friar) on Feb 05, 2011 at 13:45 UTC

    A quick google suggests you might want to check out valof(OBJECT) in Win32::OLE.

Re: Excel OLE cell value custome pre-defined predefined function formula error
by dasgar (Priest) on Feb 05, 2011 at 23:05 UTC

    As davies pointed out, without your Excel file, others won't be able to test your script. However, I took a quick look into the OLE browser. You might want to try Value2 to see if that is returning what you want.

    Based on your follow on post, it looks like cell A5 is the cell that has the special function. To try my suggestion, just change:

    $values[2]  = $sheet -> Range("A5")->{Value};

    to be:

    $values[2]  = $sheet -> Range("A5")->{Value2};

    Not claiming that this will "fix" the problem. Just tossing out an idea to try out.

      There is usually no difference between Value and Value2, and if you are getting an error value, I have never encountered a difference. The difference comes with dates, where Value will return 01/01/2001 while Value2 will return 36892. I believe there are some other esoteric situations where there is a difference, but I have never needed to know them.

      Regards,

      John Davies

Re: Excel OLE cell value custome pre-defined predefined function formula error
by Anonymous Monk on Feb 05, 2011 at 00:08 UTC

    Additional Details:

    A5: has value: Canada, USA, Mexico, formula :=sheet1!concat2(A1:A4)

    B5:has value: Egypt, Libya, Syria, Iran and formula : =sheet1!concatenate(A12,A13,A14,A15)

    So A5 is using a custom made VBA formula called concat2 and B5 is using the normal concatenate formula. I am using Perl to output something like:

    $values[2] = $sheet -> Range("A5")->{Value}; $values[3] = $sheet -> Range("B5")->{Value};

    But only printing $values[2] is correct. printing  $values[1] gives a : Win32::OLE::Variant=SCALAR(0x102e444)

    If change the formula of A5 to use a normal formula, it will display the correct value.

    But I want to use a custom made formula so what is wrong?

      sorry please replace the 3rd last sentence above with this:

      But only printing $values[3] is correct. printing $values[2] gives a : Win32::OLE::Variant=SCALAR(0x102e444)

        It might be better to edit the original post rather than keep adding supplementary replies to yourself.