I have a piece of code that opens a word document and performs a mail merge. When it is done i want to close the template without saving. I have added
Application.DisplayAlerts = wdAlertsNone
but it does not seem to be working or i'm not using it correctly. There might also be another way to achieve this. Below is the code that i'm using.
Private Sub cmdMerge_Click()Application.EnableEvents = FalseApplication.ScreenUpdating = FalseApplication.Calculation = xlCalculationManualApplication.DisplayAlerts = FalseOn Error GoTo MyErrorPrepareDataDoMergeApplication.EnableEvents = TrueApplication.ScreenUpdating = TrueApplication.Calculation = xlCalculationAutomaticApplication.DisplayAlerts = TrueExit SubMyError:MsgBox Err.DescriptionApplication.EnableEvents = TrueApplication.ScreenUpdating = TrueApplication.Calculation = xlCalculationAutomaticApplication.DisplayAlerts = TrueWord.Application.DisplayAlerts = wdAlertsAllEnd SubSub DoMerge()Dim appWd As Word.ApplicationDim WdDoc As Word.DocumentDim strBookFullName As StringDim cell As Excel.RangeDim strWordPath As StringDim txt As StringstrBookFullName = ActiveWorkbook.FullNamestrWordPath = Sheets("Program").Range("A2").ValueSet appWd = CreateObject("Word.Application")appWd.Visible = TrueWith appWd.Application.DisplayAlerts = wdAlertsNone Set WdDoc = appWd.Documents.Open(strWordPath) WdDoc.Activate WdDoc.MailMerge.OpenDataSource Name:=(strBookFullName), _ ReadOnly:=True, LinkToSource:=0, AddToRecentFiles:=False, _ PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _ WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _ Connection:="", SQLStatement:="SELECT * FROM `MergeRange`", SQLStatement1:="" 'finds text in word doc and replace with merge field For Each cell In Range("MergeText") txt = cell.Offset(0, 1).Value .ActiveDocument.MailMerge.Fields.Add Range:=WdDoc.Bookmarks(cell.Offset(0, 2).Value).Range, Name:=txt & cell.Offset(0, 3).Value Next cell'.Application.DisplayAlerts = wdAlertsNone .ActiveDocument.MailMerge.Execute .ActiveDocument.SaveAs Sheets("Program").Range("B4").Value & "Rebate Letters For " & Sheets("MergeSheet").Range("U2").Value & ".docx" .Quit.Application.DisplayAlerts = wdAlertsAllEnd With Sheets("MergeSheet").Delete ThisWorkbook.Sheets("GoodmanAmana").AutoFilterMode = FalseEnd Sub
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
pls try
Open in new window
SInce you use late binding it does not know what wdAlertsNone isRegards