Skip to content

Interop word insert page break

Imran Shaw

Asked Dec 16, 2014, 8:07 AM EST1 answer
Share
LinkedInX
I am writing a program that loops through an array to write a report in MsWord. When a certain value is encountered I would like to insert a page break. I only want the courses of a subject code list on one page. 

example of output desired:
AHS 20112
AHS 20114
BREAK
BUS 20024
BUS 20025
BUS 20154
BUS 20187
BREAK
CRJ 20006
CRJ 20009


etc...

foreach (FacultyElements F in db._DataHolder)
             {
                  if (PreviousSubCode == string.Empty)
                  {
                      Debug.WriteLine(F.subjCode + " " + F.CRN);
                  }
                  else
                  {
                      r++;
                      if (PreviousSubCode == F.subjCode)
                      {
                          Debug.WriteLine(F.subjCode + " " + F.CRN);
                          objTable.Cell(r, 1).Range.Text = F.subjCode;
                          objTable.Cell(r, 2).Range.Text = F.sessCode;
                          objTable.Cell(r, 3).Range.Text = F.courseNum;
                          objTable.Cell(r, 4).Range.Text = F.seqNum;
                          objTable.Cell(r, 5).Range.Text = F.PtrmCode;
                          objTable.Cell(r, 6).Range.Text = F.CRN;
                          objTable.Cell(r, 7).Range.Text = F.courseTitle;
                          //dual fields 
                          objTable.Cell(r, 8).Range.Text = "";
                          objTable.Cell(r, 9).Range.Text = "";
                          objTable.Cell(r, 10).Range.Text = "";
                          objTable.Cell(r, 11).Range.Text = F.CourseHours;
                      }
                      else
                      {

                          Debug.WriteLine("BREAK");
                          Debug.WriteLine(F.subjCode + " " + F.CRN);

                          objTable.Cell(r, 1).Range.Text = F.subjCode;
                          objTable.Cell(r, 2).Range.Text = F.sessCode;
                          objTable.Cell(r, 3).Range.Text = F.courseNum;
                          objTable.Cell(r, 4).Range.Text = F.seqNum;
                          objTable.Cell(r, 5).Range.Text = F.PtrmCode;
                          objTable.Cell(r, 6).Range.Text = F.CRN;
                          objTable.Cell(r, 7).Range.Text = F.courseTitle;
                          //dual fields 
                          objTable.Cell(r, 8).Range.Text = "";
                          objTable.Cell(r, 9).Range.Text = "";
                          objTable.Cell(r, 10).Range.Text = "";
                          objTable.Cell(r, 11).Range.Text = F.CourseHours;

                      }
                  }

                  PreviousSubCode = F.subjCode;
                          
                  }


1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Imran try something like this:
    PowerShell
    int r = 1;
    foreach (FacultyElements F in db._DataHolder)
    {
        if (F.subjCode == "BREAK")
        {
            objTable.Rows.Add();
            objTable.Rows[r].Range.InsertBreak(WdBreakType.wdPageBreak);
            objTable = document.Tables.Add(Range: para.Range, NumRows: 1, NumColumns: 11);
            r = 1;
        }
     
        if (r != 1)
            objTable.Rows.Add();
     
        objTable.Cell(r, 1).Range.Text = F.subjCode;
        objTable.Cell(r, 2).Range.Text = F.sessCode;
        objTable.Cell(r, 3).Range.Text = F.courseNum;
        objTable.Cell(r, 4).Range.Text = F.seqNum;
        objTable.Cell(r, 5).Range.Text = F.PtrmCode;
        objTable.Cell(r, 6).Range.Text = F.CRN;
        objTable.Cell(r, 7).Range.Text = F.courseTitle;
        objTable.Cell(r, 8).Range.Text = "";
        objTable.Cell(r, 9).Range.Text = "";
        objTable.Cell(r, 10).Range.Text = "";
        objTable.Cell(r, 11).Range.Text = F.CourseHours;
     
        r++;
    }
     
    string file = Path.Combine(
                    Environment.GetFolderPath(
                        Environment.SpecialFolder.Desktop),
                    "Sample.docx");
    document.SaveAs2(file);
    document.Close();
    application.Quit();
    System.Runtime.InteropServices.Marshal.ReleaseComObject(application);
    I hope that helps.
    Also if interested I would advise you to take a look at this alternative approach, the code uses this word component in C#:
    Java
    Section section = document.Sections[0];
    foreach (FacultyElements F in db._DataHolder)
    {
        if (F.subjCode == "BREAK")
        {
            section.Blocks.Add(
                new Paragraph(document,
                    new SpecialCharacter(document, SpecialCharacterType.PageBreak)));
     
            table = new Table(document);
            section.Blocks.Add(table);
        }
                    
        table.Rows.Add(
            new TableRow(document,
                new TableCell(document,
                    new Paragraph(document, F.subjCode)),
                new TableCell(document,
                    new Paragraph(document, F.courseNum)),
                new TableCell(document,
                    new Paragraph(document, F.seqNum)),
                new TableCell(document,
                    new Paragraph(document, F.PtrmCode)),
                new TableCell(document,
                    new Paragraph(document, F.CRN)),
                new TableCell(document,
                    new Paragraph(document, F.courseTitle)),
                new TableCell(document,
                    new Paragraph(document, string.Empty)),
                new TableCell(document,
                    new Paragraph(document, string.Empty)),
                new TableCell(document,
                    new Paragraph(document, string.Empty)),
                new TableCell(document,
                    new Paragraph(document, F.CourseHours))));
    }
     
    document.Save(
        Path.Combine(
            Environment.GetFolderPath(
                Environment.SpecialFolder.Desktop),
            "Sample.docx"));
    Regards,
    Lorine
    0