简体   繁体   English

MS Word 宏的权限被拒绝

[英]Permission denied on MS Word macro

I have an MS Word document (Office 365 version 1803) with a large number of images.我有一个包含大量图像的 MS Word 文档(Office 365 版本 1803)。 I need to select all images in the document, but there are far too many to do this by hand.我需要选择文档中的所有图像,但是手动完成的图像太多了。 Looking online, it seems the only way to do this is using a macro, which I have no experience with.在网上查看,似乎唯一的方法是使用我没有经验的宏。 I wrote the following very simple macro:我写了以下非常简单的宏:

Sub SelectAllImages()
    ActiveDocument.Shapes.SelectAll
End Sub

When I saved the document, I was forced to change it to a macro-enabled Word document (.docm), which I did.当我保存文档时,我被迫将其更改为启用宏的 Word 文档 (.docm),我也这样做了。 However, whenever I try to run the macro I get the following error:但是,每当我尝试运行宏时,都会出现以下错误:

Run-time error '70': Permission denied

I have Googled this error, but nothing has helped me fix it.我在谷歌上搜索了这个错误,但没有任何帮助我解决它。 Anyone have any idea what I'm doing wrong?任何人都知道我做错了什么?

Edit: As additional data, what I'm trying to do is delete most but not all of the images.编辑:作为附加数据,我想要做的是删除大部分但不是所有的图像。 I have a document that is 200+ pages long with an average of about 1 image per page.我有一个 200 多页长的文档,平均每页大约有 1 张图像。 I need to publish 2 versions of this document: 1 with all images, the second with all but about 12 of the images removed.我需要发布此文档的 2 个版本:第一个包含所有图像,第二个删除了大约 12 个图像之外的所有图像。 The document is updated regularly, and I don't want to have to keep updating 2 separate versions and ensure they are identical except for the inclusion of the images.该文档会定期更新,我不想一直更新 2 个单独的版本,并确保它们除了包含图像之外是相同的。 Thus I'd like to be able to maintain only one version, which includes all the images.因此,我希望能够只维护一个版本,其中包括所有图像。 Then after each update I'd like to be able to select all the images, manually unselect the 12 I want to keep, and delete the others.然后在每次更新后,我希望能够选择所有图像,手动取消选择我想要保留的 12 个图像,然后删除其他图像。

If there is a way to somehow "tag" the images I want to keep and have a macro delete all but the tagged ones that would be an even better solution.如果有一种方法可以以某种方式“标记”我想要保留的图像,并使用宏删除所有标记的图像,这将是一个更好的解决方案。

You will get that error if there are any InlineShapes in the document.如果文档中有任何 InlineShapes,您将收到该错误。 There only has to be one and that command will error out.只需要一个,该命令就会出错。 You will have to select InlineShapes separately from Shapes with wrapping text.您必须从带有换行文本的形状中分别选择 InlineShapes。 See code below.请参阅下面的代码。

Concerning your question about tagging.关于你关于标签的问题。 To "Tag" the images put a unique phrase like "Do Not Delete" in the Alt Text of the images.要“标记”图像,请在图像的替代文本中放置一个独特的短语,例如“请勿删除”。 Then you can use code like the below to remove all images, except those that have been tagged.然后,您可以使用如下代码删除所有图像,但已标记的图像除外。

For Inline Images this code will potentially leave a blank paragraph in the document and I will leave that to you to figure out how you want the final document to look.对于内嵌图像,此代码可能会在文档中留下一个空白段落,我会将其留给您来确定您希望最终文档的外观。

Sub RemoveAllImagesWithExceptions()
Dim doc As Word.Document, iShp As Word.InlineShape
Dim shp As Word.Shape, i As Long, rng As Word.Range
Set doc = ActiveDocument
Set rng = doc.Content
For i = rng.InlineShapes.Count To 1 Step -1
    Set iShp = rng.InlineShapes(i)
    Select Case iShp.Type
        Case wdInlineShapeLinkedPicture, wdInlineShapePicture
            If InStr(1, iShp.AlternativeText, "Do Not Delete") = 0 Then
                iShp.Delete
            End If
    End Select
Next
For i = rng.ShapeRange.Count To 1 Step -1
    Set shp = rng.ShapeRange(i)
    Select Case shp.Type
        Case msoLinkedPicture, msoPicture
            If InStr(1, shp.AlternativeText, "Do Not Delete") = 0 Then
                shp.Delete
            End If
    End Select
Next
End Sub

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

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