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:ut( view, L"Type", &viewType );
}
//-- Set document focus to the page header
{
CComVariant seekViewValue( wdSeekCurrentPageHeader );
hr = CAutoWrap:ut( 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:ut( textEffect, L"NormalizedHeight", &msoFalseParam
);
}
// Line.Visible = msoFalse
{
hr = CAutoWrap::Get( &result, shape, L"Line" );
CComPtr<IDispatch> line = result.pdispVal;
hr = CAutoWrap:ut( line, L"Visible", &msoFalseParam );
}
// Fill
{
hr = CAutoWrap::Get( &result, shape, L"Fill" );
CComPtr<IDispatch> fill = result.pdispVal;
//-- Fill.Visible = msoTrue
hr = CAutoWrap:ut( 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:ut( foreColor, L"RGB", &color );
}
//-- Fill.Transparency = 0.5f
CComVariant transparency( 0.5f );
hr = CAutoWrap:ut( fill, L"Transparency", &transparency );
}
// Rotation
{
CComVariant rotation( 315.0f );
hr = CAutoWrap:ut( shape, L"Rotation", &rotation );
}
// LockAspectRatio
hr = CAutoWrap:ut( shape, L"LockAspectRatio", &msoTrueParam );
// Height
{
CComVariant height( 2.82f * 72 );
hr = CAutoWrap:ut( shape, L"Height", &height );
}
// Width
{
CComVariant width( 5.64f * 72 );
hr = CAutoWrap:ut( 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:ut( shape, L"RelativeHorizontalPosition", &position
);
}
// RelativeVerticalPosition
{
CComVariant position( wdRelativeVerticalPositionMargin );
hr = CAutoWrap:ut( shape, L"RelativeVerticalPosition", &position );
}
// Left and Top
{
CComVariant center( (float)wdShapeCenter );
hr = CAutoWrap:ut( shape, L"Left", ¢er );
hr = CAutoWrap:ut( shape, L"Top", ¢er );
}
//-- Set document focus back to the document
{
CComVariant seekViewValue( wdSeekMainDocument );
hr = CAutoWrap:ut( view, L"SeekView", &seekViewValue );
}
return S_OK;
}
Thanks,
Jeff
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:ut( view, L"Type", &viewType );
}
//-- Set document focus to the page header
{
CComVariant seekViewValue( wdSeekCurrentPageHeader );
hr = CAutoWrap:ut( 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:ut( textEffect, L"NormalizedHeight", &msoFalseParam
);
}
// Line.Visible = msoFalse
{
hr = CAutoWrap::Get( &result, shape, L"Line" );
CComPtr<IDispatch> line = result.pdispVal;
hr = CAutoWrap:ut( line, L"Visible", &msoFalseParam );
}
// Fill
{
hr = CAutoWrap::Get( &result, shape, L"Fill" );
CComPtr<IDispatch> fill = result.pdispVal;
//-- Fill.Visible = msoTrue
hr = CAutoWrap:ut( 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:ut( foreColor, L"RGB", &color );
}
//-- Fill.Transparency = 0.5f
CComVariant transparency( 0.5f );
hr = CAutoWrap:ut( fill, L"Transparency", &transparency );
}
// Rotation
{
CComVariant rotation( 315.0f );
hr = CAutoWrap:ut( shape, L"Rotation", &rotation );
}
// LockAspectRatio
hr = CAutoWrap:ut( shape, L"LockAspectRatio", &msoTrueParam );
// Height
{
CComVariant height( 2.82f * 72 );
hr = CAutoWrap:ut( shape, L"Height", &height );
}
// Width
{
CComVariant width( 5.64f * 72 );
hr = CAutoWrap:ut( 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:ut( shape, L"RelativeHorizontalPosition", &position
);
}
// RelativeVerticalPosition
{
CComVariant position( wdRelativeVerticalPositionMargin );
hr = CAutoWrap:ut( shape, L"RelativeVerticalPosition", &position );
}
// Left and Top
{
CComVariant center( (float)wdShapeCenter );
hr = CAutoWrap:ut( shape, L"Left", ¢er );
hr = CAutoWrap:ut( shape, L"Top", ¢er );
}
//-- Set document focus back to the document
{
CComVariant seekViewValue( wdSeekMainDocument );
hr = CAutoWrap:ut( view, L"SeekView", &seekViewValue );
}
return S_OK;
}
Thanks,
Jeff