Sub不会打开MS字(VBA)

我正在尝试使用一个在Word中build立报告的macros(这个macros不是由我写的,也不是写这个macros的人在这里工作)。 无论如何,它对我的​​同事个人电脑运行良好,但不会运行在我的(我有一个表面亲3,如果重要)。

当我运行macros时失败: Set VAVdoc = WordApp.Documents.Add

当它失败我收到以下错误:

运行时错误“91”:对象variables或块variables未设置。

我不知道为什么它不能在我的设置上运行,但它会在我的同事身上。 有任何想法吗?

任何帮助不胜感激。 更新:尝试后:

设置o = CreateObject(“Word应用程序”)

再次没有o.quit我得到的错误

ActiveX组件不能创build对象

 Dim i As Integer Dim WordApp As Word.Application Dim HVACdoc As Word.Document, VAVdoc As Word.Document, CDWdoc As Word.Document Dim FullName As String, ShortName As String, TrendMonth As String, TrendYear As String, StartTrend As String, EndTrend As String Dim ChartName As String, Directory As String, FolderName As String Dim VAVName As String, VAVLocation As String, HVACName As String, HVACLocation As String, CDWName As String, CDWLocation As String Call WorksheetCall("AHU-1") FullName = "Mossman Building" ShortName = "Mossman" TrendMonth = MonthName(Month(Cells(4, 3))) TrendYear = Year(Cells(4, 3)) StartTrend = Format(Cells(4, 3), "dddd, mmmm dd, yyyy") EndTrend = Format(Cells(4, 3) + 6, "dddd, mmmm dd, yyyy") Directory = "P:\M&V\- Projects\UNC-G\UNCG Year 7 Report" FolderName = MonthName(Month(Cells(4, 3)), True) & " " & TrendYear VAVName = FolderName & " - " & ShortName & " C.2.3.docx" VAVLocation = Directory & FolderName & "\" & VAVName HVACName = FolderName & " - " & ShortName & " C.2.4.docx" HVACLocation = Directory & FolderName & "\" & HVACName CDWName = FolderName & " - " & ShortName & " C.2.5.docx" CDWLocation = Directory & FolderName & "\" & CDWName On Error Resume Next Set WordApp = GetObject(, "Word.Application") If Err.number <> 0 Then Set WordApp = CreateObject("Word.Application") End If On Error GoTo 0 Call DefineDescriptions(TrendMonth, TrendYear, StartTrend, EndTrend) 'Report C.2.3 - VAV Conversion If Dir(VAVLocation) = "" Then Set VAVdoc = WordApp.Documents.Add VAVdoc.SaveAs (VAVLocation) End If 

这不是一个解决scheme,而是一个debugging提示。

首先在任务pipe理器中closures所有运行的Word应用程序。

Tools - References取消select所有对Microsoft Word ??? Object Library的引用Microsoft Word ??? Object Library Microsoft Word ??? Object Library

现在尝试下面的macros。 用F8来完成它。 但最后还要通过.Close.Quit 。 因为如果您没有在系统中收集未使用的Word进程。

 Sub testWordAppLateBinding() Dim oWordApp As Object Dim oWordDoc As Object Set oWordApp = CreateObject("Word.Application") oWordApp.Visible = True Set oWordDoc = oWordApp.Documents.Add oWordDoc.Close oWordApp.Quit End Sub 

这是否工作? 有没有Word打开一个新的文件? 如果是这样,那么晚期绑定的作品。 如果没有,发生哪些错误?

现在在Tools - Referencesselect对Microsoft Word 14.0 Object Library的引用,然后尝试下面的macros:

 Sub testWordAppEarlyBinding() Dim oWordApp As Word.Application Dim oWordDoc As Word.Document Set oWordApp = CreateObject("Word.Application") oWordApp.Visible = True Set oWordDoc = oWordApp.Documents.Add oWordDoc.Close oWordApp.Quit End Sub 

这是否也工作? Word是否有一个新文档打开? 如果是的话,那么早期的绑定工作也是。 如果是这样,那么错误是在别处。 如果没有,但后期绑定的作品,那么你必须改变你的代码到后期绑定。

如果什么都行不通,确定你可以手动启动Word应用程序? Word是否没有对话框? 或显示哪些对话框?