【发布时间】:2015-03-12 15:37:01
【问题描述】:
我正在使用以下代码来选择和删除文档中的所有自定形线条。
它在 MSWord 2003 中运行良好。(也适用于在 2007 年打开时使用 2003 绘制的线条) 但它没有选择在 MS word 2007 中绘制的线条。
Sub line()
Dim shp As Shape, intBoxNbr As Integer
intNbrShapes = 0
For Each shp In ActiveDocument.Shapes
If shp.Type = msoLine Then
intNbrShapes = intNbrShapes + 1
ActiveWindow.ScrollIntoView Selection.Range, True
shp.Select
Selection.Delete
'shp.Delete
'(Selection.Delete is used in MSWord 2007 and shp.Delete used in MSWord 2003)
End If
Next shp
End Sub
我发现在 MSword 2007 中绘制的线条名称为 Autoshape##,其中 2003 具有线条##。 我在文档中有其他自动形状(文本框等),所以我不能只使用“If shp.Type = msoAutoShape Then”。 请帮助如何选择和删除使用 MS Word 2007 绘制的线。
谢谢。
我现在更新了代码...它不会一次删除所有行。我需要多次运行宏才能全部删除。
Sub Macro1()
'
' Macro1 Macro
'
Dim shp As Shape, i As Integer
i = 0
For Each shp In ActiveDocument.Shapes
If shp.Type = msoAutoShape Then
i = i + 1
ActiveWindow.ScrollIntoView Selection.Range, True
shp.Select
If Selection.ShapeRange.Line.DashStyle = msoLineSolid Then
Selection.Delete
End If
'shp.Delete
End If
Next shp
End Sub
【问题讨论】:
-
你做了什么调试?在
For Each shp语句上放置一个断点,并检查shp.Type属性中是否存在已知的“行”。它可能不是msoLine类型,那么显然会“失败”选择/删除这些形状。 -
你能简单介绍一下吗?
-
我没有 2003 或 2007 可用于测试这些条件,并且在 2010 中,形状默认名称与您描述的不同(相反,我得到“直连接器 1”等。 )。因此,您应该进行一些调试并尝试修改各种属性以查看可能的情况。
-
当你把代码放在 cmets 中时,为什么人们不明白代码绝对难以辨认?
-
从集合中删除项目时,总是需要向后遍历集合。