C++ How to use Range parameter with InlineShapes AddPicture() method

Viewed 69

There is very limited documentation on using this method with C++. Most of the documentation is for VB. Please help me to 1) create a range object 2) use this range object with the AddPicture() method. Here is the AddPicture definition for C++:

LPDISPATCH InlineShapes::AddPicture(LPCTSTR FileName, VARIANT* LinkToFile, VARIANT* SaveWithDocument, VARIANT* Range)

Below is working code that inserts an image into a word document. It inserts at top of doc because the range parameter(4th parameter, currently 'covOptional') is not specified. There is other code that sets up m_disp to interact with document of interest.

_Document objDoc;
COleVariant covOptional;
//instantiate the document object
objDoc.AttachDispatch(m_disp);
//adding image to doc
InlineShapes objInlineShapes(objDoc.GetInlineShapes())
objInlineShapes.AddPicture("C:\\QR.png", covOptional, covOptional, covOptional);

Here is more info on what I am trying to do incase there are alternative ways. I have a word document that I need to add a png image to. I see a couple ways of doing this: 1) hardcode range objects that specify the position in the document of the png to be inserted into 2) add anchor strings (ex. %pngLocation%) to the document. Find a way to return a range that represents this string's location. Use that range with the AddPicture() method.

1 Answers

I had to use a different msword library, but I got this to work with the following code.

 #import "C:\Program Files (x86)\Microsoft Office\root\Office16\MSWORD.OLB" named_guids raw_native_types rename("ExitWindows", "WordExitWindows") rename("FindText", "WordFindText"), rename("VBE", "testVBE")

#include "path\debug\msword.tlh"
//setting up 
 Word::WindowPtr pWindow = w_app.GetActiveWindow();
 Word::Range* pRange = pWindow->Selection->GetRange();
 pRange->Start = 20;
 pRange->End = 20;
 VARIANT vTargetRange;
 vTargetRange.vt = VT_DISPATCH;
 vTargetRange.pdispVal = pRange;

I was able to use '&vTargetRange' as the range parameter in AddPicture(). Thank you to Castorix31.

Related