Setting RelativeHorizontalPosition causes "Parameter not found" er

J

Jeff Mastry

I am trying to create a watermark in word via C++ and automation. When I set
the RelativeHorizontalPosition property, I get a "Parameter not found" error.
Here's the code:

HRESULT CWordFactory::AddWaterMarkText( LPCWSTR text )
{
HRESULT hr;
CComVariant result;

CComVariant msoTrueParam( msoTrue );
CComVariant msoFalseParam( msoFalse );
CComVariant optionalParam = CAutoWrap::MakeOptionalParameter();

//-- Get the active window
hr = CAutoWrap::Get( &result, this->m_outputDoc, L"ActiveWindow" );
CComPtr<IDispatch> window = result.pdispVal;

//-- Get the active pane
hr = CAutoWrap::Get( &result, window, L"ActivePane" );
CComPtr<IDispatch> pane = result.pdispVal;

//-- Get the view
hr = CAutoWrap::Get( &result, pane, L"View" );
CComPtr<IDispatch> view = result.pdispVal;

//-- Ensure the document is in preview mode
{
CComVariant viewType( wdPrintView );
hr = CAutoWrap::put( view, L"Type", &viewType );
}

//-- Set document focus to the page header
{
CComVariant seekViewValue( wdSeekCurrentPageHeader );
hr = CAutoWrap::put( view, L"SeekView", &seekViewValue );
}

//-- Get the current selection
hr = CAutoWrap::Get( &result, this->m_wordServer, L"Selection" );

CComPtr<IDispatch> selection = result.pdispVal;

//-- Create the watermark shape in the header (
Selection.HeaderFooter.Shapes.AddTextEffect( ... ) )
hr = CAutoWrap::Get( &result, selection, L"HeaderFooter" );
CComPtr<IDispatch> headerFooter = result.pdispVal;

hr = CAutoWrap::Get( &result, headerFooter, L"Shapes" );
CComPtr<IDispatch> shapes = result.pdispVal;

{
CComVariant textEffect( msoTextEffect2 );
CComVariant textParam( text );
CComVariant fontName( L"Arial Black" );
CComVariant fontSize( 1.0f );
CComVariant left( 0.0f );
CComVariant top( 0.0f );
hr = CAutoWrap::Exec( &result, shapes, L"AddTextEffect", 9,
optionalParam,
top,
left,
msoFalseParam,
msoFalseParam,
fontSize,
fontName,
textParam,
textEffect );
}
CComPtr<IDispatch> shape = result.pdispVal;

//-- Set all of the shape's properties

// TextEffect.NormalizedHeight = msoFalse
{
hr = CAutoWrap::Get( &result, shape, L"TextEffect" );
CComPtr<IDispatch> textEffect = result.pdispVal;

hr = CAutoWrap::put( textEffect, L"NormalizedHeight", &msoFalseParam
);
}

// Line.Visible = msoFalse
{
hr = CAutoWrap::Get( &result, shape, L"Line" );
CComPtr<IDispatch> line = result.pdispVal;

hr = CAutoWrap::put( line, L"Visible", &msoFalseParam );
}

// Fill
{
hr = CAutoWrap::Get( &result, shape, L"Fill" );
CComPtr<IDispatch> fill = result.pdispVal;

//-- Fill.Visible = msoTrue
hr = CAutoWrap::put( fill, L"Visible", &msoTrueParam );

//-- Fill.Solid()
hr = CAutoWrap::Exec( &result, fill, L"Solid", 0 );

//-- Fill.ForeColor.RGB = wdColorGray25
{
hr = CAutoWrap::Get( &result, fill, L"ForeColor" );
CComPtr<IDispatch> foreColor = result.pdispVal;

CComVariant color(wdColorGray25);
hr = CAutoWrap::put( foreColor, L"RGB", &color );
}

//-- Fill.Transparency = 0.5f
CComVariant transparency( 0.5f );
hr = CAutoWrap::put( fill, L"Transparency", &transparency );
}

// Rotation
{
CComVariant rotation( 315.0f );
hr = CAutoWrap::put( shape, L"Rotation", &rotation );
}

// LockAspectRatio
hr = CAutoWrap::put( shape, L"LockAspectRatio", &msoTrueParam );

// Height
{
CComVariant height( 2.82f * 72 );
hr = CAutoWrap::put( shape, L"Height", &height );
}

// Width
{
CComVariant width( 5.64f * 72 );
hr = CAutoWrap::put( shape, L"Width", &width );
}

//-- Select the watermark shape
hr = CAutoWrap::Exec( &result, shape, L"Select", 1, optionalParam );

// RelativeHorizontalPosition
{
CComVariant position( wdRelativeHorizontalPositionMargin );
//****** The following line returns 0x80020004 Parameter not found
hr = CAutoWrap::put( shape, L"RelativeHorizontalPosition", &position
);
}

// RelativeVerticalPosition
{
CComVariant position( wdRelativeVerticalPositionMargin );
hr = CAutoWrap::put( shape, L"RelativeVerticalPosition", &position );
}

