MS Word VBA Macro 移除文本形状后面的内容。

问题描述 投票:0回答:1

我是一个新的VBA宏,并试图删除MS文档中的水印,因为公司要使用新的水印。因此,我试图用宏删除文本背后的shapesimages,但宏不工作。任何帮助是非常感激的。

enter image description here

VBA代码。

For Each s In ActiveDocument.Shapes
    s.Select
    If Selection.ShapeRange.WrapFormat.Type = wdWrapBehind Then s.Delete
   Next
vba ms-word word-vba
1个回答
2
投票

Word所谓的水印并不是在文档正文中格式化为 "文本后面 "的形状。相反,它们是文档标题中的形状对象。没有简单的方法来区分文档头中的水印和其他可能存在的形状对象,因为水印可以由各种不同的形状类型组成。有了这个注意事项,可以尝试使用下面的宏来删除使用文本效果的内置水印。

Sub Demo()
Application.ScreenUpdating = False
Dim HdFt As HeaderFooter, Shp As Shape
For Each HdFt In ActiveDocument.Sections.First.Headers
  For Each Shp In HdFt.Shapes
    If Shp.Type = msoTextEffect Then
      Shp.Delete
    End If
  Next
Next
Application.ScreenUpdating = True
End Sub

0
投票

你的代码对我来说很好,它删除了我所有包裹在文字后面的图片。你确定水印真的是一个 形状物 Ms字有一个 内置 函数的水印。

如果你想通过编程方式去除它,已经有了一个叫做 可用代码 (这个上增加了保护,所以你要修饰一下)。它比只有形状的复杂,因为它是一个新的形状。单词构件对象.

简单的说,这里有一些代码,让你知道如果是水印对象该怎么做。你可以数出头形来删除所有对应的索引,或者你会知道所有头形的名称列表,其中Word随机给出的名称。(我用宏记录器快速回答了一些代码中不完全有用的地方,主要是所有的.Select方法)

 ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader 'header mode
 ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes(1).Select 'Select the first shape from headers (possibly a watermark)

 ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument 'normal mode
    ActiveDocument.Sections(1).Range.Select 'selects section 1
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    Selection.HeaderFooter.Shapes.AddTextEffect(PowerPlusWaterMarkObject44576390, "CUSTOM WATERMARK", "Calibri", 1, False, False, 0, 0).Select 'Create a custom watermark
    Selection.ShapeRange.Name = "PowerPlusWaterMarkObject44576390" 'weird name created automatically by MS Word

    ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes(1).Delete 'delete header shape by index ".Shapes(1).Delete"
    Selection.HeaderFooter.Shapes("PowerPlusWaterMarkObject44576390").Delete 'deleting knowing the name

请记住,如果水印是其他对象,这可能就不适用了。

希望你觉得有用

© www.soinside.com 2019 - 2024. All rights reserved.