我正在尝试使用在Word中构建报表的宏(此宏不是由我编写的,编写它的人也不在这里工作)。无论如何,它在我的同事PC上运行良好但不会在我的运行中运行(如果重要的话,我有一个表面专业3)。
当我运行宏时,它失败了:Set VAVdoc = WordApp.Documents.Add
如果失败,我收到以下错误:
运行时错误' 91':对象变量或未设置块变量。
我不知道为什么它不能在我的设置上运行,但它会在我的同事身上运行。有什么想法吗?
任何帮助非常感谢。 更新:尝试后:
设置o = CreateObject(" Word Application")
再次没有o.quit我得到错误
ActiveX组件无法创建对象
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
答案 0 :(得分:3)
这不是解决方案,而是调试提示。
首先在任务管理器中关闭所有正在运行的Word应用程序。
在Tools - References
中取消选择对Microsoft Word ??? Object Library
的所有引用。
现在尝试以下宏。使用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 - References
中选择对Microsoft Word 14.0 Object Library
的引用,然后尝试以下宏:
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没有对话框吗?或者显示了哪些对话框?