vb.net code to delete watermarks in header

diana
3StarLounger
Posts: 279
Joined: 01 Jun 2010, 00:27

vb.net code to delete watermarks in header

Post by diana »

hello

there can be multiple watermarks in the header

i have the following vb.net code to delete watermarks.

Whats occuring is; it doesnt delete the specific named watermark. its deleting all/any watermarks.

do i need to define/set my shapes as a specifc range to work with to ensure I delete the correct watermark?

note:
in vba when i step /debug through through the code the correct behaviour occurs and deletes only the specified watermark name.

in vb.net when I run it the result is deletes random/ multiple watermarks in the header



Code: Select all

    

           Dim i As Integer = Nothing
            Dim j As Integer = 0
            Dim shpname As String = Nothing
            Dim HdFt As Microsoft.Office.Interop.Word.HeaderFooter
            Dim WordDocument As MSWord.Range = worddoc.Document.Range


            'With ActiveDocument
            With WordDocument
                For i = 1 To .Sections.Count
                    For Each HdFt In .Sections(i).Headers
                        With HdFt
                            For j = HdFt.Shapes.Count To 1 Step -1
                                shpname = .Shapes(j).AlternativeText

                                If WATERMARK_STATUS = "CLEAR" Then
                                    .Shapes(j).Delete()
                                End If

                                If InStr(shpname, FirstPageWatermark) > 0 Or InStr(shpname, SecondPageWatermark) > 0 Then
                                    'MsgBox(shpname)
                                    .Shapes(j).Delete()
                                End If
                            Next j
                        End With
                    Next
                Next
            End With



many thanks

dd

User avatar
HansV
Administrator
Posts: 78507
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: vb.net code to delete watermarks in header

Post by HansV »

The lines

Code: Select all

                                If WATERMARK_STATUS = "CLEAR" Then
                                    .Shapes(j).Delete()
                                End If
appear to use a string variable WATERMARK_STATUS that is neither declared nor assigned in the code fragment that you posted. If it happens to be set to "CLEAR", each and any shape in the header/footer will be deleted. Otherwise, these three lines won't do anything.
Since you want to delete specific shapes only, I think you should remove or comment out these three lines. Does that help?
Best wishes,
Hans

diana
3StarLounger
Posts: 279
Joined: 01 Jun 2010, 00:27

Re: vb.net code to delete watermarks in header

Post by diana »

Thanks Hans

the variable WATERMARK_STATUS is a global variable passed through this sub. sorry I omitted Ive re-attached the full code in this post.

yes ive been worked on the code this afternoon and worked out the issue.

with the first version of code, the instr variable names were abit "general"

update - Ive redefined the variable names to check exact names and this works, and deletes the specific watermark.

ive attached the working code

thanks again Hans

dd

Code: Select all


 Public Sub DeleteWatermark(ByVal worddoc As WordDocument, ByVal WATERMARK_STATUS As String, ByVal FirstPageWatermark As String, ByVal SecondPageWatermark As String)

            Dim i As Integer = 0
            Dim j As Integer = 0
            Dim shpname As String = Nothing
            Dim HdFt As Microsoft.Office.Interop.Word.HeaderFooter
            Dim WordDocument As MSWord.Range = worddoc.Document.Range
            Dim strFirstPageWatermark As String = FirstPageWatermark & "_CORRS"
            Dim strSecondPageWatermark As String = SecondPageWatermark & "_CORRS"



                'for each header 
                With WordDocument
                    For i = 1 To .Sections.Count
                        For Each HdFt In .Sections(i).Headers
                            With HdFt
                                For j = HdFt.Shapes.Count To 1 Step -1
                                    shpname = .Shapes(j).AlternativeText

                                    If WATERMARK_STATUS = "CLEAR" Then
                                        .Shapes(j).Delete()
                                    End If

                                    If (shpname = strFirstPageWatermark) Or (shpname = strSecondPageWatermark) Then
                                        .Shapes(j).Delete()
                                    End If
                                Next j
                            End With
                        Next
                    Next
                End With

end sub


User avatar
HansV
Administrator
Posts: 78507
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: vb.net code to delete watermarks in header

Post by HansV »

Be aware that if you pass "CLEAR" as WATERMARK_STATUS when you call DeleteWaterMark, you'll delete ALL watermarks, regardless of their alternative text.
Best wishes,
Hans