// Left and Top
{
CComVariant center( (float)wdShapeCenter );
hr = CAutoWrap::put( shape, L"Left", &center );
hr = CAutoWrap::put( shape, L"Top", &center );
}

//-- Set document focus back to the document
{
CComVariant seekViewValue( wdSeekMainDocument );
hr = CAutoWrap::put( view, L"SeekView", &seekViewValue );
}

return S_OK;
}

Thanks,
Jeff
 
P

Peter Huang [MSFT]

Hi Jeff,

Thanks for your posting.
It seems to be a complex issue, without concrete context it is hard to
reproduce the problem.
Anyway, I suggest you tried to run the equivalent VBA code in the Word
Document.
e.g. Press Alt+F11 and add the macro below to run to see if the whole
procedure of your code will produce any error?

Sub Test()
Dim v As View
Set v = ThisDocument.ActiveWindow.ActivePane.View
v = wdPrintView
v.SeekView = wdSeekCurrentPageHeader
Dim sp As Shape
Set sp = Selection.HeaderFooter.Shapes.AddTextEffect(msoTextEffect2,
"Text", "Arial Black", 1, msoFalse, msoFalse, 0, 0)
....
End Sub

If yes, I suggest you try to tweak the VBA code first. Commonly we can use
the Word's record macro method to observe how the macro will go when we
tried to do something.


Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeff Mastry

Hi Peter,

I "designed" the procedure with VBA from the beginning. The VBA code does
exactly what I intended. Here it is:

ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader

Dim shape As shape
Set shape =
Selection.HeaderFooter.Shapes.AddTextEffect(PowerPlusWaterMarkObject1, _
"VOID", "Arial Black", 1, False, False, 0, 0)
shape.TextEffect.NormalizedHeight = False
shape.Line.Visible = msoFalse
shape.Fill.Visible = msoTrue
shape.Fill.Solid
shape.Fill.ForeColor.RGB = RGB(192, 192, 192)
shape.Fill.Transparency = 0.5
shape.Rotation = 315
shape.LockAspectRatio = msoTrue
shape.Height = 2.82 * 72
shape.Width = 5.64 * 72

shape.Select
shape.RelativeHorizontalPosition = wdRelativeHorizontalPositionMargin
shape.RelativeVerticalPosition = wdRelativeVerticalPositionMargin
shape.Left = wdShapeCenter
shape.Top = wdShapeCenter
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

Let me know what kind of context information you want. If needed, I can send
you the entire project.

Thanks,
Jeff
 
P

Peter Huang [MSFT]

Hi Jeff,

I suggest you import the type into C++, so that we can programming much
easier.

Here is my test code which works fine.

//Added the stuff below into header.
#include <atlbase.h>
#include <atlstr.h>
//import the necessary library
#import "libid:2DF8D04C-5BFA-101B-BDE5-00AA0044DE52" version("2.3")
lcid("0") auto_search auto_rename
#import "libid:0002E157-0000-0000-C000-000000000046" version("5.3")
lcid("0") auto_search auto_rename
#import "libid:00020905-0000-0000-C000-000000000046" version("8.3")
lcid("0") auto_search auto_rename


int _tmain(int argc, _TCHAR* argv[])
{
::CoInitialize(NULL);
Word::_ApplicationPtr wdApp;
wdApp.CreateInstance(_T("Word.Application"));
wdApp->Visible = true;
Word::DocumentsPtr wdDocs = wdApp->Documents;
Word::_DocumentPtr wdDoc = wdDocs->Add();
wdApp->ActiveWindow->ActivePane->View->SeekView =
Word::wdSeekCurrentPageHeader;
_bstr_t text = ::SysAllocString(_T("VOID"));
_bstr_t fontname = ::SysAllocString(_T("Arial Black"));
Word::ShapePtr shape =
wdApp->Selection->HeaderFooter->Shapes->AddTextEffect(Office::MsoPresetTextE
ffect::msoTextEffect2,text,fontname,1,Office::MsoTriState::msoFalse,Office::
MsoTriState::msoFalse,0,0);
shape->TextEffect->put_NormalizedHeight(Office::MsoTriState::msoFalse);
shape->Line->put_Visible(Office::MsoTriState::msoFalse);
shape->Fill->put_Visible(Office::MsoTriState::msoTrue);
shape->Fill->Solid();
shape->Fill->ForeColor->__RGB = RGB(192,192,192);
shape->Fill->Transparency = 0.5;
shape->Rotation = 315;
shape->put_LockAspectRatio(Office::MsoTriState::msoTrue);
shape->Height = 2.82f*72;
shape->Width = 5.64f*72;
shape->Select();
shape->RelativeHorizontalPosition =
Word::wdRelativeHorizontalPositionMargin;
shape->RelativeVerticalPosition = Word::wdRelativeVerticalPositionMargin;
shape->Left = Word::wdShapeCenter;
shape->Top = Word::wdShapeCenter;
wdApp->ActiveWindow->ActivePane->View->SeekView = Word::wdSeekMainDocument;
::CoUninitialize();
return 0;
}

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeff Mastry

