Hello,

I just recently started trying to write code that would automate a
"reporting" function by writing to a microsoft word document. At this
point, all I'm trying to do is open a new document, write some text,
insert a picture, write some more text, save the file as a specific
name, and then quit word. Here is the code I've written:

_Application objWordApp; 
_Document objDoc; 
LPDISPATCH lpDisp; 
Range objRange; 


// Common OLE variants that are easy to use for calling arguments. 
COleVariant covTrue((short)TRUE), 
        covFalse((short)FALSE), 
        covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR); 


objWordApp.CreateDispatch("Word.Application"); 
objWordApp.SetVisible(FALSE); 


Documents docs(objWordApp.GetDocuments()); 
lpDisp = docs.Add(covOptional, covOptional, covOptional, covOptional); 
objDoc.AttachDispatch(lpDisp); 
lpDisp = objDoc.GetContent(); 
objRange.AttachDispatch(lpDisp); 


//move insert pointer and write text 
//objRange.Collapse(COleVariant((long)0));  //0 = wdCollapseEnd, 1 = 
wdCollapseBeginning 
objRange.InsertAfter("This is the text that will go before each 
picture"); 
objRange.InsertParagraphAfter(); 
objRange.InsertParagraphAfter(); 


InlineShapes objInlineShapes(objDoc.GetInlineShapes()); 
objInlineShapes.AddPicture("C:\\jentek\\Visualization\\Testimage.bmp", 
covOptional, covOptional, covOptional); 


objRange.InsertParagraphAfter(); 
objRange.InsertAfter("This is the text that will go after each 
picture"); 
objRange.InsertParagraphAfter(); 


objDoc.SaveA(COleVariant"C:\\Users\\Scott\\Plugins\\Reporting\ 
\Reporting\\woohoo.doc",VT_BSTR), 
        covOptional, covOptional, covOptional, covOptional, covOptional, 
        covOptional, covOptional, covOptional, covOptional, covOptional, 
        covOptional, covOptional, covOptional, covOptional, covOptional); 


AfxMessageBox("Report finished..."); 
objDoc.Save(); 
objDoc.SetSaved(TRUE);  // Hides the "Save your changes..." dialog. 
AfxMessageBox("Word will close now. Goodbye"); 


objWordApp.Quit(covFalse, covFalse, covFalse);

It works perfectly except for one thing: The picture, no matter what I
seem to do, gets inserted at the beginning of the file and not after
the first line of text. How do I control where the picture gets
inserted? I've been looking all over the internet but without any
luck...I think it has to do with the final "range" option of the
InlineShapes.AddPicture method, but I cannot figure out how to set
it. All the online documentation seems to be for .Net c++ and VB, but
not for the old school C++ that I'm using.


Any help would be greatly appreciated.

thanks for sharing the codes. looks like selection needs to be used to move the insertion point:

range.InsertAfter("Test");
	range.InsertParagraphAfter(); 

	WordSelection selection(word.get_Selection());
	COleVariant wdStory((short)6), wdMove((short)0);

	selection.EndKey(&wdStory, &wdMove);
	WordInlineShapes shapes(selection.get_InlineShapes());
	shapes.AddPicture("xxx.bmp", covOptional, covOptional, covOptional);

related article:
http://www.microsoft.com/technet/scriptcenter/resources/officetips/may05/tips0519.mspx

cheng

This article has been dead for over six months. Start a new discussion instead.