Я создаю документы Word и добавляю изображения с помощью Excel VBA.
Поскольку я хочу, чтобы изображения умещались на половину ширины страницы, я создаю временный документ с макетом из двух столбцов и вставляю изображение сначала туда, а затем вырежьте и вставьте его в документ, который мне действительно нужен.
Временный документ закрывается после использования. Когда программа закрывает основной документ и создает другой, появляется всплывающее окно с вопросом, хочу ли я сохранить последний скопированный элемент. Как мне закрыть это окно или сказать ему «нет» из VBA?
При этом программа продолжает работать и игнорирует всплывающее окно, но раздражает, что программа не закрыта, когда я явно так и сказал.
После того, как все документы созданы, многие windows 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
Вот как выглядит всплывающее окно: Так выглядит всплывающее окно