问题描述
我正在尝试使用一个在Word中构建报表的宏(这个宏不是由我写的,也不是写在这里的人在这里工作)。无论如何,它在我的同事PC上运行良好,但不会运行在我的(我有一个表面专业3,如果这重要)。
I am trying to use a macro that builds reports in Word (This macro was not written by me, nor does the person who wrote it still work here). Anyway, it runs fine on my coworkers PC but will not run on mine (I have a surface pro 3 if that matters).
当我运行宏失败:设置VAVdoc = WordApp.Documents.Add
当它失败时,我收到以下错误:
When it fails I receive the following error:
我很失望,为什么它不会运行在我的设置,但它会我的同事任何想法?
I am at a loss as to why it will not run on my setup but it will on my co-workers. Any ideas?
任何帮助非常感谢。
更新:尝试后:
Any help greatly appreciated.Update: After trying:
再次没有o.quit我得到错误
again without the o.quit I get the error
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
推荐答案
这不是解决方案,而是调试提示。
This is not a solution but a debugging hint.
首先在任务管理器中关闭所有运行的Word应用程序。
At first in Task Manager close all running Word applications.
在工具 - 参考文献
取消选择所有引用 Microsoft Word ???对象库
。
In Tools - References
deselect all the references to Microsoft Word ??? Object Library
.
现在尝试以下宏。通过 F8
。但最后也通过 .Close
和 .Quit
。因为如果你没有在系统中收集到未使用的Word进程。
Now try the following Macro. Step through it with F8
. But at the end also through the .Close
and .Quit
. Because if you don't there were unused Word processes collected in the system.
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打开一个新的文件?如果是这样,那么晚期绑定工作。如果没有,哪些错误发生?
Does this work? Is there Word opened with a new document? If so, then late binding works. If not, which errors occurs?
现在在工具 - 参考文献
选择引用 Microsoft Word 14.0对象库
并尝试以下宏:
Now in Tools - References
select the reference to Microsoft Word 14.0 Object Library
and try the following Macro:
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打开一个新的文件?如果是这样,那么早期绑定也是如此。如果是这样,那么错误在别的地方。如果没有,但后期绑定工作,那么你必须将你的代码更改为晚期绑定。
Does this work also? Is there Word opened with a new document? If so, then early binding works also. If so, then the error is elsewhere. If not, but late binding works, then you have to change your code to late binding.
如果没有什么工作,请确保您可以手动启动Word应用程序? Word是否开始没有对话?还是显示哪些对话框?
If nothing works, sure you can start the Word application manually at all? Is Word starting without dialogs? Or which dialogs were displayed?
这篇关于Sub不会打开MS字(VBA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!