Excel生成Word文档

Excel 生成word报告

查了很多资料,看看能否使用。

Option Explicit

Sub CreateBasicwordreport()
	Dim wdApp as object
	dim saveName as string
	set wdapp=createobject("work.application")
	with wdapp
	.documents.add

	with .selection
		.ParagraphFormat.Alignment=1
		.boldrun
		.font.size=14
		.typetext "TESTT"
		.Boldrun
		.typeparagraph
		.font.size=10
		.paragraphformat.alignment=0
		.typeparagraph
	end with

range("C52",Range("C5").end(xldown).end(xltoright)).copy
		.selection.paste
		savename=environ("UserProfile")&"\Desktop\report"*_Format(Now,"ddmmyy hhmmss")&".docx"
		.activedocument.saveas2 savename
		.activedocument.close
		.quit
end with
set wdapp=nothing
end sub

添加表格和chart到word文档内

Option Explicit

Sub CreateBasicwordreport()
	Dim wdApp as word.application
	dim saveName as string
	set wdapp=createobject("work.application")
	with wdapp
	
	
	.documents.add "c\data collection\CrossPeriod_Template.docx" 
range("C5",Range("C5").end(xldown).end(xltoright)).copy
		.selection.goto  what:=-1,Name:="TDL"
		.selection.paste
		Chart1.charArea.copy
		.selection.goto  waht:=-1,Name:="ChartDL"
		.selection.paste
		
			savename=environ("UserProfile")&"\Desktop\report"*_Format(Now,"ddmmyy hhmmss")&".docx"
		.activedocument.saveas2 savename
		.activedocument.close
		.quit
end with
set wdapp=nothing
end sub

你可能感兴趣的:(学习Excel)