none
Copy paragraph to a table cell and style it RRS feed

  • Question

  • I have an application where I need to combine 2 word files (in English and Spanish) into a 2-column table of the 3rd word file
    English goes to the left column, Spanish to the right.

    Both files have paragraphs styled as Heading 1, Heading 2, Heading 3
    but in the 3rd file I have 2 sets of Headings as I need to create a resulting doc TOC like this -

    1. Heading 1
    1. Heading 1 Spanish
    1.1. Heading 2 
    1.1. Heading 2 Spanish
    1.1.1. Heading 3
    1.1.1. Heading 3 Spanish
    etc

    How can I do something like this -

    if paragraph in English and has style "Heading 1", then copy and keep that style
    if paragraph in Spanish and has style "Heading 1", then copy and replace style with "Heading 1 Spanish"

    P.S. Also I copy paragraphs into a table I lose Heading styles for some reasons I guess because of the table's style)

    Friday, August 26, 2011 4:59 PM

Answers

  • Hi esb

    Are you saying that the "Spanish" document shouldn't be changed? Or only that you need to be certain that the styles contained in doc3 aren't changed?

    In the first case, create a copy of the Spanish document before opening it, then close without saving (and delete).

    In the second (and first) case, you should be able to copy the styles you need to the Spanish document in order to use them, rather than creating something different. Look up Application.OrganizerCopy in the Word object model...


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by esb1922 Sunday, August 28, 2011 8:06 PM
    Saturday, August 27, 2011 5:31 PM
    Moderator
  • Thank you Cindy, after browsing your replies above and other Word automation posts I figured out a solution that worked

    1. First copy all those styles with name ending with "Spanish" from doc template to doc Spanish

         app.OrganizerCopy(DocTemplatePath, DocSpanishPath, styleName, WdOrganizerObject.wdOrganizerObjectStyles);

    2. Then replace in doc Spanish all "Heading 1" paragraphs with "Heading 1 Spanish"

         foreach (Paragraph p in DocSpanish.Paragraphs)
         {
              Range rng = p.Range;
              Style styleToGet = (Style)rng.get_Style();
              object styleToSet = null;
              if (styleToGet.NameLocal == "Heading 1")
              {
                   styleToSet = "Heading 1 Spanish";
                   p.set_Style(ref styleToSet);
              }

              etc ........

    3. After all with this scenario formatting comes across via clipboard, so

              p.Range.Copy();
              tbl.Rows[row].Cells[1].Range.Paste(); works fine

     

     

    Sunday, August 28, 2011 8:05 PM

All replies

  • Regarding your last comment concerning Heading styles in table cells, that should not be a problem. Why yours aren’t sticking is a bit of a mystery. If you select the entire cell, including the end of cell mark, and set the style then it should be fine.  In the absence of a paragraph mark in the cell, the end of cell marker serves as the depository of the cell’s style.

    Concerning your main question on approaches, my first thought is to use Bookmarks and Ranges and it assumes that these documents are setup much like an outline where an H2 only exists as a subordinate element to an H1 and an H3 only exists as a subordinate of an H2.

    I’d begin by bookmarking all H1 text ranges and I’d name them 1H1, 2H1, 3H1, etc.. Next, for each of the H1 ranges I would define its encompassing area of subordinate headings. So I would assign to a temporary working range variable the text range that begins with the start of 1H1 and ends at the start of 2H1 and from this working range I’d locate and bookmark the subordinate headings.

    What I’d be constructing is a Bookmark name outline that would end up looking like this in the bookmark dialog if opened:

    1H1

    1H1 1H2

    1H1 1H2 1H3

    1H1 2H2 1H3

    1H1 2H2 2H3

    2H1

    2H1 1H2

    2H1 2H2

    2H1 2H2 1H3

    Once this is done it’s all downhill from here … running thru a loop of the named bookmarks, you can put it into the correct column of the table (based on which document you are drawing from), and examine the name and assign the correct style name in the table cell.

    As I said, this was just a first thought and I’m hopeful that someone else will have a simpler and/or more elegant approach.


    Kind Regards, Rich ... http://greatcirclelearning.com

    Saturday, August 27, 2011 2:57 AM
  • Hi esb

    Firstly, with which version of Word or Word file format are we dealing,here?

    Then, which programming language are you using?

    Is this a VSTO solution? If not, the better place for the discussion would be the Word for Developers forum. There's a broader spectrum of word speciealists there who could join in the discussion and offer ideas.

    Right off-hand, I'd say the best thing would be to change the information in the original Spanish document, before importing it to the third document. You could use Word's Find-Replace facility for changing the formatting of Heading 1 to Heading 1 Spanish.

    I'd also like to see the code you're using to import the documents' content to the table before offering an opinion on why you might be losing formatting.


    Cindy Meister, VSTO/Word MVP
    Saturday, August 27, 2011 7:23 AM
    Moderator
  • Hi Cindy, I knew you'd answer :)

    At this moment I do it in C# for Word 2003 and this is not VSTO solution but I guess I can build this piece as a VSTO project, compile and integrate it into my main application, right?

    I got your Word's Find-Replace suggestion but the whole idea is to help formatters before they even open those docs.

    My code is pretty primitive. All I have is 3 opened word documents (aDoc1, aDoc2, aDoc3) and a 2-column table in aDoc3 (tbl)
               
    Then I read paragraphs like
    foreach (Paragraph p1 in aDoc1.Paragraphs)
    {
      Range doc1Range = p1.Range;
      //skip all paragraphs like "\r" or "\r\a" or "\f" and copy/paste the whole range when it's possible
      if (!doc1Range.Text.EndsWith("\r\a"))
      {
         p1.Range.Copy();
         tbl.Rows[row].Cells[1].Range.Paste();
      }
      else
         tbl.Rows[row].Cells[1].Range.Text = doc1Range.Text;
    ...
    }

    Saturday, August 27, 2011 4:31 PM
  • <<I knew you'd answer :)>>

    Heeee - I'm not sure whether that's a good sign :-)

    <<I got your Word's Find-Replace suggestion but the whole idea is to help formatters before they even open those docs.>>

    Are you saying the heading formatting doesn't exist, yet? Or what? I'm not certain I understand the requirement correctly... But if, as your original message says, the Spanish document already has Heading formatting, then (in my thoughts) what you'd need to do in your code is:

    - Open the Spanish document
    - Create the basic Heading n Spanish styles in that document
    - Run Find.Execute on Document.Content (Content is a Range) with the following settings (among others - pseudocode!)
    - Find.Format = true; Find.Style = wdStyleHeading1; Find.Replacement.Style = "Heading 1 Spanish"; Find.Text=""; Find.Replacement.Text = "";

    Then, when you import this text into Doc3 the styles will come with it, since they won't (yet) exist in Doc3. That will save you needing to loop through stuff in the second column in order to change the formatting, which would certainly be a time-consuming undertaking.

    <<My code is pretty primitive. All I have is 3 opened word documents (aDoc1, aDoc2, aDoc3) and a 2-column table in aDoc3 (tbl)>>

    Thank you for the code snippet. If I may make a suggestion, try transferring the ranges more like this:

    tbl.Rows[row].Cells[1].Range.FormattedText = p1.Range.FormattedText

    This will avoid using the Clipboard and should ensure that formatting comes across.


    Cindy Meister, VSTO/Word MVP
    Saturday, August 27, 2011 4:46 PM
    Moderator
  • Thanks. I'll try

    The only thing is that the program shouldn't create any style - the 3rd resulting doc is more like a template, I mean it already has all styles

    Saturday, August 27, 2011 5:01 PM
  • Hi esb

    Are you saying that the "Spanish" document shouldn't be changed? Or only that you need to be certain that the styles contained in doc3 aren't changed?

    In the first case, create a copy of the Spanish document before opening it, then close without saving (and delete).

    In the second (and first) case, you should be able to copy the styles you need to the Spanish document in order to use them, rather than creating something different. Look up Application.OrganizerCopy in the Word object model...


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by esb1922 Sunday, August 28, 2011 8:06 PM
    Saturday, August 27, 2011 5:31 PM
    Moderator
  • Cindy,

    I'll make one more attempt to avoid VSTO - I won't need it in order to use Application.OrganizerCopy and Range.FormattedText

    But I understand tings like Find.Style = wdStyleHeading1; Find.Replacement.Style =... are VSTO specific

    Is there anyway I can replace styles w/o it?

     

    Saturday, August 27, 2011 7:49 PM
  • I couldn't even make this working -

    tbl.Rows[row].Cells[1].Range.FormattedText = p1.Range.FormattedText - got type mismatch error

    Sunday, August 28, 2011 12:10 AM
  • Thank you Cindy, after browsing your replies above and other Word automation posts I figured out a solution that worked

    1. First copy all those styles with name ending with "Spanish" from doc template to doc Spanish

         app.OrganizerCopy(DocTemplatePath, DocSpanishPath, styleName, WdOrganizerObject.wdOrganizerObjectStyles);

    2. Then replace in doc Spanish all "Heading 1" paragraphs with "Heading 1 Spanish"

         foreach (Paragraph p in DocSpanish.Paragraphs)
         {
              Range rng = p.Range;
              Style styleToGet = (Style)rng.get_Style();
              object styleToSet = null;
              if (styleToGet.NameLocal == "Heading 1")
              {
                   styleToSet = "Heading 1 Spanish";
                   p.set_Style(ref styleToSet);
              }

              etc ........

    3. After all with this scenario formatting comes across via clipboard, so

              p.Range.Copy();
              tbl.Rows[row].Cells[1].Range.Paste(); works fine

     

     

    Sunday, August 28, 2011 8:05 PM
  • like I said it all worked fine at home but when I tried to run this app at work, I got

    System.Runtime.InteropServices.COMException (0x800A1735): The requested member of the collection does not exist.
       at Microsoft.Office.Interop.Word.Styles.get_Item(Object& Index)

    What could be wrong? Styles are there but can't be found.

     

    Tuesday, August 30, 2011 1:36 PM
  • Hi esb

    Style names are like xml: capitalization makes a difference. Are you sure the style names are exactly the same at work as at home?


    Cindy Meister, VSTO/Word MVP
    Tuesday, August 30, 2011 3:08 PM
    Moderator