none
Can not retrieve data in firstPageHeader RRS feed

  • Question

  • Hi,

    I am trying to update first page header in existing document. Once I open the document I am trying to read a firstPageHeader data as follows. I add "aaaaaaa" and added page number in my first page header before I run this code

    Word.ActiveDocument.Sections(1).PageSetup.DifferentFirstPageHeaderFooter = True

    Word.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdPrintView

    Word.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekFirtstPageHeader

    MyRange = Word.ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range

    MyRange.Text is always " " even my document has some data in the first page header I also checked Paragrahs of the MyRange but it has only one paragraphs with  " "

    Wednesday, October 3, 2012 1:24 PM

Answers

  • Hi BLT

    I've been following this discussion. Tom's code is VB.NET (in a VSTO document, actually), not VBA. What he's shown you should work and if it does not, then I, too, would suspect a problem in the document.

    But it's impossible for us to determine that without being able to inspect exactly what you have.

    I can think of no other suggestion or recommendation for you.

    I should think it would be possible for you to share a test document (not the code) where you're seeing this problem so that we can test it with our code and see if it behaves the same here, or whether things work correctly.

    If they work correctly, then it's a problem in your code. If we see the same problem you do, we can probably figure out what's wrong with your document, or with the logic of the situation.


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, October 6, 2012 3:47 PM
    Moderator

All replies

  • Hi BLT123,

    Thanks for posting in the MSDN Forum.

    Would you please try following snippet:

    Public Class ThisDocument
    
        Private Sub ThisDocument_Startup() Handles Me.Startup
            Dim wdDoc As Word.Document = Globals.ThisDocument.Application.ActiveDocument
            Dim wdFH As Word.HeaderFooter = wdDoc.Sections(1).Headers(Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage)
            Dim wdOH As Word.HeaderFooter = wdDoc.Sections(1).Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary)
            Dim wdEH As Word.HeaderFooter = wdDoc.Sections(1).Headers(Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages)
    
            MsgBox(wdFH.Range.Text)
            MsgBox(wdOH.Range.Text)
            MsgBox(wdEH.Range.Text)
        End Sub
    
        Private Sub ThisDocument_Shutdown() Handles Me.Shutdown
    
        End Sub
    
    End Class

    I hope it can help you.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, October 4, 2012 6:06 AM
    Moderator
  • hi Tom,

    I just ran above code, here is the results from my document has "aaaa" in the FirstPageHeader and "bbb" in the header in the second page

    MsgBox(wdFH.Range.text) = " "

    MsgBox(wdOH.Range.text) = "bbb"

    MsgBox(wdEH.Range.Text) = " "

    I still don't get the data from firstPageheader.

    BTW,  I am not using macro in word template, I am using word interop object from vb.net.

    Thanks for your suggestion.

    Thursday, October 4, 2012 2:21 PM
  • Hi BLT123,

    I'm wondering whether your have problem on your Word Document. Would you please share your Word document on the Skydrive do further research?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Friday, October 5, 2012 7:37 AM
    Moderator
  • Tom,

    It happens when I create new document and add some data in the first page header and trying to read it from program, I am not using Macro, I am using Microsoft.Office.Interop.Word.

    I really like to share my code since I am stuck for couple of days on this issue but my company policy does not allow to do that.

    Thanks for your help!

    Friday, October 5, 2012 1:07 PM
  • Hi BLT

    I've been following this discussion. Tom's code is VB.NET (in a VSTO document, actually), not VBA. What he's shown you should work and if it does not, then I, too, would suspect a problem in the document.

    But it's impossible for us to determine that without being able to inspect exactly what you have.

    I can think of no other suggestion or recommendation for you.

    I should think it would be possible for you to share a test document (not the code) where you're seeing this problem so that we can test it with our code and see if it behaves the same here, or whether things work correctly.

    If they work correctly, then it's a problem in your code. If we see the same problem you do, we can probably figure out what's wrong with your document, or with the logic of the situation.


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, October 6, 2012 3:47 PM
    Moderator
  • Cindy,

    Since this code is really long and not well organized, we decided to rewrite from scratch instead of patching here and there. It will give us more issues later. Our team decided to rewrite it. It was coupld thousand lines of code with not much comments. I will let you know how did it go.

    thanks!

    Wednesday, October 10, 2012 3:08 PM
  • Hi BLT123,

    Any update?

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Friday, October 12, 2012 5:29 AM
    Moderator
  • Hi BLT123,

    I will mark Cindy's reply as answer, please free feel to unmark it if you don't think so.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, October 16, 2012 7:10 AM
    Moderator