E
EA
I have a problem with the code below, quite a strange one.
The sub procedure below is called from within Excel to Produce a stand alone
Word Document. This procedure is called x number of times depending on the
data. This codes runs fine on my PC, and did run without issue on my
colleague's PC.
However now on my colleagues PC I am getting a Run-time error '91' Object
varibale or with block variable not set. This is occuring on the line
marked "******". The very strange thing is the error does not occur on the
first or second pass through the code, but on the third, when the third
document is attempted to be created.
I am using exactly the same code on my PC without issue - can anyone explain
this...please [smile]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Private Sub Invoice(Client, CCY, MonthText)
Dim appWD As Word.Application
Dim TheError As Integer
Err.Number = 0
On Error GoTo WordNotOpen
Set appWD = GetObject(, "Word.application")
WordNotOpen:
If Err.Number = 429 Then
Set appWD = CreateObject("Word.application")
TheError = Err.Number
End If
' appWD.Visible = True
On Error GoTo 0
FileLocation = ThisWorkbook.Path
Dim WDobj As Word.Document
With appWD
Set WDobj = .Documents.Add(FileLocation & "\Brokerage.dot")
"******"
With WDobj
If CCY = "USD" Then
.Bookmarks("GBPText").Range.Delete
.Bookmarks("EURText").Range.Delete
ElseIf CCY = "GBP" Then
.Bookmarks("USDText").Range.Delete
.Bookmarks("EURText").Range.Delete
Else
.Bookmarks("GBPText").Range.Delete
.Bookmarks("USDText").Range.Delete
End If
.Fields.Update
.Fields.Unlink
.SaveAs FileLocation & "\Invoices\" & MonthText & "\" & CCY &
"\" & Client & ".doc"
.Close SaveChanges:=wdDoNotSaveChanges
End With
End With
Set WDobj = Nothing
If TheError = 429 Then appWD.Quit
Set appWD = Nothing
End Sub
The sub procedure below is called from within Excel to Produce a stand alone
Word Document. This procedure is called x number of times depending on the
data. This codes runs fine on my PC, and did run without issue on my
colleague's PC.
However now on my colleagues PC I am getting a Run-time error '91' Object
varibale or with block variable not set. This is occuring on the line
marked "******". The very strange thing is the error does not occur on the
first or second pass through the code, but on the third, when the third
document is attempted to be created.
I am using exactly the same code on my PC without issue - can anyone explain
this...please [smile]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Private Sub Invoice(Client, CCY, MonthText)
Dim appWD As Word.Application
Dim TheError As Integer
Err.Number = 0
On Error GoTo WordNotOpen
Set appWD = GetObject(, "Word.application")
WordNotOpen:
If Err.Number = 429 Then
Set appWD = CreateObject("Word.application")
TheError = Err.Number
End If
' appWD.Visible = True
On Error GoTo 0
FileLocation = ThisWorkbook.Path
Dim WDobj As Word.Document
With appWD
Set WDobj = .Documents.Add(FileLocation & "\Brokerage.dot")
"******"
With WDobj
If CCY = "USD" Then
.Bookmarks("GBPText").Range.Delete
.Bookmarks("EURText").Range.Delete
ElseIf CCY = "GBP" Then
.Bookmarks("USDText").Range.Delete
.Bookmarks("EURText").Range.Delete
Else
.Bookmarks("GBPText").Range.Delete
.Bookmarks("USDText").Range.Delete
End If
.Fields.Update
.Fields.Unlink
.SaveAs FileLocation & "\Invoices\" & MonthText & "\" & CCY &
"\" & Client & ".doc"
.Close SaveChanges:=wdDoNotSaveChanges
End With
End With
Set WDobj = Nothing
If TheError = 429 Then appWD.Quit
Set appWD = Nothing
End Sub