none
insert different watermarks into a whole document with different page headers RRS feed

  • Question

  • in vb.net, add in i want to add watermarks created as building blocks to every page of the document

    different watermarks are required for different first page headers and primary headers

    ive searched the web however im just getting confused.

    ive code in vb which works in vb, however when i convert the code into vb.net it doesnt work.

     how do I do this?

    dd



    • Edited by dd_ Thursday, September 22, 2011 5:51 AM
    Thursday, September 22, 2011 4:57 AM

Answers

  • hello all

    I worked this out.  Ive attached the working vb.net code to assist someone else if they have the same issue.

    note: i may have got the primay and odd/even headers around the wrong way - no big thing

    many thanks again all

    dd

     

                Dim HeaderFooter As Microsoft.Office.Interop.Word.HeaderFooter
    
                Dim i As Integer = 0
    
                Dim iHeaderType As Integer = 0
    
                Dim WordDocument As MSWord.Range = worddoc.Document.Range
    
                Dim oRange As MSWord.Range
    
    
    
                    With WordDocument
    
                        For i = 1 To .Sections.Count
    
                            iHeaderType = 0
    
    
    
                            For Each HeaderFooter In .Sections(i).Headers
    
    
    
                                iHeaderType = iHeaderType + 1
    
    
    
                                With HeaderFooter
    
    
    
                                    If .LinkToPrevious = False Then
    
    
    
                                        'first page header
    
                                        If iHeaderType = 1 Then
    
                                            'If section.PageSetup.DifferentFirstPageHeaderFooter = -1 Then
    
                                            oRange = worddoc.Document.Sections(i).Headers(MSWord.WdHeaderFooterIndex.wdHeaderFooterFirstPage).Range
    
                                            oRange.Collapse(MSWord.WdCollapseDirection.wdCollapseStart)
    
                                            worddoc.InsertBuildingBlock(FirstPageWatermark, oRange)
    
                                            oRange = Nothing
    
                                        End If
    
    
    
                                        ' odd/evenheaders
    
                                        If iHeaderType = 2 Then
    
                                            oRange = worddoc.Document.Sections(i).Headers(MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages).Range
    
                                            oRange.Collapse(MSWord.WdCollapseDirection.wdCollapseStart)
    
                                            worddoc.InsertBuildingBlock(SecondPageWatermark, oRange)
    
                                            oRange = Nothing
    
                                        End If
    
    
    
                                        'primary headers
    
                                        If iHeaderType = 3 Then
    
                                            oRange = worddoc.Document.Sections(i).Headers(MSWord.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
    
                                            oRange.Collapse(MSWord.WdCollapseDirection.wdCollapseStart)
    
                                            worddoc.InsertBuildingBlock(SecondPageWatermark, oRange)
    
                                            oRange = Nothing
    
                                        End If
    
    
    
                                    End If
    
                                End With
    
                            Next
    
                        Next
    
                    End With
    
                End If
    
    
    
    

    Thursday, September 22, 2011 11:41 PM

All replies

  • Please show us the code that works in vb(a?) but isn't working in VB.NET, and show us the VB.NET code, as well. Please remember to use the "code" button on the forum toolbar when pasting your code.

    Which version of Word is involved?


    Cindy Meister, VSTO/Word MVP
    Thursday, September 22, 2011 5:54 AM
    Moderator
  • hello all

    I worked this out.  Ive attached the working vb.net code to assist someone else if they have the same issue.

    note: i may have got the primay and odd/even headers around the wrong way - no big thing

    many thanks again all

    dd

     

                Dim HeaderFooter As Microsoft.Office.Interop.Word.HeaderFooter
    
                Dim i As Integer = 0
    
                Dim iHeaderType As Integer = 0
    
                Dim WordDocument As MSWord.Range = worddoc.Document.Range
    
                Dim oRange As MSWord.Range
    
    
    
                    With WordDocument
    
                        For i = 1 To .Sections.Count
    
                            iHeaderType = 0
    
    
    
                            For Each HeaderFooter In .Sections(i).Headers
    
    
    
                                iHeaderType = iHeaderType + 1
    
    
    
                                With HeaderFooter
    
    
    
                                    If .LinkToPrevious = False Then
    
    
    
                                        'first page header
    
                                        If iHeaderType = 1 Then
    
                                            'If section.PageSetup.DifferentFirstPageHeaderFooter = -1 Then
    
                                            oRange = worddoc.Document.Sections(i).Headers(MSWord.WdHeaderFooterIndex.wdHeaderFooterFirstPage).Range
    
                                            oRange.Collapse(MSWord.WdCollapseDirection.wdCollapseStart)
    
                                            worddoc.InsertBuildingBlock(FirstPageWatermark, oRange)
    
                                            oRange = Nothing
    
                                        End If
    
    
    
                                        ' odd/evenheaders
    
                                        If iHeaderType = 2 Then
    
                                            oRange = worddoc.Document.Sections(i).Headers(MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages).Range
    
                                            oRange.Collapse(MSWord.WdCollapseDirection.wdCollapseStart)
    
                                            worddoc.InsertBuildingBlock(SecondPageWatermark, oRange)
    
                                            oRange = Nothing
    
                                        End If
    
    
    
                                        'primary headers
    
                                        If iHeaderType = 3 Then
    
                                            oRange = worddoc.Document.Sections(i).Headers(MSWord.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
    
                                            oRange.Collapse(MSWord.WdCollapseDirection.wdCollapseStart)
    
                                            worddoc.InsertBuildingBlock(SecondPageWatermark, oRange)
    
                                            oRange = Nothing
    
                                        End If
    
    
    
                                    End If
    
                                End With
    
                            Next
    
                        Next
    
                    End With
    
                End If
    
    
    
    

    Thursday, September 22, 2011 11:41 PM