VBA学习笔记(2)--新建word文档并插入文字 - Jacklovely

时间:2024-02-25 21:38:13

说明(2017.3.20):

1. Dim As声明变量类型,Set赋值/初始化

2. With使后面的省略对象,直接点就行,后面要End With

3. Application.StatusBar = "正在创建word",这个会在Excel左下角出现提示

4. .ActiveDocument.SaveAs2(),里面第一个参数是文件名,需要加地址,不然不行

5. 最后记得加Quit,wordApp要清零(释放对象实例,收回该对象占用的内存),Application.StatusBar要false(将状态栏上的信息恢复为默认的“就绪”)

 1 Public Sub dianbo()
 2 
 3     Dim wordApp As Word.Application
 4     Application.StatusBar = "正在创建。。。"
 5     Set wordApp = New Word.Application
 6     
 7     With wordApp
 8         .Visible = True
 9         Application.StatusBar = "正在创建word"
10         .Documents.Add
11         .ActiveDocument.Paragraphs(1).Range.InsertBefore ("邀请函")
12         Application.StatusBar = "正在保存"
13         .ActiveDocument.SaveAs2 ("D:\工作\脚本下载\vba练习\test.docx")
14         Application.StatusBar = "正在退出"
15         .Quit
16     End With
17     Set wordApp = Nothing
18     Application.StatusBar = False
19 End Sub

 

posted on 2017-03-20 22:16  Jacklovely  阅读(6016)  评论(0编辑  收藏  举报