Question

I have a word document that I generate through automation. I need to put a chart (generated in Excel) inside this document, and resize the chart so it would not exceed the margins of the document. How can I do that? Visual Basic does not record this activity when recording a macro, so I have no idea on how to proceed.

Any thoughts?

My approach is:

var
MsWord,E:OleVariant;
name:Variant;
...
try
    MsWord := GetActiveOleObject('Word.Application');
  except
    try
      MsWord := CreateOleObject('Word.Application');
      MsWord.Visible := False;
    except
      Exception.Create('Error');
    end;
  end;
MSWord.Documents.Add;
MSWord.Selection.TypeText('some text');
...
E:=CreateOleObject('Excel.Application');
E.Workbooks.Add;
E.ActiveWorkbook.Sheets.Item[1].Select;
name:=E.Charts.Add.Name;
E.Charts.Item[name].ChartType:=1;
E.ActiveWorkbook.Charts.Item[name].SetSourceData(Source:=E.ActiveWorkbook.Sheets.Item[1].Range['A1:B6'],PlotBy:=2);

E.ActiveWorkbook.Charts.Item['Chart1'].Select;
E.Selection.Copy;
MSWord.Selection.PasteSpecial(,,,,wdPasteOleObject);

So this is how I add to Word a chart generated in Excel. The only problem is that the chart is too big, I need to make it smaller so it would fit better in the word document without the user interaction

I tried to record the resizing done in word, and the macro says:

ActiveDocument.Shapes("Object 2").Select
Selection.ShapeRange.ScaleWidth 0.81, msoFalse, msoScaleFromTopLeft
Selection.ShapeRange.ScaleHeight 0.81, msoFalse, msoScaleFromTopLeft

so I tried:

  MSWord.ActiveDocument.Shapes('Object 2').Select;
  MSWord.Selection.ShapeRange.ScaleWidth(0.51, 0, 0);
  MSWord.Selection.ShapeRange.ScaleHeight(0.51, 0, 0);

However, apparently, for Delphi: "Shapes" is not a method So it does not work this way either.

If I change

 MSWord.ActiveDocument.Shapes('Object 2').Select;

to

 MSWord.ActiveDocument.Shapes['Object2'].Select;

then I get: "Item" is not a property

Please help.

Thank you

Was it helpful?

Solution

You can access the shape like this:

MsWord.ActiveDocument.Shapes.Item('Object 2').Select;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top