0

I am trying to create an OpenXml word document that looks as follows:

2.1.1 This is a paragraph

2.1.2 This is also a paragraph

2.1.2.1 This paragraph relates to 2.1.2

The initial plan is to have the number in its own paragraph, and have the text in its own paragraph as well but the problem is that there a line breaks the interfere with the structure of the list items.

I have tried using OpenXml numbering part but the problem there is that I couldn't figure out how to write custom numbers for the numbering.

I have tried to use Justification and Indentation classes but I can't seem to get the desired result, due to the line breaks that come with each paragraph causing the paragraphs to print one below the other.

Is there an alternative way to achieve this result or a way to remove the line breaks for a paragraph?

  • Exactly what you want isn't at all clear. Can you create this in the Word UI, as an end-user? If not, there's little chance VBA can do it... But then try creating a mock-up of what the result should be and provide a screenshot so we can understand what the requirement is. My gut feeling is that you need to work with styles linked to the numbering levels, to ensure the numbering carrying on, regardless of the content between the numbered items... You might need to do some research at the end-user level on using styles with numbering. – Cindy Meister Jan 06 '20 at 12:57
  • In Word, text blocks can only appear beside each other if they are in a 2-column section with section breaks before and after the 2-column area, or they can be in adjancent table cells, or they can be in a pair of text boxes or in a pair of frames. So you'll have to create one of those in Open XML. I would try the 2-column or table cell approaches, those seem like they would be simpler to code. Setting up numbering styles in the user interface is hard enough, I try to use a base Word file with pre-formatted styles to fill in with Open XML when complex styles are required. – John Korchok Jan 06 '20 at 15:49
  • Could you explain why you want the number (e.g., "2.1.1") and the text (e.g., "This is a paragraph") in separate paragraphs? This is very unusual. – Thomas Barnekow Jan 07 '20 at 23:20

1 Answers1

0

Apologies for not being clear enough when posting the question, but for those who are interested I have managed to solve the problem as follows.

  1. I downloaded OpenXml Productivity tools which as available here
  2. I then created the paragraphs that I wanted in MS Word, which looks like this
  3. I then used the tool to generate the code needed, and here it is.`

    Paragraph paragraph1 = new Paragraph(){ RsidParagraphAddition = "005C6DC7", RsidParagraphProperties = "005C6DC7", RsidRunAdditionDefault = "005C6DC7", ParagraphId = "5F7449EC", TextId = "6B3C1D9C" };
    
        Run run1 = new Run();
        Text text1 = new Text();
        text1.Text = "2.1.2";
    
        run1.Append(text1);
    
        Run run2 = new Run();
        TabChar tabChar1 = new TabChar();
        Text text2 = new Text();
        text2.Text = "More text";
    
        run2.Append(tabChar1);
        run2.Append(text2);
    
        paragraph1.Append(run1);
        paragraph1.Append(run2);
        return paragraph1;`
    
  • The confusing bit in you question was that you talked about separate **paragraphs** in your question, which is why I had asked you to clarify. Now, did you fully answer your own question or is there anything else? As an additional hint, you can create your specific multi-level numbered list in Word and use the productivity tool to create the required code. – Thomas Barnekow Jan 12 '20 at 16:43