Reputation: 115
I am trying to move cursor to a new line in the Word document that I generate from Excel using VBA.
I do manage to add a new line, however since the last text entry in the Word document has top and bottom borders, every new line added stays within those borders. Is there a way to leave that box and go beyond the border?
Code is attached. Any help is highly appreciated, thanks!
Option Explicit
Sub CreateWordDocument()
Dim wdApp As Object
Dim wdDoc As Object
Dim wdSelection As Object
Dim wdTable As Object
Dim wdRange As Object
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Add
Set wdSelection = wdApp.Selection
With wdSelection
.Font.Name = "Calibri Light"
.Font.Size = "26"
.Font.Color = RGB(0, 0, 0)
.TypeText Text:=("TEXT 1")
.ParagraphFormat.SpaceAfter = 0
.TypeParagraph
.Font.Name = "Calibri Light"
.Font.Size = "11"
.Font.Color = RGB(128, 128, 128)
.TypeText ("Text 2")
.ParagraphFormat.SpaceAfter = 0
.TypeParagraph
.TypeParagraph
.Font.Name = "Calibri Light"
.Font.Size = "11"
.Font.Color = RGB(128, 128, 128)
.TypeText ("Text 3")
With .ParagraphFormat
.Alignment = 1
.Borders(-1).LineStyle = 1
.Borders(-1).LineWidth = 2
.Borders(-3).LineStyle = 1
.Borders(-3).LineWidth = 2
End With
.TypeParagraph ' !!! This line must be modified
End With
End Sub
Upvotes: 0
Views: 995
Reputation: 3145
This is not very elegant, but you could defer the borders until later:
Sub CreateWordDocument()
Dim wdApp As Object
Dim wdDoc As Object
Dim wdSelection As Object
Dim wdTable As Object
Dim wdRange As Object
Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Add
Set wdSelection = wdApp.Selection
With wdSelection
.Font.Name = "Calibri Light"
.Font.Size = "26"
.Font.Color = RGB(0, 0, 0)
.TypeText Text:=("TEXT 1")
.ParagraphFormat.SpaceAfter = 0
.TypeParagraph
.Font.Name = "Calibri Light"
.Font.Size = "11"
.Font.Color = RGB(128, 128, 128)
.TypeText ("Text 2")
.ParagraphFormat.SpaceAfter = 0
.TypeParagraph
.TypeParagraph
.Font.Name = "Calibri Light"
.Font.Size = "11"
.Font.Color = RGB(128, 128, 128)
.TypeText ("Text 3")
.TypeParagraph ' !!! This line must be modified
End With
With wdDoc.Paragraphs(4)
With .Format
.Alignment = 1
.Borders(-1).LineStyle = 1
.Borders(-1).LineWidth = 2
.Borders(-3).LineStyle = 1
.Borders(-3).LineWidth = 2
End With
End With
End Sub
Hope that helps.
Upvotes: 1