乔山办公网我们一直在努力
您的位置:乔山办公网 > word文档 > excel用vba自动生成word-vba创建word,vba自动生成word

excel用vba自动生成word-vba创建word,vba自动生成word

作者:乔山办公网日期:

返回目录:word文档


直接写入word即可
我正在做这个

Sub GenDocfromExcel()

'excel控制word,生成新文件,插入图片和文件名,保存
'office 2003, VBA工具/引用中要勾选Microsoft Word 11.0 Object Library
'office 2007, VBA工具/引用中要勾选Microsoft Word 12.0 Object Library
'...

Range("A1:B13").Copy '复制当前内容

Dim WordApp As Word.Application '定义变量
Set WordApp = CreateObject("Word.Application") '生成WORD对象
WordApp.Documents.Add '新建文件

WordApp.Selection.Paste '粘贴

fn$ = "D:\" & Range("b1") '生成文件名
WordApp.ActiveDocument.SaveAs fn$ '保存文件

WordApp.Quit '退出
Set WordApp = Nothing '取消变量

End Sub
VBA也得根据你的要求来.具体情况具体处理

以下是自行编写的代码实例片段,供参考:

'==7a686964616fe58685e5aeb9338=================
'生成分析报告
'===================

Private Sub btnDtyp_Click()

sPathName = ThisWorkbook.Path & "\模板\" & "动态研判分析.doc"
Set WordApp = CreateObject("Word.Application") '生成WORD对象
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Add(sPathName)

With Sheet6
sText = "{ksrq}" '开始日期
sReplace = Format(Range("D1").Value, "YYYY年M月DD日")
WordDoc.Range.Find.Execute sText, False, True, False, False, False, True, 0, False, sReplace, 2

sText = "{jsrq}" '结束日期
sReplace = Format(Range("F1").Value, "YYYY年M月DD日")
WordDoc.Range.Find.Execute sText, False, True, False, False, False, True, 0, False, sReplace, 2

sText = "{zj}" '总计人数
sReplace = .Range("E48").Value
WordDoc.Range.Find.Execute sText, False, True, False, False, False, True, 0, False, sReplace, 2

sText = "{nan}" '男性人数
sReplace = .Range("B33").Value
WordDoc.Range.Find.Execute sText, False, True, False, False, False, True, 0, False, sReplace, 2

sText = "{nv}" '女性人数
sReplace = .Range("B34").Value
WordDoc.Range.Find.Execute sText, False, True, False, False, False, True, 0, False, sReplace, 2


sText = "{jyrs}" '就业人数
sReplace = .Range("B84").Value + .Range("B85").Value
WordDoc.Range.Find.Execute sText, False, True, False, False, False, True, 0, False, sReplace, 2

sText = "{jxrs}" '就学人数
sReplace = .Range("B86").Value
WordDoc.Range.Find.Execute sText, False, True, False, False, False, True, 0, False, sReplace, 2


End With

sFileName = "分析记录(" & Range("D1").Value & "至" & Range("F1").Value & ").doc"
WordApp.ChangeFileOpenDirectory "E:\分析报告\"
WordDoc.SaveAs Filename:=sFileName, FileFormat:=wdFormatDocument

End Sub

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网