############################################################################### ### In excel ### ### select picture in the 1st sheet, and paste it to 2nd sheet. ### Then select another picture in 2nd sheet Sub Macro1() ' ' Macro1 Macro ' ' Sheets("picTest").Select Selection.Copy Sheets("Sheet1").Select Range("D7").Select ActiveSheet.Paste Selection.Copy Application.Goto Reference:="Macro1" End Sub ################################################################################ ### In word ### ### at the end of word file, recording the marco ### Ctrl+V to paste the picture which was copied in excel file ### Then, alignment, paragraph Sub Macro2() ' ' Macro2 Macro ' ' Selection.PasteAndFormat (wdPasteDefault) Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter Selection.MoveLeft Unit:=wdCharacter, Count:=1 Selection.TypeParagraph End Sub ############################################################################### ############################################################################### ### In excel ### ### select chart in the one sheet. Sub Macro2() ' ' Macro2 Macro ' ' Sheets("picTest").Select ActiveSheet.ChartObjects("Chart 9""").Activate ActiveSheet.ChartObjects("Chart 9").Activate ActiveChart.ChartArea.Copy Application.Goto Reference:="Macro2" End Sub ################################################################################ ### In word ### ### paste the picture which copied in the excel Sub Macro3() ' ' Macro3 Macro ' ' Selection.TypeParagraph Selection.MoveUp Unit:=wdLine, Count:=1 Selection.PasteAndFormat (wdChartLinked) End Sub #### ##################################################### ########### loading module ##################################################### use strict; use Win32::OLE; use Win32::OLE qw(in with); use Win32::OLE::Const; $Win32::OLE::Warn = 3; use Win32::OLE::Const 'Microsoft.Excel'; use Win32::OLE::Const 'Microsoft.Word'; use Win32::OLE::Const 'Microsoft Office'; ########### loading module end ##################################################### ########### workdir and input file definition ##################################################### my $workdir = "E:\\workdir\\"; my $excelFileName = "test.xlsx"; ########### workdir and input file definition ##################################################### ########### open the excel file ##################################################### my $srcExcelName = $workdir.$excelFileName; my $excelObject=Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); $excelObject->{DisplayAlerts} = 1; my $xlsBook=$excelObject->WorkBooks->Open($srcExcelName); my $xlsSheet = $xlsBook->Worksheets("picTest"); my $iCount=0; my $picFileType="BMP"; my @picFileArray; #foreach my $picObj ( $xlsSheet->Pictures ){ #foreach my $picObj ( $xlsSheet->Shapes ){ # print "aaaaaaaaaaa".$iCount."\n"; # my $picSaveName = $workdir."aa".$iCount++.".".$picFileType; #} foreach my $ChartObj (in $xlsSheet->ChartObjects) { my $savename = $workdir."aa".$iCount++.".".$picFileType; $ChartObj->Chart->Export({ FileName => $savename, FilterName => $picFileType, Interactive => 0}); push @picFileArray, $savename; } ##################################################### ########### open the word file for saving ##################################################### my $wordBasicName=$excelFileName; $wordBasicName =~s/\.xlsx?$//; my $saveWordName=$workdir.$wordBasicName.".docx"; my $wordObject = CreateObject Win32::OLE 'Word.Application' or die $!; $wordObject->{'Visible'} = 1; my $document = $wordObject->Documents->Add; my $selection = $wordObject->Selection; ########### open the word file for saving foreach ( @picFileArray ){ insert_picture( $selection, $_ ); } ######################################################### ############# sub function for insert_picture ######################################################### sub insert_picture { my $selection = shift; my $picFileName = shift; $selection -> TypeParagraph; $selection -> InlineShapes->AddPicture({FileName=>$picFileName}); $selection -> TypeParagraph; } ############# sub function for insert_picture #################