学习VBA中遇到的问题

来源:百度知道 编辑:UC知道 时间:2024/05/31 02:35:33
那位大虾,帮我看看下面的代码,为何在EXCEL 2003上跑不通呢?
Sub WD()
Dim appWD As Word.Application
Application.StatusBar = "chuan jian word ying yong cheng xu dui xiang"
Set appWD = CreateObject("Word.Application")
With appWD
.Visible = True
Application
.StatusBar = "chuang jian yi ge xin wen dang......"
.Documents.add
.ActiveDocument
.Paragraphs (1)
.Range
.InsertBefore "Microsoft Excel VBA ru men dao jing tong"
Application
.StatusBar = "zheng zai bao cun wen dang"
.ActiveDocument
.SaveAs "E:\try.docx"
Application
.StatusBar = "tui chu Word...."
End With
Set appWD = Nothing
Application
.StatusBar = False
End Sub

你代码贴得有问题,有些是不用换行的。帮你修正了,如下:

Sub WD()
Dim appWD As Object

Application.StatusBar = "chuang jian word ying yong cheng xu dui xiang"
Set appWD = CreateObject("Word.Application")
With appWD
.Visible = True
Application.StatusBar = "chuang jian yi ge xin wen dang......"
.Documents.Add
.ActiveDocument.Paragraphs(1).Range.InsertBefore "Microsoft Excel VBA ru men dao jing tong"
Application.StatusBar = "zheng zai bao cun wen dang"
.ActiveDocument.SaveAs "E:\try.docx"
Application.StatusBar = "tui chu Word...."
End With
Set appWD = Nothing
Application.StatusBar = False
End Sub

试过是可以跑的,没什么问题。

1,要在工具——引用中添加WORD的库。
2,你的代码中有很多属性跑不出来,应该不是WORD程序的属性。(是Documents的属性?)删掉有错的可以跑出来。
Sub WD()
Dim appWD As Word.Application
Application.S