在文档末尾插入几行文本(MS WORD MACRO)

时间:2017-08-08 17:10:08

标签: vba word-vba

我的标题似乎与可能有我的答案的问题不匹配,我确实从其他线程/网站找到了一些片段来帮助我做到这一点。我正在寻找帮助将整个宏绑在一起的帮助。以下是我到目前为止的情况:

Sub Test()

Selection.EndKey Unit:=wdStory
Dim oPara1 As Word.Paragraph

Set oDoc = oWord.Documents.Add

Set oPara1 = oDoc.Content.Paragraphs.Add
With oPara1.Range
    .ParagraphFormat.Alignment = wdAlignParagraphCenter
    .InsertParagraphAfter
    With .Font
        .Name = "Times New Roman"
        .Size = "12"
        .Bold = True
    End With
End With

Selection.TypeText Text:="Fosters, Inc."
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.TypeText Text:="www.genericwebsite.com"
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend

'this needs to be left alignment from here on out
Selection.TypeText Text:="Block\Paragraph Format:"
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.TypeText Text:="Run Date:"
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.TypeText Text:="Picture:"
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.TypeText Text:="Symbol:"
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.TypeText Text:="Guest Book:"

End Sub

我希望它移到文档的末尾并打印:

                     Fosters, Inc.
                www.genericwebsite.com

Block\Paragraph Format:
Run Date:
Picture:
Symbol:
Guest Book:

感谢您的帮助 - 我今天在Word中用vba花了一个小时左右。

1 个答案:

答案 0 :(得分:1)

Option Explicit

Sub Test()

Selection.EndKey Unit:=wdStory
Dim oPara1 As Word.Paragraph

Dim oDoc As Word.Document
Set oDoc = ActiveDocument

Set oPara1 = oDoc.Content.Paragraphs.Add
With oPara1.Range
    .ParagraphFormat.Alignment = wdAlignParagraphCenter
    .InsertParagraphAfter
    With .Font
        .Name = "Times New Roman"
        .Size = "12"
        .Bold = True
    End With
End With

Selection.TypeText Text:=vbCr
Selection.TypeText Text:="Fosters, Inc." & vbCr
Selection.TypeText Text:="www.genericwebsite.com" & vbCr
oPara1.Range.ParagraphFormat.Alignment = wdAlignParagraphLeft
Selection.TypeText Text:="Block\Paragraph Format:" & vbCr
Selection.TypeText Text:="Run Date:" & vbCr
Selection.TypeText Text:="Picture:" & vbCr
Selection.TypeText Text:="Symbol:" & vbCr
Selection.TypeText Text:="Guest Book:"

End Sub