我需要在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 = 1With 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:=SaveAsNameEnd With
Set WordApp = Nothingend sub
因为时间仓促说的不是很明确的地方请指出来,谢谢
遇到几个问题
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 = 1With 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:=SaveAsNameEnd With
Set WordApp = Nothingend 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 = 1With 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 = NothingEnd Sub