首页 > 解决方案 > 如何不保留最后一项复制?

问题描述

我正在使用 Excel VBA 创建 Word 文档并添加图像。

由于我希望图像适合页面宽度的一半,因此我创建了一个具有两列布局的临时文档,并先将图像粘贴到那里,然后将其剪切并粘贴到我真正需要的文档中。

临时文档在使用后关闭。当程序关闭主文档并创建另一个文档时,会弹出一个窗口询问我是否要保留最后一个项目的复制。如何从 VBA 关闭此窗口或对它说“不”?

话虽如此,程序继续运行,并忽略弹出窗口,但当我明确表示时,程序没有关闭很烦人。

创建所有文档后,Microsoft Word 的许多窗口保持打开状态,每个窗口都会弹出此窗口。我想它也会在内存中保存图像,所以这也不好。

Sub Newdoc_withImages()

    Dim wdapp As Word.Application
    Dim doct As Documents
    Dim imag As Variant
    .
    .
    *more variables*
    
    Set wdapp = New Word.Application
    With wdapp
        .Visible = True
        .Activate
        .Documents.Add "C:\Users\USER\Desktop\Template.dot"
        .Selection.GoTo (*I position the cursor and write some things in preparation to add image*)
        If condition (*are there any images available? If yes create Temporal Document and give two columns layout for it*)
            .Documents.Add
            .Documents(1).PageSetup.TextColumns.Add Width:=InchesToPoints(2.85), EvenlySpaced:=True
            For Each imag In imgcollection 
                If condition (*If True, take the image and paste it in the Temporal Doc, then cut it to paste it in the document created at the beginning*)
                    .Documents(1).Select
                    .Documents(1).InlineShapes.AddPicture Filename:=imag, LinkToFile:=False, SaveWithDocument:=True
                    .Documents(1).InlineShapes(1).Select
                    .Selection.Cut
                    .Documents(2).Select
                    .Selection.GoTo (*Position to paste the img*)
                    .Selection.Paste
                    imgcollection.Remove (imag) (*here I remove the image from my collection, not relevant*)
                End If
            Next imag
            (*The Temporal Document actually closes*)
            .Documents(1).Close 0
            (*I return to the Document I actually care, ready for the next batch of images if I need to add more for given condition*)
            .ActiveDocument.Select
        End If
        .DisplayAlerts = 0 (*I tried these to close any pop up maybe I'm wrong, because It doesn't seem to work either*)
        .ActiveDocument.SaveAs2 (objSubFolder.Path & "\" & StrConv(NamesxCrear.Items(i), vbProperCase))
        .ActiveDocument.Close (*HERE is where the pop up window comes up asking me if I want to keep the last item copied, I don't know how to close it from here*)
        .Application.Quit (*doesn't actually quit, program reamins open because of the pop up*)
    
    End With

End Sub

这是弹出窗口的样子:
这是弹出窗口的样子

标签: excelvbams-word

解决方案


Doug Robbins 在 MS 网站上的回答:这里

如果您在 Normal.dotm 模板或加载项中创建以下宏,则该消息将不再出现

Sub FileClose()
Dim MyData   As DataObject
Set MyData = New DataObject
MyData.SetText ""
MyData.PutInClipboard
ActiveDocument.Close
End Sub

它用空字符串替换剪贴板的内容,然后在您使用 File>Close 时关闭文档。


推荐阅读