Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Is there any way to change the margin of MSWord using CNIWordDocument?

I am trying to link the report to MS Word using CNiWordDocument.  Can I change the margin (left and right) of the document from CNiWordDocument?
 
Thanks,
yajai.
0 Kudos
Message 1 of 3
(3,145 Views)

You can do this by calling CNiWordDocument::GetDispatch to get the Word Document IDispatch pointer, then you can use the Word automation interfaces with this IDispatch pointer to perform opreations that aren't exposed by CNiWordDocument. In your case, you can get the Word automation PageSetup object from the document and then adjust the margins. For example, add the following to your stdafx.h (this assumes Office XP; the paths will change if you have an earlier version of Office):

#include "oleacc.h"
#include "atlbase.h"

#pragma warning(push)
#pragma warning(disable: 4278)
 
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\Office10\\mso.dll" \
	rename_namespace("OfficeXP")
using namespace OfficeXP;
 
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.olb" \
	rename_namespace("VBE6")
using namespace VBE6;
 
#import "C:\\Program Files\\Microsoft Office\\Office10\\MSWORD.olb" \
	rename("ExitWindows", "WordExitWindows") \
	named_guids, \
	rename_namespace("MSWord")
using namespace MSWord;
 
#pragma warning(pop)

The following example will move the left and right margins of the document in a little bit:

CNiWordApplication application;
CNiWordDocument document = application.CreateDocument();
  
CComQIPtr<_Document> spDocument(document.GetDispatch(false));
PageSetupPtr spPageSetup = spDocument->PageSetup;
spPageSetup->LeftMargin += 50.0f;
spPageSetup->RightMargin += 50.0f;

Hope this helps.

- Elton

0 Kudos
Message 2 of 3
(3,137 Views)

Thank you so much for your help!!

Yajai

0 Kudos
Message 3 of 3
(3,116 Views)