一:开启类库

VBA不仅可以操作Excel也可以操作Word,Word也有开发工具也有宏录制。

VBA Word_c#

二:基础示例

Dim wdcx As Object, wd As Object
Set wdcx = CreateObject("word.application")
Set wd = wdcx.Documents.Open(Thisworkbook.Path & "xxx.docx")
Set wd = wdcx.Documents.Add

' 在word中新建表格
With wd
	.tables.Add.Range(0,0), 5, 6
	.tables(1).Style = "网格型"
	.SaveAs ThisWorkbook.Path & "xxx"
End With

wdcx.Visible = True



wd.Range(2, 4).Font.Color = RGB(255, 0, 0)
wd.Tables(1).Range

wd.Save
wd.Close
wdcx.Quit

Set wdcx = Nothing
Set wd = Nothing

三:txt转pdf

wd.ExportAsFixedFormat OUtputFileName:= "D:\", ExportFormat:=wdExportFormatPDF
wd.Close SaveChanges:=False