none
Paragraph is added when adding Table to Field

    Question

  • Hi.

     

    I'm using VSTO and Office 2003 (Word).

     

    I have programmatically added a Field using the ActiveDocument.Fields.Add() method.

     

    This field is supposed to act as placeholder for some data I'm loading from a database. I can for instance add a picture, som text or a table to this or these fields (all programmatically).

     

    When adding text or a picture everything works perfectly. When adding a table a paragraph is added before my placeholder (the field). Not a big deal, but when having a large template with a lot of fields, it does get pretty annoying

     

    My code is something like this:

    Code Snippet

    object missing = Type.Missing;

    Table table = ActiveDocument.Tables.Add(myField.Result, rowCount, colCount, ref missing, ref missing);

     

     

    My Word document containing the field looks like this:

    http://peterlindholm.com/field_without_data.bmp

     

    After adding the table to fields range it looks like this:

    http://peterlindholm.com/field_with_data.bmp

     

    Does anyone have an idea to how I can avoid this?

     

    Thanks in advance for any reply.

     

    Best regards,

    Peter Lindholm

    Tuesday, February 12, 2008 3:29 PM

Answers

  •  Peter Lindholm wrote:

    I'm currently using a field of the type Author. I initially was using a bookmark as placeholders for content to be dynamically loaded, but found that when I once had read data into a bookmark I wasn't able to update the data in the field because the range was no longer available as a bookmark.

     

    Considering the current solution with the use of a field as placeholder - is there any way to avoid the insertion of a bookmark, or maybe as a workaround delete the inserted paragraph?

    OK, so this is actually a DocProperty field. I think you'll find, when the field is updated, that your table will disappear... Try clicking in the table and pressing F9 to test this. DocProperty fields link directly to Document Properties stored in the document structure (as in File/Properties).

     

    The bookmark approach: You can (should) alter the code to recreate the bookmark around the text. There are lots of code examples of this technique in the newsgroups and otherwise, on the Internet. The basic approach goes something like this:

     

    Dim bkm as Word.Bookmark

    Dim rng as Word.Range

     

    bkm = Doc.Bookmarks("abc")

    rng = bkm.Range

    rng.Text = "the new data"

    Doc.Bookmarks.Add("abc", rng)

     

    Given that you want to put complex objects (tables) into the target, and not just plain text, there is no field type you ought to be using in Word 2003. (Word 2007 provides Content Controls that would be exactly what you need.) An alternative to a bookmark would be linking an XML schema to the document, then inserting XML nodes. You could then target the Range contained in the XML nodes (the nodes would not get deleted).

    Wednesday, February 13, 2008 8:08 PM

All replies

  • It always helps if we could know what kind of field we're dealing with. And why this kind of field is being used (instead of a bookmark, for example). Word has over 100 fields and, while there are a couple of "obvious" types, what people end up using never ceases to amaze...

     

    Generally, though, yes, inserting a table in more recent versions of Word will generate a new paragraph mark. This is because there is information specific for the table (such as position on the page and whether it has text wrap) that is stored "in" the preceding paragraph. If the table is put "into" something with no available paragraph mark (as is probably the case with your field), then Word will insert a paragraph mark for the purpose.

     

    Wednesday, February 13, 2008 7:13 PM
  • Dear Cindy.

     

    Thank you for your reply.

     

    I'm currently using a field of the type Author. I initially was using a bookmark as placeholders for content to be dynamically loaded, but found that when I once had read data into a bookmark I wasn't able to update the data in the field because the range was no longer available as a bookmark.

     

    Considering the current solution with the use of a field as placeholder - is there any way to avoid the insertion of a bookmark, or maybe as a workaround delete the inserted paragraph?

    Wednesday, February 13, 2008 7:22 PM
  •  Peter Lindholm wrote:

    I'm currently using a field of the type Author. I initially was using a bookmark as placeholders for content to be dynamically loaded, but found that when I once had read data into a bookmark I wasn't able to update the data in the field because the range was no longer available as a bookmark.

     

    Considering the current solution with the use of a field as placeholder - is there any way to avoid the insertion of a bookmark, or maybe as a workaround delete the inserted paragraph?

    OK, so this is actually a DocProperty field. I think you'll find, when the field is updated, that your table will disappear... Try clicking in the table and pressing F9 to test this. DocProperty fields link directly to Document Properties stored in the document structure (as in File/Properties).

     

    The bookmark approach: You can (should) alter the code to recreate the bookmark around the text. There are lots of code examples of this technique in the newsgroups and otherwise, on the Internet. The basic approach goes something like this:

     

    Dim bkm as Word.Bookmark

    Dim rng as Word.Range

     

    bkm = Doc.Bookmarks("abc")

    rng = bkm.Range

    rng.Text = "the new data"

    Doc.Bookmarks.Add("abc", rng)

     

    Given that you want to put complex objects (tables) into the target, and not just plain text, there is no field type you ought to be using in Word 2003. (Word 2007 provides Content Controls that would be exactly what you need.) An alternative to a bookmark would be linking an XML schema to the document, then inserting XML nodes. You could then target the Range contained in the XML nodes (the nodes would not get deleted).

    Wednesday, February 13, 2008 8:08 PM