Peter,

This is just a small piece of a very large project. It would take tremendous
effort to convert the project to early-binding with "import". Besides there
are a lot of issues with Office and "import" that I don't want to deal with
(and I remember reading somewhere that MS does not recommend using "import"
with Office).

Any other options? I really need this to work.

Thanks,
-Jeff

"Peter Huang" said:
Hi Jeff,

I suggest you import the type into C++, so that we can programming much
easier.

Here is my test code which works fine.

//Added the stuff below into header.
#include <atlbase.h>
#include <atlstr.h>
//import the necessary library
#import "libid:2DF8D04C-5BFA-101B-BDE5-00AA0044DE52" version("2.3")
lcid("0") auto_search auto_rename
#import "libid:0002E157-0000-0000-C000-000000000046" version("5.3")
lcid("0") auto_search auto_rename
#import "libid:00020905-0000-0000-C000-000000000046" version("8.3")
lcid("0") auto_search auto_rename


int _tmain(int argc, _TCHAR* argv[])
{
::CoInitialize(NULL);
Word::_ApplicationPtr wdApp;
wdApp.CreateInstance(_T("Word.Application"));
wdApp->Visible = true;
Word::DocumentsPtr wdDocs = wdApp->Documents;
Word::_DocumentPtr wdDoc = wdDocs->Add();
wdApp->ActiveWindow->ActivePane->View->SeekView =
Word::wdSeekCurrentPageHeader;
_bstr_t text = ::SysAllocString(_T("VOID"));
_bstr_t fontname = ::SysAllocString(_T("Arial Black"));
Word::ShapePtr shape =
wdApp->Selection->HeaderFooter->Shapes->AddTextEffect(Office::MsoPresetTextE
ffect::msoTextEffect2,text,fontname,1,Office::MsoTriState::msoFalse,Office::
MsoTriState::msoFalse,0,0);
shape->TextEffect->put_NormalizedHeight(Office::MsoTriState::msoFalse);
shape->Line->put_Visible(Office::MsoTriState::msoFalse);
shape->Fill->put_Visible(Office::MsoTriState::msoTrue);
shape->Fill->Solid();
shape->Fill->ForeColor->__RGB = RGB(192,192,192);
shape->Fill->Transparency = 0.5;
shape->Rotation = 315;
shape->put_LockAspectRatio(Office::MsoTriState::msoTrue);
shape->Height = 2.82f*72;
shape->Width = 5.64f*72;
shape->Select();
shape->RelativeHorizontalPosition =
Word::wdRelativeHorizontalPositionMargin;
shape->RelativeVerticalPosition = Word::wdRelativeVerticalPositionMargin;
shape->Left = Word::wdShapeCenter;
shape->Top = Word::wdShapeCenter;
wdApp->ActiveWindow->ActivePane->View->SeekView = Word::wdSeekMainDocument;
::CoUninitialize();
return 0;
}

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang [MSFT]

Hi Jeff,

We did not provide large project review in newsgroup support.
For your concern, I suggest you contact MSPSS directly.
http://support.microsoft.com

If you still have any concern, please feel free to post here.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jeff Mastry

I just want to create a watermark in a word document with C++ and late
binding. That shouldn't be beyond the scope of this newsgroup. I apologize
for giving you the impression that I wanted a full project review.

Thanks,
-Jeff
 
P

Peter Huang [MSFT]

Hi Jeff,

Thanks for your posting!

Based on my test Late Binding in VB6, it is OK. So I think the Object Modal
should be OK.
Also it is not a trivial to build a latebinding sample in C++, did you have
any special concern to use latebinding? What is your scenario?
Because according to earlybinding, late binding will be less efficient.

If you do need to use LateBinding, can you build a simple reproduce sample
using LateBinding just I posted before.
I will look into it.

You may package your simple reproduce and send to me via removing "online"
from my email address.

Thanks for your understanding!


Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang [MSFT]

Hi Jeff,

Thanks for your posting!
I am researching the issue and get back to you ASAP.
Thanks!

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang [MSFT]

Hi Jeff,

Based on my research, I think you may try to use the code below to set your
property.
{
VARIANT FAR *pVarResult;
DISPPARAMS dispparams;
memset(&dispparams, 0, sizeof dispparams);
DISPID mydispid = DISPID_PROPERTYPUT;
szMember = L"RelativeHorizontalPosition";
dispparams.rgvarg = new VARIANT[1];
dispparams.rgvarg[0].vt = VT_I4;
dispparams.rgvarg[0].lVal = 0;
dispparams.cArgs = 1;
dispparams.cNamedArgs = 0;
hr = shape->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_USER_DEFAULT,
&dispid);
hr = shape->Invoke(
dispid,
IID_NULL,
LOCALE_USER_DEFAULT,
DISPATCH_PROPERTYPUT,
&dispparams, NULL, NULL, NULL);
}

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang [MSFT]

Hi Jeff,

You are welcomed!
I am glad that my suggestion helps you.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top