excel vba 生成word

爱偷懒的茧 2009-11-19 10:08:54
我需要在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


因为时间仓促说的不是很明确的地方请指出来,谢谢
...全文
939 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljwcompany 2011-08-10
  • 打赏
  • 举报
回复
继续关注
slashliang 2011-08-05
  • 打赏
  • 举报
回复
对他有没有用我不知道,反正对我是非常有用的!感谢你![Quote=引用 3 楼 chinaboyzyq 的回复:]
如果我解决了你的问题,请把分结给我!
[/Quote]
chinaboyzyq 2009-11-19
  • 打赏
  • 举报
回复
如果我解决了你的问题,请把分结给我!
chinaboyzyq 2009-11-19
  • 打赏
  • 举报
回复

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
爱偷懒的茧 2009-11-19
  • 打赏
  • 举报
回复
帮帮忙吧,自己顶一下

2,462

社区成员

发帖
与我相关
我的任务
社区描述
VBA(Visual Basic for Applications)是Visual Basic的一种宏语言,是在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言。
社区管理员
  • VBA
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