当前位置:编程学习 > VB >>

excel vba 生成word

我需要在excel用vba编写生成word文档代码。
遇到几个问题
1.我在<code>那添加了表格(2行3列),然后我想添加新页,但新页总是加在表格与"AAAAA"中间,我想让表格挨着“AAAAA”,在表格后面生成新页,并写入文字,怎么实现
2.插入背景图片显示不出来,如何解决
sub CreateWord()
Dim WordApp As New Word.Application
Dim PageCount As Integer: PageCount = 1
Dim Otable As Object
Dim i As Integer
Dim SaveAsName As String
SaveAsName = ThisWorkbook.Path & "\test.doc"
Dim ICount As Integer: ICount = 1

With WordApp

.Documents.Add
    With .Selection
        .Font.Size = 24
        .Font.Bold = True
        .Font.Color = wdColorDarkGreen
        .ParagraphFormat.Alignment = wdAlignParagraphCenter
        .TypeText Text:="AAAAA"
        .TypeParagraph
        .TypeParagraph
        .TypeParagraph
        .TypeParagraph
        .TypeParagraph
        .TypeParagraph
        .TypeParagraph
    End With
'<code>:
    Set Otable = .ActiveDocument.Tables.Add(Range:=.Selection.Range, NumRows:=3, NumColumns:=2)
    .Selection.Tables(1).Rows.Alignment = wdAlignRowCenter
    Set Otable = Nothing
    For i = 2 To 5
        With .Selection
            .Font.Size = 24
            .Font.Bold = True
            .Font.Color = wdColorDarkGreen
            .ParagraphFormat.Alignment = wdAlignParagraphCenter
            .TypeText Text:="AAAAA"
            .TypeParagraph
            .InsertBreak Type:=wdPageBreak

        End With
    Next i

    
    .ActiveDocument.Background.Fill.Visible = msoTrue
    .ActiveDocument.Background.Fill.ForeColor.RGB = RGB(255, 255, 255)
    .ActiveDocument.Background.Fill.Transparency = 0#
    .ActiveDocument.Background.Fill.UserPicture ThisWorkbook.Path & "\Images\imgback.jpg"
'    .ActiveDocument.Background.Fill.Visible = msoCTrue
    .ActiveWindow.ActivePane.VerticalPercentScrolled = 0

    .ActiveDocument.SaveAs Filename:=SaveAsName

End With
Set WordApp = Nothing

end sub


因为时间仓促说的不是很明确的地方请指出来,谢谢 --------------------编程问答-------------------- 帮帮忙吧,自己顶一下 --------------------编程问答--------------------

Sub CreateWord()
Dim WordApp As New Word.Application
Dim PageCount As Integer: PageCount = 1
Dim Otable As Object
Dim i As Integer
Dim SaveAsName As String
SaveAsName = ThisWorkbook.Path & "\test.doc"
Dim ICount As Integer: ICount = 1

With WordApp

.Documents.Add
    With .Selection
        .Font.Size = 24
        .Font.Bold = True
        .Font.Color = wdColorDarkGreen
        .ParagraphFormat.Alignment = wdAlignParagraphCenter
        .TypeText Text:="AAAAA"
        .TypeParagraph

    End With
' <code>:
    .ActiveDocument.Tables.Add Range:=.Selection.Range, NumRows:=3, NumColumns:=2
    .Selection.Tables(1).Rows.Alignment = wdAlignRowCenter
    .Selection.EndKey Unit:=wdStory '这里解决分页问题
    .Selection.TypeParagraph
    .Selection.InsertBreak Type:=wdPageBreak
    .Selection.TypeText Text:="chinaboyzyq(猴哥)"
    .Selection.TypeParagraph

    .ActiveDocument.Background.Fill.Visible = msoTrue
    .ActiveDocument.Background.Fill.ForeColor.RGB = RGB(255, 255, 255)
    .ActiveDocument.Background.Fill.Transparency = 0#
    .ActiveDocument.Background.Fill.UserPicture "C:\temmp\Images\imgback.jpg"
    .ActiveDocument.ActiveWindow.View.DisplayBackgrounds = True '这里解决背景图像显示问题
    
    .ActiveDocument.SaveAs Filename:=SaveAsName
End With
WordApp.Quit '你要让你的word退出。
Set WordApp = Nothing

End Sub
--------------------编程问答-------------------- 如果我解决了你的问题,请把分结给我! --------------------编程问答-------------------- 对他有没有用我不知道,反正对我是非常有用的!感谢你!
引用 3 楼 chinaboyzyq 的回复:
如果我解决了你的问题,请把分结给我!
--------------------编程问答-------------------- 继续关注
补充:VB ,  VBA
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,