VBA Word - 将光标移动到边框之外的新行

时间:2017-06-26 09:53:10

标签: excel excel-vba ms-word word-vba vba

我正在尝试将光标移动到我使用VBA从Excel生成的Word文档中的新行。

我确实设法添加了一个新行,但是由于Word文档中的最后一个文本条目具有顶部和底部边框,因此添加的每个新行都保留在这些边框内。有没有办法离开那个盒子并越过边界?

附上代码。任何帮助都非常感谢,谢谢!

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

1 个答案:

答案 0 :(得分:1)

这不是很优雅,但你可以将边界推迟到以后:

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

希望有所帮助。