使用 VBA 从标题中删除形状时 Word 崩溃
(免责声明:就职业而言,我不是 VBA 程序员)
附加到功能区中的按钮,我有代码可以在 Word 文档中切换公司徽标。 一个按钮用于徽标类型 A,第二个按钮用于徽标类型 B,第三个按钮用于无徽标(徽标是在纸上预印的)
首先,我使用 removeLogo
删除徽标,然后将其添加为请求的徽标使用setLogoAt
。
第一次单击按钮就可以了(例如,对于徽标类型 A),徽标将添加到文档的标题中。当我单击其他按钮(例如徽标类型 B)时,Word 崩溃(可能是在删除当前徽标时)
我的代码有什么问题(或者不太可能:使用 Word?)
Sub setLogoAt(left As Integer, path As String)
Dim logoShape As Shape
Dim anchorLocation As Range
Dim headerShapes As Shapes
Set logoShape = ActiveDocument. 'linebreks for readability
.Sections(1)
.Headers(wdHeaderFooterPrimary)
.Shapes
.AddPicture(FileName:=path, LinkToFile:=False,
SaveWithDocument:=True, left:=0,
Top:=0, Width:=100, Height:=80)
logoShape.name = "CompanyLogo"
logoShape.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
logoShape.RelativeVerticalPosition = wdRelativeVerticalPositionPage
logoShape.Top = CentimetersToPoints(0.1)
logoShape.left = CentimetersToPoints(left)
End Sub
Sub removeLogo()
Dim headerShapes As Shapes
Set headerShapes = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes
Dim shapeToDelete As Shape
If (headerShapes.Count > 0) Then
If Not IsNull(headerShapes("CompanyLogo")) Then
Set shapeToDelete = headerShapes("CompanyLogo")
End If
End If
If Not (shapeToDelete Is Nothing) Then
shapeToDelete.Delete
End If
End Sub
编辑
代码。一切都很好,直到我到达 removeLogo
中的 shapteToDelete.Delete
行。即使在调试时,Word 也会严重崩溃。我使用的是 Word 2007(这是一项要求)
edit2 我清除了所有宏、所有法线.dot、所有自动加载模板,然后使用上面的两个例程和此测试方法创建了一个新文档:
Sub test()
setLogoAt 5, "C:\path\to\logo.jpg"
removeLogo
setLogoAt 6, "C:\path\to\logo.jpg"
End Sub
当我运行 test
时,它在 removeLogo
中崩溃> 在 shapeToDelete.Delete
处。
编辑3 我“解决”了这个问题,首先使页眉/页脚查看Word中的活动视图,然后删除形状,然后返回到普通视图。很奇怪。它有效,但作为一名程序员我不高兴。
(disclaimer: i'm not a VBA programmer by occupation)
Attached to buttons in the Ribbon I have code to toggle the company logo in a Word Document.
One button for the logo type A, a second button for logo type B and a third for no logo (logo is preprintend on paper)
First I remove the logo with removeLogo
and then i add it the requested logo with setLogoAt
.
The first button click is fine (e.g. for Logo Type A), a logo is added to the header of the document. When i click an other button (e.g for Logo Type B) Word crashes (probably on removing the current logo)
What is wrong with my code (or less probably: with Word?)
Sub setLogoAt(left As Integer, path As String)
Dim logoShape As Shape
Dim anchorLocation As Range
Dim headerShapes As Shapes
Set logoShape = ActiveDocument. 'linebreks for readability
.Sections(1)
.Headers(wdHeaderFooterPrimary)
.Shapes
.AddPicture(FileName:=path, LinkToFile:=False,
SaveWithDocument:=True, left:=0,
Top:=0, Width:=100, Height:=80)
logoShape.name = "CompanyLogo"
logoShape.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
logoShape.RelativeVerticalPosition = wdRelativeVerticalPositionPage
logoShape.Top = CentimetersToPoints(0.1)
logoShape.left = CentimetersToPoints(left)
End Sub
Sub removeLogo()
Dim headerShapes As Shapes
Set headerShapes = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Shapes
Dim shapeToDelete As Shape
If (headerShapes.Count > 0) Then
If Not IsNull(headerShapes("CompanyLogo")) Then
Set shapeToDelete = headerShapes("CompanyLogo")
End If
End If
If Not (shapeToDelete Is Nothing) Then
shapeToDelete.Delete
End If
End Sub
edit
I steped trough my code. All is fine until I reach the line shapteToDelete.Delete
in removeLogo
. Here Word crashes hard, even while debugging. I'm using Word 2007 (and that is a requirement)
edit2
I cleared all macros, all normals.dot, all autoloading templates, then created a new document with the two routines above and this test method:
Sub test()
setLogoAt 5, "C:\path\to\logo.jpg"
removeLogo
setLogoAt 6, "C:\path\to\logo.jpg"
End Sub
When I run test
it crashes in removeLogo
at shapeToDelete.Delete
.
Edit 3
I 'solved' the problem by first making the headers/footers view the active view in Word, then deleting the Shape and then returning to normal view. Very strange. It works but as a programmer I'm not happy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另一个可能的解决方案是首先尝试选择形状,然后删除选择:
shapeToDelete.Select
Selection.Delete
如果此方法有效,您可能需要关闭屏幕更新,否则当 Word 在文档中移动时,您会看到闪烁。
Another potential solution is to try and select the shape first and then delete the selection:
shapeToDelete.Select
Selection.Delete
You would probably want to switch off screen updating if this works, else you'll get flickering as Word moves around the document.
我以前遇到过这个问题,通常会出现自动化错误:“调用的对象已与其客户端断开连接”。我还没有找到解决办法。
然而,一个好的解决方法是隐藏形状而不是删除它。
所以:
shapeToDelete.Visible = False
I've experienced this problem before and normally with an automation error: "The object invoked has disconnected from its clients". I haven't yet found a solution.
However a good workaround is to hide the shape rather than delete it.
So:
shapeToDelete.Visible = False
这有效:
我只有 2 个盒子要隐藏,所以这不是通用的
This works:
I only have 2 boxes to hide so this isn't generic