Good day Monks. I am trying to perform a merge & center operation on a set of columns in an Excel spreadsheet via Win32::OLE. I have learned that the way to find out how to do new things in OLE is to record a macro to translate the VB commands into Perl/Win32::OLE statements. Here is the macro code for exactly what I wanted to do:
Sub Macro1() ' ' Macro1 Macro ' ' Range("B1:Q1").Select With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlBottom .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With Selection.Merge End Sub
Here is my code to implement:
use Win32::OLE qw(in with CP_UTF8); Win32::OLE->Option(CP => CP_UTF8); use Win32::OLE::Const 'Microsoft Excel'; use Win32::OLE::Variant; $Win32::OLE::Warn = 3; my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32: +:OLE->new('Excel.Application', 'Quit'); # get already active Excel $Excel->{'Visible'} = 1; my $Book = $Excel->Workbooks->Open('foo.xlsx'); my $Sheet = $Book->Worksheets(1); $Sheet->Range("B1:Q1")->Merge(); $Sheet->Range("B1:Q1")->{HorizontalAlignment} = 'xlCenter';
The merge works correctly! Alas, the center does not:
Win32::OLE operating in debugging mode: _Unique => 1 OLE exception from "Microsoft Excel": Unable to set the HorizontalAlignment property of the Range class Win32::OLE(0.1709) error 0x80020009: "Exception occurred" in PROPERTYPUT "HorizontalAlignment" at C:\Users\boss\Dropbox C:\t +mp\merge-center.pl line 11.
Does anyone know how to fix it?

In reply to Win32::OLE Excell Merge & Center by cormanaz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.