Hi All,
My test routine below works but needs 2 items adjusting.
1) The page break is inserted on a new line which creates a blank page (page 2 in the document). I would like to insert the page break on the same line as the text "Keep with test 11" this will result in removing the second blank page
2) How to remove the paragraph marks at the end of the document
Any help will greatly help. Many thanks in advance
Sub TextToWord() Dim lCount As Long 'generic counter Dim lPageCount As Long 'current document page number Dim sText As String 'generic text Dim lWdPageNumber As Long 'document page number after inserting text Dim oRngBreak As Object 'Range object for page break On Error Resume Next Set oApp = GetObject(, "Word.Application") If Err Then Set oApp = CreateObject("Word.Application") End If On Error GoTo 0 Set oDoc = oApp.Documents.Add Set oSelection = oApp.Selection 'uncomment to view document while running code 'oApp.Visible = True For lCount = 1 To 23 sText = "Text to test" & lCount & vbNewLine & "Keep with test " & lCount & vbNewLine & vbNewLine 'get current page number before inserting text lPageCount = oSelection.Information(3) '(3=wdActiveEndPageNumber) 'send text to word document With oSelection .ParagraphFormat.Alignment = 0 '(0=left align text) .Font.Name = "Times New Roman" .Font.Size = 11 .Font.Color = 0 .Font.Bold = False .TypeText (sText) End With 'word document page number after inserting text lWdPageNumber = oSelection.Information(3) 'insert page break on previous page if text over wraps to another page 'this has the effect of inserting the previous text on a new page If lWdPageNumber > lPageCount Then Set oRngBreak = oSelection.Paragraphs(1).Range.Previous.Paragraphs(1).Range oRngBreak.Collapse Direction:=(1) '(1= wdCollapseStart) oRngBreak.InsertBreak (7) 'select position in document oSelection.Paragraphs(1).Range.Previous.Paragraphs(1).Range.Select oSelection.Collapse Direction:=(1) 'wdCollapseEnd 'have different text at the top of a new page sText = "Top of page " & lWdPageNumber & vbNewLine & vbNewLine 'send text to top of page in word document With oSelection .ParagraphFormat.Alignment = 1 'center text .Font.Name = "Times New Roman" .Font.Size = 14 .Font.Color = 255 .Font.Bold = True .TypeText (sText) End With End If Next lCount 'add text at end of the document sText = "This is the end of the document" With oSelection .ParagraphFormat.Alignment = 0 'left align text .Font.Name = "Times New Roman" .Font.Size = 14 .Font.Color = 255 'red .Font.Bold = True .TypeText (sText) End With oApp.Visible = True End Sub