none
Add Text/Image to Word Document with format. RRS feed

  • Question

  • Hi All,

    Currently, I can add Text or Image with Word application's default format. I want to assign format to the Text or Image when I insert them to Word Document. I searched through MSDN Document, But I didn't find documents about this scenario. Could you please let me know how to achieve this? Or is there any document about this scenario.

    	Globals.ThisAddIn.Application.ActiveDocument.Range(Start: start, End: end).Copy();
    	var doc = Globals.ThisAddIn.Application.Documents.Add();
    	doc.Range().Paste();

    Thanks.

    Tuesday, December 2, 2014 8:32 AM

Answers

  • You cannot paste an image as text. You can, however, paste some text content (eg content copied from another document) as an image. The VBA code to do that looks like:

    Selection.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, Placement:=wdInLine, DisplayAsIcon:=False


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, December 2, 2014 12:11 PM
  • Hi Ricky Yan,

    >>Currently, I can add Text or Image with Word application's default format.<<

    What did you mean the default format for the image? Based on the code, you are copy a range from one document to another. If you don't copy the format from the orignal document and want the text is formated to previews text's format, you can use code below:

     Selection.PasteAndFormat (wdFormatPlainText)

    >> I want to assign format to the Text or Image when I insert them to Word Document<<

    If you want to format the text or image use PIAs, I suggest that you can use Record Macros.

    It is a good way to learn how to navigate the object models of Word, Excel, and Powerpoint, based on what you want to do as a user.

    Keep in mind the recorded macro won't be the best possible code in most cases, but it's does very well for a quick example.

    You can get more detail about Word object model from link below:
    Object model reference (Word 2013 developer reference)

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, December 3, 2014 2:24 AM
    Moderator

All replies

  • You cannot paste an image as text. You can, however, paste some text content (eg content copied from another document) as an image. The VBA code to do that looks like:

    Selection.PasteSpecial Link:=False, DataType:=wdPasteEnhancedMetafile, Placement:=wdInLine, DisplayAsIcon:=False


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Tuesday, December 2, 2014 12:11 PM
  • Hi Ricky Yan,

    >>Currently, I can add Text or Image with Word application's default format.<<

    What did you mean the default format for the image? Based on the code, you are copy a range from one document to another. If you don't copy the format from the orignal document and want the text is formated to previews text's format, you can use code below:

     Selection.PasteAndFormat (wdFormatPlainText)

    >> I want to assign format to the Text or Image when I insert them to Word Document<<

    If you want to format the text or image use PIAs, I suggest that you can use Record Macros.

    It is a good way to learn how to navigate the object models of Word, Excel, and Powerpoint, based on what you want to do as a user.

    Keep in mind the recorded macro won't be the best possible code in most cases, but it's does very well for a quick example.

    You can get more detail about Word object model from link below:
    Object model reference (Word 2013 developer reference)

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, December 3, 2014 2:24 AM
    Moderator
  • Wow, Amazing answer. Thank you very much Fei.
    Friday, December 5, 2014 3:02 AM