locked
Pasting from clipboard (text and image) to word using MS OLE in C++. RRS feed

  • Question

  • Hi

    I'm working on small application which requires send some contents (text and images) to clipboard and then from clipboard paste it to active word document. It could be text or images to active open document.

    Though I'm successfully send my contents to clipboard but I'm not able to paste them into opened active word document.

    However I'm able to paste it using Ctlr+V once the contents is send to clipboard.

    I've tried various methods listed on forums but none of them worked for me.

    Here is the snippet of code.

     
    wxString mssg = "This is New ClipBoard String";
    LPTSTR  lptstrCopy; 
    HGLOBAL hglbCopy; 
    hglbCopy = GlobalAlloc(GMEM_MOVEABLE, (mssg.Length() + 1) * sizeof(TCHAR));
    
    lptstrCopy = (LPTSTR)GlobalLock(hglbCopy); 
    memcpy(lptstrCopy, mssg.char_str(), mssg.Length() * sizeof(TCHAR)); 
    lptstrCopy[mssg.Length()] = (TCHAR) 0;    // null character 
    GlobalUnlock(hglbCopy);
    
    OpenClipboard(NULL);
    EmptyClipboard();
    SetClipboardData(CF_TEXT, hglbCopy); 
    CloseClipboard(); 
    
     wxString outWordFile = filename.BeforeFirst('.') + wxString(".doc");
     wxAutomationObject wordDoc2;
                    if (FAILED(CoInitialize(NULL)))
                    {
                        throw ERROR_MSG_CUSTOM(wxTRANSLATE("Please open a Word document first."));
                    }
                    if (!wordDoc2.GetInstance("Word.Application", wxAutomationInstance_UseExistingOnly )) {
                        throw ERROR_MSG_CUSTOM(wxTRANSLATE("Please open a Word document first."));
                    } 
                    wordDoc2.PutProperty("Visible", true);
                   // wordDoc2.PutProperty("Options.Overtype", false);
                    wxAutomationObject actDoc;
                    wordDoc2.GetObject(actDoc, "ActiveDocument");
                    actDoc.CallMethod("Selection.Range.Paste()");

    I'm using wxAutionmationObject to execute OLE command. 

    I've tried all the method

    ActiveDocument.Selection.Paste(),  ActiveDocument.Selection.Range.Paste(), ActiveDocument.Selection.PasteFormat().

    But none of them worked. 

    Please suggest what could be going wrong.

    Thanks and Regards,

    Pradeep

    Friday, December 20, 2013 1:54 PM

Answers

  • The main problem is that Selection isn't a property/member of the Document class. It's a property of the Application class. Not sure what GetInstance returns, but assuming it returns an Application object then that's probably what you need to work with.

    Peter Jamieson

    • Marked as answer by praks411 Monday, December 23, 2013 6:59 PM
    Friday, December 20, 2013 2:00 PM
  • Hello Pradeep,

    I have noticed the following code above:

    actDoc.CallMethod("Selection.Range.Paste()");

    Note, the Selection property of the Application class returns an instance of the Selection class. Then you call the Range property of the Selection class which returns an instance of the Range class. And, finally, you call the Paste method of the Range class. In the single call of CallMethod you are trying to get the job done. That is why I would recommend break the string into separate calls.

    Also, as Peter wrote, the Document class doesn't provide the Selection property.

    • Edited by Eugene Astafiev Friday, December 20, 2013 3:01 PM
    • Marked as answer by praks411 Monday, December 23, 2013 6:59 PM
    Friday, December 20, 2013 2:59 PM

All replies

  • The main problem is that Selection isn't a property/member of the Document class. It's a property of the Application class. Not sure what GetInstance returns, but assuming it returns an Application object then that's probably what you need to work with.

    Peter Jamieson

    • Marked as answer by praks411 Monday, December 23, 2013 6:59 PM
    Friday, December 20, 2013 2:00 PM
  • Hello Pradeep,

    I have noticed the following code above:

    actDoc.CallMethod("Selection.Range.Paste()");

    Note, the Selection property of the Application class returns an instance of the Selection class. Then you call the Range property of the Selection class which returns an instance of the Range class. And, finally, you call the Paste method of the Range class. In the single call of CallMethod you are trying to get the job done. That is why I would recommend break the string into separate calls.

    Also, as Peter wrote, the Document class doesn't provide the Selection property.

    • Edited by Eugene Astafiev Friday, December 20, 2013 3:01 PM
    • Marked as answer by praks411 Monday, December 23, 2013 6:59 PM
    Friday, December 20, 2013 2:59 PM
  • Hi Eugene and Peter

    Thanks for the reply. I tried both suggestion still it is not working.

    As Peter said Selection is not property of Document, it is property of Application.

    I tried this

    Word.Application.Selection.Range.Paste()

    and also

    Word.Application.Document.Application.Selection.Range.Paste(). Also all the combinations like.

    Word.Application.Paste(), Word.Application.Selection.Paste() , Word.Application.Document.Application.Paset() etc

    But none worked for me. I've also tried them in separate calls but still no use.

    Please let me know if you have any other suggestion.

    Thanks and Regards,

    Pradeep

    Saturday, December 21, 2013 4:13 PM
  • Word is the Application, so you'll need something more like 

    Word.Selection.Range.Paste()


    Peter Jamieson

    Saturday, December 21, 2013 4:28 PM
  • Hi Peter

    I've to first call GetInstance on Word application to get an OLE automation object.

    I also tried your method but it won't work.

    Actually Word.Application is working fine for me for other automation like inserting picture into document from jpeg file.

    It is just in Copy()/Paste() method I'm facing the issue.

    Thanks and Regards,

    Pradeep

    Monday, December 23, 2013 9:21 AM
  • Hi Peter and Eugene

    Thanks for your suggestions. It is working now.

    I've tried

    Word.Application.Selection.Paste.

    Pradeep

    Monday, December 23, 2013 6:59 PM