简体   繁体   English

使用 Excel VBA 删除 Word 文档中的水印

[英]Deleting Watermark in Word Document using Excel VBA

I am trying to use excel to delete a watermark in all areas of a word document.我正在尝试使用excel删除word文档所有区域的水印。 I was able to do so with VB in Word, and I have translated that code over to excel to reach out to word to complete.我能够在 Word 中使用 VB 完成此操作,并且我已将该代码转换为 excel 以使用 Word 来完成。 There are no errors, but the code is not able to delete or remove the watermark.没有错误,但代码无法删除或去除水印。 I believe there must be some additional link to the shape or location of it that I need to reference, but I don't know what it would be missing.我相信必须有一些额外的链接到我需要参考的形状或位置,但我不知道它会丢失什么。

The working code in Word VBA used spShape.Visible = False which did not do anything in excel, and I have tried spShape.Delete to no avail either. Word VBA 中的工作代码使用 spShape.Visible = False 在 excel 中没有做任何事情,我也尝试过 spShape.Delete 也无济于事。

Any Help is appreciated, here is my code:任何帮助表示赞赏,这是我的代码:

Sub AddRemoveWatermark()
'Word Variables
Dim wrdApplication As Word.Application
Dim wrdDocument As Word.Document
Dim wrdSection As Word.section
Dim wrdHeader As Word.HeaderFooter
Dim rngHeader As Word.Range
Dim spShape As Word.Shape

Dim strDocumentName As String
Dim strPath As String
Dim strBBPath As String
Dim lngCount As Long
Dim pHeaderType As Long
Dim strShapeName As String

' Open the file dialog
With Application.FileDialog(msoFileDialogOpen)
    .AllowMultiSelect = True
    .Show

    Set wrdApplication = New Word.Application

    ' Display paths of each file selected
    For lngCount = 1 To .SelectedItems.Count
        strPath = .SelectedItems(lngCount)
        Set wrdDocument = wrdApplication.Documents.Open(strPath)

        strDocumentName = wrdDocument.FullName 'Record the document name
        wrdApplication.Templates.LoadBuildingBlocks

        wrdApplication.Visible = True

            'Address each section
            For Each wrdSection In wrdDocument.Sections
                With wrdSection
                    Set rngHeader = .Headers(wdHeaderFooterFirstPage).Range
                    For Each spShape In rngHeader.ShapeRange
                        strShapeName = spShape.Name

                        If InStr(strShapeName, "PowerPlusWaterMarkObject") > 0 Then
                            'spShape.Delete
                            spShape.Visible = msoFalse
                        End If
                    Next

                    Set rngHeader = .Headers(wdHeaderFooterPrimary).Range

                    For Each spShape In rngHeader.ShapeRange
                        strShapeName = spShape.Name

                        If InStr(strShapeName, "PowerPlusWaterMarkObject") > 0 Then
                            'spShape.Delete
                            spShape.Visible = msoFalse
                        End If
                    Next

                End With
            Next wrdSection

            wrdDocument.SaveAs (wrdDocument.FullName)

        wrdDocument.Close
    Next lngCount
End With

wrdApplication.Quit

End Sub

Hey is my code for removing watermark, not so elegant though.嘿是我的去除水印的代码,虽然不是那么优雅。

Sub RemoveWordArtWaterMark()
'on error resume next
On Error Resume Next
'from google group
Dim x           As Long
For x = 1 To 20
    ActiveDocument.sections(1).Headers(wdHeaderFooterFirstPage).Shapes(1).Delete
Next x

End Sub结束子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM