question

EugeneGough-2857 avatar image
0 Votes"
EugeneGough-2857 asked EugeneGough-2857 commented

Using VB.NET, How to add a header and then add Left, Center and Right data to the header

I can add a single item but any attempt to add additional results in the last item being the only item.

For Each section As Word.Section In oDoc.Sections
Dim headerRange As Word.Range = section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldDate)
headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
headerRange.Text = " Librarian "
headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphcenter
headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage)
headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
Next

In above example, the header results with the page number in the header at far right. I find the documentation and examples for VB.Net word interop to be incomplete hard to find. One would think that it would be a simple matter to add more than one item to a header.

dotnet-visual-basic
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@EugeneGough-2857

As the tag "office-word-itpro" focuses more on general issues of Word client, I would remove the tag.
Thanks for your understandings.

0 Votes 0 ·

1 Answer

JohnKorchok avatar image
0 Votes"
JohnKorchok answered EugeneGough-2857 commented

I imagine you're thinking of Excel, which has separate header sections for left, center and right alignment. Word doesn't have that. So each new line of code simply over-writes the last text the you inserted, and you end up with only the last insertion.

Instead, you would have to create a 3-cell table in the header, set the alignment of each cell, then place the text in the relevant cell.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks. I had a feeling that it would be something like that. I have also been trying to just make the wdFieldPage and pages function store values that I can then format into "Page x of y" and manually place that into the header. Have not been able to do that either. There must be an easier way to build a simple header that would look like below with Date left, title center and Page right. As easy as it is in excel, one would expect at least reasonable way to accomplish the same in Word with VB.Net.

Date Title Page 1 of 5

I am going to try the fields.add method and see if I can build the format above.









0 Votes 0 ·

Thanks. I commented on you answer but it disappeared. I am going to play with the fields.add method to build what I want.

0 Votes 0 ·