Adding text to a Shape using Microsoft.Office.Interop.Word

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Dransfield
    New Member
    • Jan 2011
    • 46

    Adding text to a Shape using Microsoft.Office.Interop.Word

    I am trying to add text to a shape but I can't figure out how to do it.. I am using the method

    Code:
    public void DrawOnDoc(object oMissing, Microsoft.Office.Interop.Word._Document oDoc)
            {
                oDoc.Shapes.AddShape(1, (xPos / 2) + 100, (yPos / 2) + 100, nWidth / 2, nHeight / 2, oMissing);
            }
    to add a shape to a document, but I can't find anything about adding text to the shape. Is it possible to do this?
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I would recommend using MsoAutoShapeTyp e for the type instead of just the number, it makes your code a bit easier to read :D

    As for adding text to it... I'm not sure. I notice that the AddShape method returns a Shape object, but I don't see any text in there. Hmm...

    You might be able to get it from the document itself. This article seems to suggest you can..



    I tried to make a quick program to test but I can't seem to find the reference to Microsoft.Offic e.Interop.Word (or whatever it is), it's not in my list. I wonder if it's because I have the Express edition of VS...

    Comment

    • Alex Dransfield
      New Member
      • Jan 2011
      • 46

      #3
      I have that too. I had to add it manually as a reference. Microsoft.Offic e.Core and Microsoft.Offic e.Interop.Word are what I have. It's under the COM tab of Add References. Microsoft Office 14.0 Object Library, or something like that.

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        There we go.

        And I knew it had to be in there somewhere...

        Code:
        Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
        Microsoft.Office.Interop.Word.Document oWordDoc = new Microsoft.Office.Interop.Word.Document();
        
        oWord.Visible = true;
        oWordDoc = oWord.Documents.Add();
        
        Microsoft.Office.Interop.Word.Shape s = oWordDoc.Shapes.AddShape((int)Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, 0, 0, 100, 100);
        s.TextFrame.TextRange.Text = "Blah";

        Comment

        • Alex Dransfield
          New Member
          • Jan 2011
          • 46

          #5
          Thank you again. Remind me to put you in my will.

          Comment

          • GaryTexmo
            Recognized Expert Top Contributor
            • Jul 2009
            • 1501

            #6
            I like cookies... ;)

            It was tough to find information on that... two people looking is better than one, I just happened to figure it out before you this time, but I'm pretty sure you'd have gotten there.

            Comment

            Working...