Reputation: 11
I'm trying to create a word doc Footer programmatically in vb.net that has 3 lines or more with 3 different font colors and font styles. I can only seem to get the first color and style to ‘stick’ the other 2 never change. I am using Microsoft.Office.Interop.Word and am fairly new to coding with it. I would rather not use a macro.
The Footer should not be visible on the 1st page. The 2nd page should consist of only 2 of the lines and the all other will have all 3 lines. The third line will contain the Page numbers in the format of (Page number X of XX).
Here is what I have so far:
For Each section As Word.Section In moApp.Sections
Dim footer As Microsoft.Office.Interop.Word.HeaderFooter = section.Footers(Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary)
Dim footerRange As Word.Range = section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
footerRange.Font.ColorIndex = Word.WdColorIndex.wdGray50
footerRange.Font.Size = 12
footerRange.Font.Name = "Calibri"
footerRange.Text = "This is Line # 1"
footerRange.Font.ColorIndex = Word.WdColorIndex.wdBlue
footerRange.Font.Size = 10
footerRange.Font.Name = "Calibri"
footerRange.Text = "This is Line # 2"
footerRange.Font.ColorIndex = Word.WdColorIndex.wdRed
footerRange.Font.Size = 8
footerRange.Font.Name = "Calibri"
footerRange.Text = "This is Line # 3 that will include the page numbers"
Next
When I run this my results display All 3 lines with the same font and style in the Footer. I need them a 3 different ones. Any help is appreciated!
Upvotes: 1
Views: 56
Reputation: 468
Your problem is that you are assigning values to properties, and then you are overriding them with other values, resulting in only the last value written being displayed.
Here is a simpler example:
Dim x As Integer = 0
x = 1
x = 2
Despite all the other values assigned to x, only the last one assigned (the x = 2
) will have any effect on the final value. x
is going to always be 2
after those three lines are run because that was the last value assigned. Just because you might run those lines more than once doesn't change that.
So, to bring this back to your problem, I'm going to subdivide part of the inside of your For Each
loop into three sections:
footerRange.Font.ColorIndex = Word.WdColorIndex.wdGray50
footerRange.Font.Size = 12
footerRange.Font.Name = "Calibri"
footerRange.Text = "This is Line # 1"
footerRange.Font.ColorIndex = Word.WdColorIndex.wdBlue
footerRange.Font.Size = 10
footerRange.Font.Name = "Calibri"
footerRange.Text = "This is Line # 2"
footerRange.Font.ColorIndex = Word.WdColorIndex.wdRed
footerRange.Font.Size = 8
footerRange.Font.Name = "Calibri"
footerRange.Text = "This is Line # 3 that will include the page numbers"
These sections are run consecutively in the order they are ordered in. So, similar to the previous example, each one of these will have one value assigned to it, and then a second value assigned to it, and then a third value which will be the value it ends with. Thus, the result of the combination of those three sections is the same as the result of the last without the two previous:
footerRange.Font.ColorIndex = Word.WdColorIndex.wdRed
footerRange.Font.Size = 8
footerRange.Font.Name = "Calibri"
footerRange.Text = "This is Line # 3 that will include the page numbers"
I'm guessing what you wanted to do would be something of this sort:
Dim sectionIdx As Integer = 0
For Each section As Word.Section In moApp.Sections
Dim footer As Microsoft.Office.Interop.Word.HeaderFooter = section.Footers(Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary)
Dim footerRange As Word.Range = section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
If sectionIdx = 0 Then
footerRange.Font.ColorIndex = Word.WdColorIndex.wdGray50
footerRange.Font.Size = 12
footerRange.Font.Name = "Calibri"
footerRange.Text = "This is Line # 1"
ElseIf sectionIdx = 1 Then
footerRange.Font.ColorIndex = Word.WdColorIndex.wdBlue
footerRange.Font.Size = 10
footerRange.Font.Name = "Calibri"
footerRange.Text = "This is Line # 2"
ElseIf sectionIdx = 2 Then
footerRange.Font.ColorIndex = Word.WdColorIndex.wdRed
footerRange.Font.Size = 8
footerRange.Font.Name = "Calibri"
footerRange.Text = "This is Line # 3 that will include the page numbers"
End If
sectionIdx += 1
Next
I haven't tested in nor have I worked with VB.NET in years, so that is just my guess. The above may or may not work as intended. However, even if it doesn't, it doesn't invalidate the reason your code doesn't work, which I am much more certain of. So, even if it doesn't, you can probably find the solution on your own given what I have given you that is correct.
Bear in mind, even if it does function as intended, the above code will only work for the first three iterations of the For Each
loop, so you'll probably have to do some string concatenation directly with sectionIdx
if you want to be able to do all pages. I only made it only do the first three because that was what your original code seemed to be trying to do.
Upvotes: 1