简体   繁体   中英

Sub won't open MS word (VBA)

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).

When I run the macro it fails on: Set VAVdoc = WordApp.Documents.Add

When it fails I receive the following error:

Run-time error '91': Object variable or With block variable not set.

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:

Set o = CreateObject("Word Application")

again without the o.quit I get the error

ActiveX component can't create object

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.

At first in Task Manager close all running Word applications.

In Tools - References deselect all the references to Microsoft Word ??? Object Library Microsoft Word ??? Object Library .

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

Does this work? Is there Word opened with a new document? If so, then late binding works. If not, which errors occurs?

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

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.

If nothing works, sure you can start the Word application manually at all? Is Word starting without dialogs? Or which dialogs were displayed?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM