Home > Blockchain >  Word VBA procedure calls suddenly failing second time through
Word VBA procedure calls suddenly failing second time through

Time:09-21

I've built an access database that populates a table and then opens several Word mail merge templates, generates merged documents, and saves them. The relevant portion of the code gets or creates a Word application object, opens a merge template as a document object, runs the mail merge, and closes the template. It then looks for the merge output, makes that the active document, saves as (following a filenaming schema), and closes it. It then quits Word if no other documents are open, and releases the document and application objects.

Following an MS update that installed last week, this all works great once, but on the second runthrough, the generated merge document stops responding after it is made the active document. I've hit the Google hard to see if I can find an explanation or workaround, with no luck. Anyone have any ideas?

Relevant code:

On Error Resume Next
Set objWord = GetObject(, "Word.Application")
If objWord Is Nothing Then
  Set objWord = CreateObject("Word.Application")
End If
On Error GoTo 0
Set objDoc = objWord.Documents.Open(templateName)
objWord.Visible = True
objWord.ActiveWindow.View.Type = wdNormalView
objDoc.MailMerge.OpenDataSource Name:= _
  tempRoot & "Merge_Output.txt", ConfirmConversions:=False, ReadOnly _
  :=True, LinkToSource:=True, AddToRecentFiles:=False, PasswordDocument:= _
  "", PasswordTemplate:="", WritePasswordDocument:="", _
  WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
  Connection:="", SQLStatement:="", SQLStatement1:="", SubType:= _
  wdMergeSubTypeOther
  objDoc.MailMerge.ViewMailMergeFieldCodes = False
objDoc.MailMerge.Execute
objDoc.Close False
For Each openDoc In objWord.Documents
  strOpenDoc = openDoc.Name
  If strOpenDoc Like "Form Letters*" Then
    openDoc.Activate
    Exit For
  End If
Next openDoc
objWord.ActiveDocument.Content.NoProofing = False
objWord.ActiveDocument.SaveAs TargetDir & "\" & DocBase & "_Designation" '<=====This is where the document stops responding
objWord.ActiveDocument.Close False
If objWord.Documents.Count = 0 Then objWord.Application.Quit
Set objDoc = Nothing
Set objWord = Nothing

CodePudding user response:

I've not come across this issue but would be tempted to try the following:

For Each openDoc In objWord.Documents
    If openDoc.Name Like "Form Letters*" Then
        With openDoc
            .Content.NoProofing = False
            .SaveAs2 FileName:=TargetDir & "\" & DocBase & "_Designation", FileFormat:=wdFormatXMLDocument
            .Close False
        End With
        Exit For
    End If
Next openDoc
If objWord.Documents.Count = 0 Then objWord.Application.Quit

CodePudding user response:

Well, turns out it wasn't a code issue at all. The MS Security Update had some unintended/unexpected effects, including causing Word to stop responding when trying to Save As some/most Word docs, whether programatically or manually. Uninstalled the update and all is now working.

  • Related