Solved

How do I add a table header to a Word document header using VB.net?

Posted on 2014-09-03
5
723 Views
Last Modified: 2016-11-24
I need to add the header of my Word table to the header of page 2 and above in a Word document.  I have it appearing on the first page but none there after.  I think I know where the problem is but I don't know how to fix it.  Any help is most welcomed!!

Here is the code.  I think the bolded area is where I need to put something but what?  

With oDoc
                oSec = .Sections(1)
                oSec.PageSetup.DifferentFirstPageHeaderFooter = True
                oSec.Range.InsertAfter("Appendix E: White Sands Test Facility Reporting Period:  " & trainee & vbCrLf & "Summary of Maximum Concentrations, Current Concentrations and T-C Plot Interpretations for WSTF Monitoring Well Network")

               oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, 1, 19)
               oTable.Range.ParagraphFormat.SpaceBefore = 3
                oTable.Range.ParagraphFormat.SpaceAfter = 3
                oTable.Borders.Enable = True
                oTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
                oTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
                oTable.Range.Font.Bold = False
                oTable.Range.Font.Size = 8
                oTable.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
                oTable.Range.Font.Color = Word.WdColor.wdColorBlack
                oTable.Range.Font.Italic = False
               
                'column headings
                oTable.Cell(1, 1).Range.Text = CStr("Well")
                oTable.Cell(1, 2).Range.Text = CStr("First" & vbCrLf & "Sample")
                oTable.Cell(1, 3).Range.Text = CStr("Interpretation")

                oTable.Cell(1, 4).Range.Text = CStr("F11" & vbCrLf & "Max")
                oTable.Cell(1, 5).Range.Text = CStr("F11" & vbCrLf & "Year")
                oTable.Cell(1, 6).Range.Text = CStr("F11" & vbCrLf & "Last")
                oTable.Cell(1, 7).Range.Text = CStr("F11" & vbCrLf & "Year")

                oTable.Cell(1, 8).Range.Text = CStr("F113" & vbCrLf & "Max")
                oTable.Cell(1, 9).Range.Text = CStr("F113" & vbCrLf & "Year")
                oTable.Cell(1, 10).Range.Text = CStr("F113" & vbCrLf & "Last")
                oTable.Cell(1, 11).Range.Text = CStr("F113" & vbCrLf & "Year")

                oTable.Cell(1, 12).Range.Text = CStr("TCE" & vbCrLf & "Max")
                oTable.Cell(1, 13).Range.Text = CStr("TCE" & vbCrLf & "Year")
                oTable.Cell(1, 14).Range.Text = CStr("TCE" & vbCrLf & "Last")
                oTable.Cell(1, 15).Range.Text = CStr("TCE" & vbCrLf & "Year")

                oTable.Cell(1, 16).Range.Text = CStr("NDMA" & vbCrLf & "(607)" & vbCrLf & "Max")
                oTable.Cell(1, 17).Range.Text = CStr("NDMA" & vbCrLf & "(607)" & vbCrLf & "Year")
                oTable.Cell(1, 18).Range.Text = CStr("NDMA" & vbCrLf & "(607)" & vbCrLf & "Last")
                oTable.Cell(1, 19).Range.Text = CStr("NDMA" & vbCrLf & "(607)" & vbCrLf & "Year")

                oTable.Rows(1).Shading.BackgroundPatternColor = Word.WdColor.wdColorGray15
                oTable.Columns.Item(1).Width = oWord.InchesToPoints(0.7) ' well
                oTable.Columns.Item(2).Width = oWord.InchesToPoints(0.75)
                oTable.Columns.Item(3).Width = oWord.InchesToPoints(0.8)

                oTable.Columns.Item(4).Width = oWord.InchesToPoints(0.6) '
                oTable.Columns.Item(5).Width = oWord.InchesToPoints(0.4)
                oTable.Columns.Item(6).Width = oWord.InchesToPoints(0.6)
                oTable.Columns.Item(7).Width = oWord.InchesToPoints(0.4)
                oTable.Columns.Item(8).Width = oWord.InchesToPoints(0.6)
                oTable.Columns.Item(9).Width = oWord.InchesToPoints(0.4)
                oTable.Columns.Item(10).Width = oWord.InchesToPoints(0.6)
                oTable.Columns.Item(11).Width = oWord.InchesToPoints(0.4)
                oTable.Columns.Item(12).Width = oWord.InchesToPoints(0.6)
                oTable.Columns.Item(13).Width = oWord.InchesToPoints(0.4)
                oTable.Columns.Item(14).Width = oWord.InchesToPoints(0.6)
                oTable.Columns.Item(15).Width = oWord.InchesToPoints(0.4)
                oTable.Columns.Item(16).Width = oWord.InchesToPoints(0.6)
                oTable.Columns.Item(17).Width = oWord.InchesToPoints(0.5)
                oTable.Columns.Item(18).Width = oWord.InchesToPoints(0.6)
                oTable.Columns.Item(19).Width = oWord.InchesToPoints(0.5)

                oSec.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Text = "Appendix E:  Summary of Maximum Concentrations, Current Concentrations and T-C Plot Interpretations for WSTF Monitoring Well Network" _
                   & vbCrLf ' ===== DO I PUT SOMETHING HERE??? ====

                '=====================================================

                oSec.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Text = "NASA White Sands Test Facility for Reporting Period:  " & trainee
            End With
0
Comment
Question by:Karen Wilson
  • 2
  • 2
5 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
Comment Utility
I might be wrong, but I think that you are confusing the table heading with the document header.

You can set the table heading like this:
MyTable.Rows(1).HeadingFormat = true

Open in new window

You could use the document header instead, but it won't look like part of the table. And anyway, in this case you specify that the first page header is different, so it won't be repeated on the  following pages.
0
 

Author Comment

by:Karen Wilson
Comment Utility
I am going to try your method as it makes more sense.  I was trying to duplicate how the report was given to me, which had the table header also as the document header.  Thanks!
0
 

Author Closing Comment

by:Karen Wilson
Comment Utility
Perfect!! Thanks so much!!

For those who want to know what I did, see below:

oTable = oDoc.Tables.Add(oDoc.Bookmarks.Item("\endofdoc").Range, hCount + 1, 19)
                    oTable.Range.ParagraphFormat.SpaceBefore = 3
                    oTable.Range.ParagraphFormat.SpaceAfter = 3
                    oTable.Borders.Enable = True
                    oTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
                    oTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
                    oTable.Range.Font.Bold = False
                    oTable.Range.Font.Size = 8
                    oTable.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
                    oTable.Range.Font.Color = Word.WdColor.wdColorBlack
                    oTable.Range.Font.Italic = False
                    oTable.Rows(1).HeadingFormat = True
0
 
LVL 76

Expert Comment

by:GrahamSkan
Comment Utility
Pleased to have got you on the right track. Thank you for the feedback.
0
 

Expert Comment

by:Ritesh Hegde
Comment Utility
Hello,

I would like to add two footers in a word document using VB.net script. How is this possible? Can any1 help me.?

Basically I need something which has a left side footer and right side footer. I think, we need to add 2 column table as footer but i am not sure how to do this.

Thanks in advance for help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now