No margins at header photo of report using ActXWord

2 views (last 30 days)
Hi,
I have created a word report using matlab code (ActXWord).
For the header i import a picture, which i would like to have no margins and cover the header from the upper left corner of page.
ActXWord.Selection.Application.ActiveWindow.View.SeekView = 1; % go to header
ActXWord.Selection.ParagraphFormat.Alignment=0; % left allignment
WordHandle.Sections.Item(1).Headers.Item(1).Range.InlineShapes.AddPicture([pwd '\header_photo.png'],0,1);
Before i have set the margins of the main body of report:
ActXWord.Selection.Pagesetup.RightMargin=28.34646 * 2;
ActXWord.Selection.Pagesetup.LeftMargin=28.34646 * 2;
ActXWord.Selection.Pagesetup.TopMargin=28.34646 * 2.54;
ActXWord.Selection.Pagesetup.BottomMargin=28.34646 * 2.54;
That way the header photo is complying to main body margins.
How to set different margins for the header photo of word header without changing the main body margins?
Thanks in advance!

Answers (1)

Rahul
Rahul on 21 Jan 2025 at 6:45
In order to set margins of a header photo without changing the margins of the body of the document while using 'ActXWord' consider referring to the following FileExchanges submission and MATLAb Answer:
Additionally, you can consider using 'ConvertToShape' method of Word VBA which can be accessed in MATLAB and set the position of the image in the following way:
ActXWord.Selection.Application.ActiveWindow.View.SeekView = 1;
% Insert the picture into the header
headerPicture = WordHandle.Sections.Item(1).Headers.Item(1).Range.InlineShapes.AddPicture([pwd '\header_photo.png'],0,1);
% Access the picture's shape properties
shape = headerPicture.ConvertToShape();
% Set the position of the picture
shape.WrapFormat.Type = 'wdWrapBehind'; % Allows the picture to be positioned freely
shape.Left = 0;
shape.Top = 0;
ActXWord.Selection.Application.ActiveWindow.View.SeekView = 0;
Refer to the following MathWorks documentations to know more:
Thanks.

Categories

Find more on MATLAB Report Generator in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!