0

I have an application where I need to merge several nearly identical documents which had been sent out to several users and received with minor changes and comments. The documents are fairly large (500K - 1M) and can contain tables and embedded graphics. The point of this is to consolidate the changes and comments from the various users into one document, keeping track of who did or said what.

The heart of this is the following procedure which uses Application.Merge.

Public Sub runMerge(files As String)

    Dim i As Integer
    Dim v() As String

    v = Split(files, vbCr)

    Word.Application.ScreenUpdating = False
    Word.Documents.Open fileName:=v(0), ReadOnly:=False, addtorecentfiles:=False

    For i = 1 To UBound(v)
        ActiveDocument.Merge fileName:=v(i), _
            MergeTarget:=wdMergeTargetCurrent, _
            DetectFormatChanges:=True, _
            UseFormattingFrom:=wdFormattingFromCurrent, _
            addtorecentfiles:=False
        DoEvents
    Next i

    Word.Application.ScreenUpdating = True
    Word.Application.ScreenRefresh

    ActiveDocument.SaveAs2 "Merge " & Replace(Now, ":", "."), , , , False

End Sub

The code opens the first file explicitly, then merges the remaining documents in a loop. Apart from being a bit fragile, this works correctly as long as the first document was explicitly opened.

Once merged, I use SaveAs2 with a new unqualified file name (i.e. no explicit directory). This works fairly well.

However, if I attempt to use SaveAs2 to place the file in a different directory, as in

ActiveDocument.SaveAs2 "C:\.....\Merge " & Replace(Now, ":", "."), , , , False

the resulting document does not re-appear on the screen after the merge. Further, when explicitly opened later, the document appears somehow damaged, strangely formatted, in one case, some of the text appeared in an orange colour. This behaviour has been seen in Word 2013 and 2016.

Apart from saving the document in whatever default directory (My Documents, in my case), then explicitly moving the file in VBA using copy and Kill and what not, how would one save this without an extra step?

V. V. Kozlov
  • 189
  • 2
  • 10
  • I am slightly confused, does it save or not? Are you saying it saves but with odd formatting? If the path is the only difference between the code that works and doesn't, then try a different path. Is your path correct and is the location writable? – Andy G Feb 12 '18 at 10:10
  • @Andy G It does save, but the resulting document appears to be "damaged" – V. V. Kozlov Feb 12 '18 at 10:23
  • If you comment out the SaveAs completely, so that you're looking at the resulting document when the code finishes, then save it as a user to a different directory - what's the result, then? My suspicion is that somehow, the Merge process hasn't completely finished when your code performs the SaveAs - that something asynchronous is going on, in the background. But if your manual SaveAs shows the same problems, it's something else... – Cindy Meister Feb 12 '18 at 12:01
  • @CindyMeister This sounds promising, as when the document finally reappears after the merge, it takes some time (a minute or two) for all the changes to fully appear. Word is still working on it (long) after the first page is visible. – V. V. Kozlov Feb 12 '18 at 12:50
  • @CindyMeister Manual save to a different directory works as expected. – V. V. Kozlov Feb 13 '18 at 07:51
  • OK, this is probably going to take some trial-and-error on your part. Frist thing I'd do is comment out the ScreenUpdating. Does that make any difference? (And also try counting how many seconds pass between what looks like the end and when SaveAs is called as an Application.OnTime could help.) You could also try putting DoEvents just in front of the SaveAs. – Cindy Meister Feb 13 '18 at 14:04
  • @CindyMeister I already tried a long (1 - 3 min) delay between the end of merge and `SaveAs`, no real difference. What worked was `ActiveDocument.Select` followed by `Selection.Collapse` after merge. Will experiment more and write it up. – V. V. Kozlov Feb 14 '18 at 10:32
  • Interesting - and good detective work :-) – Cindy Meister Feb 14 '18 at 14:08

1 Answers1

0

In the end, I did the following to solve the save problem.

  • Using the super-easy FileCopy VBA function, I copied the first file in the merge group to the directory where I wanted the merged file to end up.

  • The merge loop remains unchanged.

  • When the merge loop is done, I used the solution from an earlier post, VBA word table copy loses data, ActiveDocument.Select followed by Selection.Collapse.

  • ActiveDocument.Save, not SaveAs

V. V. Kozlov
  • 189
  • 2
  • 10