简体   繁体   中英

How do I translate this code from Visual Basic to Perl?

I am trying to export an Excel spreadsheet to SharePoint. I recorded the Visual Basic code, and now I want to translate it to Perl. I tried like this but it didn't work.

I don't get any error, but I also don't see the list in the Sharepoint. When I did it using the macro in Excel it worked

use Win32::OLE::Const 'Microsoft Excel';

my $excel = Win32::OLE->new('Excel.Application');
$excel->{'Visible'} = 1;
$excel->{DisplayAlerts} = 1;

my $book = $excel->Workbooks->Open("C:\\Book1.xlsx")
    || die("Unable to open document ", Win32::OLE->LastError());

my $list = $book->ActiveSheet->ListObjects("Table1")->Publish Array("https:\/\/sponsor\/sites\/dev_test_site", "myname"), False;  

The original Visual Basic code

Sub Macro1()

  ActiveSheet.ListObjects("Table1").Publish Array( _
    "https://sponsor/sites/dev_test_site", "myname"), False
  Range("C2").Select
End Sub

Eventually I came up with this code

my $excel = Win32::OLE->new('Excel.Application');
$excel->{'Visible'} = 1;
$excel->{DisplayAlerts} = 1;

my $book = $excel->Workbooks->Open("C:\\Book1.xlsm")
    || die("Unable to open document ", Win32::OLE->LastError());

my @array=("https:\/\/sponsor\/sites\/dev_test_site", "aaaa");

my $list= $book->ActiveSheet->ListObjects("hhhh")->Publish(@array, 0);

And this image shows the result

在此输入图像描述

You should use strict and use warnings . It will proceed to tell you a number of error messages then.

What I can make out from the Perl code you posted without running it is:

my $list= $book->ActiveSheet->ListObjects("Table1")->Publish Array("https:\/\/sponsor\/sites\/dev_test_site", "myname"), False;

Note that there is a space between Publish and Array( . That has to be a problem. The only way to have a function cal followed by something other than ( or ; or , is if it has prototypes. But method calls in object oriented Perl cannot have prototypes. So that is definitely wrong.

Then there's Array(...) . There is no built-in function called Array and I do not think that Win32::OLE::Const exports that, though I did not look. Even if it did, you told it to only export 'Microsoft Excel' . The same goes for False .

I suggest you read the documentation of Win32::OLE::Const and add use strict and use warnings . There are also some resources of how to work with Win32 modules on Sinan Ünürs blog .

You could take a look at this: Convert perl script to vba This already has some answers.

You may need to follow this script. % pp -o hello hello.pl or something like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM