• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1023
  • Last Modified:

Automating MSWord and columns

I have been creating an Asp.Net app to automate the creation of a company directory from our Active Directory. Everything is working, I just have one last tweak to make that I can't figure out.

All the information is exported to a word template which has two columns. The information starts with the department name, it's address, phone and lastly the fax#. After this "header" section the script pulls the users from Active Directory and places them under the header section. At times this will cause spill-over from one column to the next. What I am trying to do is not allow the header section to spill over to the next column and get split into 2 pieces. I want the entire header of each section to be shown either at the bottom of one column or the top of the next.

I have included my code. If anyone can assist me with this task I would greatly appreciate it. - Thanks.

 For Each node In xmlNodeList
'Start of header section
            oWord.Selection.Font.Size = 10
            oWord.Selection.Font.Bold = True
            oWord.Selection.TypeText(ProgramName) 'Adds the Program Name
            oWord.Selection.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdLineBreak)
            oWord.Selection.Font.Bold = False
            oWord.Selection.TypeText(node.ChildNodes.Item(0).InnerText) 'Adds the address
            oWord.Selection.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdLineBreak)
            oWord.Selection.TypeText(node.ChildNodes.Item(1).InnerText) 'Adds City and State
            oWord.Selection.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdLineBreak)
            oWord.Selection.TypeText("Office: " & node.ChildNodes.Item(2).InnerText) 'Adds the Office number
            oWord.Selection.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdLineBreak)
            ' oWord.Selection.Font.Underline = True
            oWord.Selection.TypeText("Fax: " & node.ChildNodes.Item(3).InnerText) 'Adds the fax number
            oWord.Selection.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdLineBreak)
'End of header section
'Code to pull users from AD goes here
Next

Open in new window

0
mascoloj
Asked:
mascoloj
1 Solution
 
irudykCommented:
Applying the keep together property on the paragraph might be the answer. Although at the end of the header paragraph you would want to start a new paragraph for the user data (i.e. so the header and user data is not all kept together via the line breaks you are entering).  You'd also want to start the next header in its own paragraph to avoid having the previous user data in the same paragraph.  For example:
 For Each node In xmlNodeList
'Start of header section
            oWord.Selection.ParagraphFormat.KeepTogether = True
            oWord.Selection.Font.Size = 10
            oWord.Selection.Font.Bold = True
            oWord.Selection.TypeText(ProgramName) 'Adds the Program Name
            oWord.Selection.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdLineBreak)
            oWord.Selection.Font.Bold = False
            oWord.Selection.TypeText(node.ChildNodes.Item(0).InnerText) 'Adds the address
            oWord.Selection.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdLineBreak)
            oWord.Selection.TypeText(node.ChildNodes.Item(1).InnerText) 'Adds City and State
            oWord.Selection.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdLineBreak)
            oWord.Selection.TypeText("Office: " & node.ChildNodes.Item(2).InnerText) 'Adds the Office number
            oWord.Selection.InsertBreak(Microsoft.Office.Interop.Word.WdBreakType.wdLineBreak)
            ' oWord.Selection.Font.Underline = True
            oWord.Selection.TypeText("Fax: " & node.ChildNodes.Item(3).InnerText) 'Adds the fax number
            oWord.Selection.TypeParagraph
'End of header section
'Code to pull users from AD goes here

            'after AD user info entered, start the next header in its own paragraph 
            oWord.Selection.TypeParagraph

Next

Open in new window

0
 
mascolojAuthor Commented:
Thanks! I didn't even know that method existed. Works perfectly.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now