C++ Using AddPicture() InlineShapes method with Range parameter

mslice2000 21 Reputation points
2022-06-02T20:49:18.777+00:00

Please help, I have exhausted all other resources on the internet.

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 or range parameter type 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.

Developer technologies | C++
Developer technologies | C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
{count} votes

Answer accepted by question author
  1. Castorix31 91,416 Reputation points
    2022-06-02T22:04:05.547+00:00

    For example, this is a part of code of a C++/Win32 app (by using #import on MSWORD.OLB) where I insert an image at 200th character :
    (with the simplest form of Range, from Range object)

    		Word::_Application* pWord;  
    		// Get Word Application in pWord...  
    		Word::DocumentsPtr pDocs = pWord->Documents;  
    		Word::_DocumentPtr pDoc = pDocs->Open2000(&variant_t("E:\\Test.doc"),&variant_t(false), &variant_t(false), &variant_t(false));  
    		Word::WindowPtr pWindow = pWord->GetActiveWindow();  
            Word::Range* pRange = pWindow->Selection->GetRange();  
    		pRange->Start = 200;  
    		pRange->End = 200;  
      
    		VARIANT vTargetRange;  
    		vTargetRange.vt = VT_DISPATCH;  
    		vTargetRange.pdispVal = pRange;  
    		  
    		Word::InlineShapes* pInlineshapes;  
    		pInlineshapes = pDoc->GetInlineShapes();  
    		  
    		CComVariant covTrue((short)true);  
    		CComVariant covFalse((short)false);  
    		BSTR bstr = SysAllocString(L"E:\\hulk.png");  
    		pInlineshapes->AddPicture(bstr, &covFalse, &covTrue, &vTargetRange);  
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.