Maybe try it this way:
Option Explicit
Sub Output2Word_v2()
Dim r As Long, c As Integer: c = 15 'c = 15 => Column "O"
Dim wrdApp As Object, wrdDoc As Object
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Do Until Trim(Cells(1, c).Value) = ""
r = 2 'Starting point/row for each turn of the inner loop
Set wrdDoc = wrdApp.Documents.Add
Do Until Trim(Cells(r, "A").Value) = ""
On Error Resume Next
wrdApp.Range.Selection.EndKey Unit:=wdStory
On Error GoTo 0
If Cells(r, c).Value = "X" Then
Range("A" & r).Copy
'wrdApp.Selection.PasteExcelTable False, False, False
wrdApp.Selection.PasteAndFormat (wdFormatOriginalFormatting)
Application.CutCopyMode = False
ElseIf Cells(r, c).Value <> "" Then
Range(Cells(r, c).Value).Copy
'wrdApp.Selection.PasteExcelTable False, True, False
wrdApp.Selection.PasteAndFormat (wdFormatOriginalFormatting)
Application.CutCopyMode = False
wrdApp.ActiveDocument.Tables(wrdApp.ActiveDocument.Tables.Count).AutoFitBehavior (wdAutoFitFixed)
End If
r = r + 1 'Next row
Loop
'Application.Wait (Now + TimeValue("00:00:04")) 'Only for testing - Preview of the created document
wrdDoc.SaveAs Filename:=ActiveWorkbook.Path & "\" & Trim(Cells(1, c).Value) & ".docx"
wrdDoc.Close SaveChanges:=False
Set wrdDoc = Nothing
c = c + 1 'Next column
Loop
wrdApp.Visible = False
wrdApp.Quit
Set wrdApp = Nothing
MsgBox "Done"
End Sub
In total, I didn't change too much. What will be the result ?
Bookmarks