MS Word 2003 VBA 删除图形对象

发布于 2024-10-06 05:30:42 字数 325 浏览 3 评论 0原文

我有一个宏,可以为不同产品的列表生成案例标签。有些产品是无菌的,需要在标签上画一个圆圈作为辐射指示点的位置。圆圈内还有一个文本框,将圆圈标记为点的位置。我尝试通过插入一个圆圈的自动形状并将其设为书签,然后使用代码:

ThisDocument.Bookmarks("GammaDot").Range.Delete 

删除所有非无菌部分上的圆圈来做到这一点。此代码用于删除圆圈内文本框中的文本,但圆圈本身不会被删除。 文本框本身似乎也没有被删除,只是框内的文本被删除。 bookmarks.Delete 命令对实际对象不起作用吗?如果没有,我将如何删除圆圈和文本框? 谢谢

I have a macro that generates a case label for a list of different products. Some of the products are sterile and require a drawing of a circle to be placed on the label as a location for a radiation indicator dot. There is also a text box inside the circle that labels the circle as the location for the dot. I tried to do this by inserting an autoshape of a circle and making it a bookmark and then using the code:

ThisDocument.Bookmarks("GammaDot").Range.Delete 

to delete the circle on all the parts that aren't sterile. This code works to delete the text from the text box inside the circle, but the circle itself doesn't get deleted.
It also seems that the text box itself isn't getting deleted, just the text inside the box. Does the bookmarks.Delete command not work on actual obects? and if it doesn't, how would I go about deleting the circle and text box?
Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

硪扪都還晓 2024-10-13 05:30:42

我怀疑您必须按名称或循环删除形状。书签父级返回文档,而不是文本框。这将删除文本框和圆圈:

For i = ThisDocument.Shapes.Count To 1 Step -1
    ''Debug.Print ThisDocument.Shapes(1).Name
    ThisDocument.Shapes(i).Delete
Next

I suspect you will have to either delete the shapes by name or in a loop. The bookmark parent returns the document, not the textbox. This will delete both the textbox and the circle:

For i = ThisDocument.Shapes.Count To 1 Step -1
    ''Debug.Print ThisDocument.Shapes(1).Name
    ThisDocument.Shapes(i).Delete
Next
吻泪 2024-10-13 05:30:42

您可以使用书签 Range 的 ShapeRange 属性来获取属于书签的形状,并使用其 TextFrame 来获取形状的文本:

Dim bkmk As Bookmark
Set bkmk = ActiveDocument.Bookmarks("circle")
Dim shp As Shape
Set shp = bkmk.Range.ShapeRange.Item(1)
Debug.Print shp.TextFrame.TextRange.Text
shp.Delete

删除形状也会删除包含的文本。

You can get hold of the shapes belonging to a bookmark using the ShapeRange property of the bookmark's Range, and the shape's text using its TextFrame:

Dim bkmk As Bookmark
Set bkmk = ActiveDocument.Bookmarks("circle")
Dim shp As Shape
Set shp = bkmk.Range.ShapeRange.Item(1)
Debug.Print shp.TextFrame.TextRange.Text
shp.Delete

Deleting the shape will also remove the contained text.

彼岸花ソ最美的依靠 2024-10-13 05:30:42

您可以通过在 vba 编辑器中运行以下代码来删除除文本及其格式之外的所有内容:

Sub DeleteAllExceptText()

Dim i As Integer

With ActiveDocument
    For i = .Tables.Count To 1 Step -1
        .Tables(i).Delete
    Next i
End With

Dim j As Integer

With ActiveDocument
    For j = .Shapes.Count To 1 Step -1
        .Shapes(j).Delete
    Next j

Dim k As Integer

With ActiveDocument
    For k = .InlineShapes.Count To 1 Step -1
        .InlineShapes(k).Delete
    Next k

End Sub

You can delete everything except text and its formatting by running following code in vba editor:

Sub DeleteAllExceptText()

Dim i As Integer

With ActiveDocument
    For i = .Tables.Count To 1 Step -1
        .Tables(i).Delete
    Next i
End With

Dim j As Integer

With ActiveDocument
    For j = .Shapes.Count To 1 Step -1
        .Shapes(j).Delete
    Next j

Dim k As Integer

With ActiveDocument
    For k = .InlineShapes.Count To 1 Step -1
        .InlineShapes(k).Delete
    Next k

End Sub

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文