none
how to find shape in word doc ? RRS feed

  • Question

  • Hi

    how can i find a shape in word doc ?

    i want to find a textBox in word doc and add table inside it programmatically (using csharp). to do this, i need the width and height of textBox to fit my generated table according to user settings.

    thanks in advance


    http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx
    Friday, June 24, 2011 9:05 AM

Answers

  • what I referred to as (textbox pointer) is the same as your 'shape' variable.
     
    I don't know C# but something along these lines ...
     
    if (wordDoc.Shapes.Count > 0)
       {
        foreach (Microsoft.Office.Interop.Word.Shape shape in wordDoc.Shapes)
        {
         string text = shape.AlternativeText;
         if (text.StartsWith("Text Box: ")) // or some better way of identifying your textbox
         { 
    






           Microsoft.Office.Interop.Word.Range textboxrange = shape.TextFrame.ContainingRange






           textboxrange.Tables.Add textboxrange, 2, 2 // change to your table details } } }
     

    Enjoy,
    Tony
    www.WordArticles.com
    • Marked as answer by Hamed_1983 Friday, June 24, 2011 12:39 PM
    Friday, June 24, 2011 12:28 PM

All replies

  • i need the width and height of textBox ...
     
    You can use the Width and Height Properties. To actually find the Textbox in the first place depends on what version of Word you are using, what type of textbox it is, and whatever criteria you have that make this textbox unique. There isn't much more that can be said without further information.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Friday, June 24, 2011 10:21 AM
  • Hi

    i think i found a way to find a textBox via this code :

    if (wordDoc.Shapes.Count > 0)
          {
            foreach (Microsoft.Office.Interop.Word.Shape shape in wordDoc.Shapes)
            {
              string text = shape.AlternativeText;
              if (text.StartsWith("Text Box: "))
              {
                text = text.Remove(0, 10);
                MessageBox.Show(text);
              }
            }                
          }

    but i don't know how to replace a table with that text which inside in this textBox.


    http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx
    Friday, June 24, 2011 10:26 AM
  • That is pretty unreliable; it may identify a text box, but the alternative text has different defaults in different versions of Word and is user changeable. Also, changing the text won't do anything to the textbox contents.
     
    If you have a pointer to the textbox, however you got it, you should be able to get the range from ..
     
        (textbox-pointer).TextFrame.ContainingRange
     
    and then add a table as you would to any range.
     
     

    Enjoy,
    Tony
    www.WordArticles.com
    Friday, June 24, 2011 10:49 AM
  • Hi

    can u tell me what's textbox pointer ? can u give me a simple example ? (i'm using word 2007)

    thanks in advance


    http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx
    Friday, June 24, 2011 11:13 AM
  • what I referred to as (textbox pointer) is the same as your 'shape' variable.
     
    I don't know C# but something along these lines ...
     
    if (wordDoc.Shapes.Count > 0)
       {
        foreach (Microsoft.Office.Interop.Word.Shape shape in wordDoc.Shapes)
        {
         string text = shape.AlternativeText;
         if (text.StartsWith("Text Box: ")) // or some better way of identifying your textbox
         { 
    






           Microsoft.Office.Interop.Word.Range textboxrange = shape.TextFrame.ContainingRange






           textboxrange.Tables.Add textboxrange, 2, 2 // change to your table details } } }
     

    Enjoy,
    Tony
    www.WordArticles.com
    • Marked as answer by Hamed_1983 Friday, June 24, 2011 12:39 PM
    Friday, June 24, 2011 12:28 PM