One of the tools I write at $work is a larger application with a front-end in Excel. As that front end does some SQL queries against some views, I want to maintain a list of all views that are actually used by the front-end. The following snippet extracts all portions of the code that somewhat look like SQL (owing to the fact that I write the SQL in a very predictable way).

use Win32::OLE; # ... my $excel_source = 'C:\Path\To\Some_workbook.xls'; my $excel = Win32::OLE->CreateObject('Excel.Application'); my $wb = $excel->Workbooks->Add($excel_source); for my $source (in ($wb->{VBProject}->{VBComponents}) ){ print $source->Name, "\n"; my $vb = $source->{CodeModule}; my $linecount = $vb->{CountOfLines}; if( $linecount) { my $body = join "", $vb->Lines(1,$linecount); while ($body =~ /^([^\n]+=\s*"(?:SELECT|INSERT|UPDATE|DELE +TE) (?:[^\n]*_\r?\n)*[^\n]*?)"\r?\n/imsg) { my $matched= $1; $matched =~ s/^\s*'.*$//mg; # remove all VB comments next unless ($matched =~ /\S/); $matched =~ s/.*?\s*=\s*"//im; $matched =~ s/_\r?\n\s*//g; # rewrap continued lines $matched =~ s/"\s*&\s*"//g; # reconcatenate all string +s #print $matched; #print "\n---\n"; if ($matched =~ /(?=from\s+(\w+(\s*,\s*\w+)*))/i) { my $tables = $1; $excel_used{uc $_} = $matched for ($tables =~ /(\w ++)/g); } if ($matched =~ /(?=join\s+(\w+)*)/i) { $excel_used{uc $1} = $matched; } push @used_sql, $matched; } }; }

In reply to Extract the code from a Microsoft Excel file by Corion

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.