Inserting a picture in MS Word (placement)

S

sasha

Hello!

I'm struggling somewhat with inserting a picture from file in MS Word
using the type library COM interfaces.

My problem is that the picture gets inserted at the top of the document,
i.e. not obeying paragraphs, existing text, etc.

I would appreciate any and all suggestions on how to fix that. Must be
something simple I'm missing with the formatting.

I'm including the code below (hope the formatting stays put!)

TIA,
..a

BookmarksPtr bookMarks( wordApp->ActiveDocument->Bookmarks );


// add paragraph
ParagraphPtr para;

BookmarkPtr bookMark;
bookMarks->Item( TVariant( _T( "\\endofdoc" ) ), &bookMark );
WordRangePtr range( bookMark->Range );
para = wordApp->ActiveDocument->Content->Paragraphs->Add(
&TVariant( static_cast <IUnknown*>( range ) ) );

para->Range->Text = WideString( _T( "New Para" ) ).c_bstr();
para->Range->InsertParagraphAfter();

if ( bookMark ) {
InlineShapesPtr shapes = para->Range->InlineShapes;
shapes->AddPicture( WideString( picture ).c_bstr(),
TNoParam(),TNoParam(),
&TVariant( static_cast <IUnknown*>( range ) ) );
bookMark->Range->ParagraphFormat->SpaceAfter = 6;
}
//insert text
TablesPtr tables( wordApp->ActiveDocument->Tables );

if ( tables ) {

BookmarkPtr bookMark;
bookMarks->Item( TVariant( _T( "\\endofdoc" ) ), &bookMark );
WordRangePtr range( bookMark->Range );
TablePtr table( tables->Add( range, strings.size(), 1 ) );
if ( range ) {

if ( table ) {
std::list <String>::const_iterator it = strings.begin();

int row( 0 );
while ( it != strings.end() ) {
log->Lines->Add( String( it->c_str() ) );

CellPtr cell;
table->Cell( row++, 0, &cell );

if ( cell ) {
cell->Range->Text = WideString( it->c_str()
).c_bstr();
}
++it;
}
}
}

}
 
S

sasha

In case anyone is interested, here is the code that seems to be working
for me - inserting picture where I want it :)

BookmarksPtr bookMarks( wordApp->ActiveDocument->Bookmarks );

// add paragraph
ParagraphPtr para;

BookmarkPtr bookMark;
bookMarks->Item( TVariant( _T( "\\endofdoc" ) ), &bookMark );
WordRangePtr range( bookMark->Range );
para = wordApp->ActiveDocument->Content->Paragraphs->Add(
&TVariant( static_cast <IUnknown*>( range ) ) );

if ( para ) {
para->Range->Text = WideString( _T( "New Para" ) ).c_bstr();
para->Range->InsertParagraphAfter();
}

// insert a picture
bookMark.Reset();
if ( SUCCEEDED( bookMarks->Item( TVariant( _T( "\\endofdoc" ) ),
&bookMark ) ) ) {
if ( bookMark ) {
InlineShapesPtr shapes = para->Range->InlineShapes;
range = bookMark->Range;
shapes->AddPicture( WideString( picture ).c_bstr(),
TNoParam(),TNoParam(),
&TVariant( static_cast <IUnknown*>( range ) ) );
bookMark->Range->ParagraphFormat->SpaceAfter = 6;
}
}
//insert text
TablesPtr tables( wordApp->ActiveDocument->Tables );

if ( tables ) {

BookmarkPtr bookMark;
bookMarks->Item( TVariant( _T( "\\endofdoc" ) ), &bookMark );
WordRangePtr range( bookMark->Range );
TablePtr table( tables->Add( range, strings.size(), 1 ) );

BordersPtr borders = table->Borders;
if ( borders ) {
borders->Enable = true;
}
if ( range ) {

if ( table ) {

std::list <String>::const_iterator it = strings.begin();

int row( 0 );

while ( it != strings.end() ) {

log->Lines->Add( String( it->c_str() ) );

CellPtr cell;
table->Cell( ++row, 0, &cell );

if ( cell ) {
cell->Range->Text = WideString( it->c_str()
).c_bstr();
}
++it;
}
}
}
}
 

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