S
Steve Barnett
I have an app that uses Word for report writing. On one machine I am getting
an "automation error - out of memory" whenever I use the Find mechanism to
locate some text for replacement and am looking for some ideas as to what it
might be.
I stripped down the app to the absolute minimum I need to test word
automation (reproduced below) and find that I consistently get this error on
the Find.Execute statement.
The machine having the problem is Windows/XP Pro SP1, Office/XP SP2. It has
lots of free disk space and plenty of memory. The temp folder isn't
particularly full of files. The app is written in VB6 SP5. This code works
fine on several dozen other machines (including another machine with the
above spec).
Anyone help?
Steve
The code I have been using for testing is:
Dim oWordApp As Word.Application
Dim oWordDoc As Word.Document
Public Sub TestWord()
Dim sFileName As String
Dim nChange As Integer
Dim sChangeFrom As String
Dim sChangeTo As String
Dim oRange As Word.Range
Dim bFound As Boolean
'*** Create instance of word and make it visible so I can
'*** see what's going on.
Set oWordApp = CreateObject("Word.application")
oWordApp.Visible = True
'*** Load the document we want to change
sFileName = App.Path & "\mytest.doc"
Set oWordDoc = oWordApp.Documents.Open(sFileName, False, False, False)
'*** Save with a new name so we don't overwrite the original
sFileName = App.Path & "\MySaved.doc"
oWordDoc.SaveAs sFileName
'*** Find and change the stuff we want changed
For nChange = 1 To 10
sChangeFrom = "%n" & Trim$(Str$(nChange)) & "%"
sChangeTo = "Field" & Trim$(Str$(nChange))
bFound = True
'*** Looks for %n1% and replaces with Field1 (same for 2 thru 10)
Do While bFound
Set oRange = oWordDoc.Range
With oRange.Find
.ClearFormatting '************************This works
bFound = .Execute(sChangeFrom, True, False, False, False)
'****** Fails
If bFound Then
oRange.Text = sChangeTo
End If
End With
Loop
Next
'*** Save the changes
oWordDoc.Save
'*** Close Word
oWordApp.Quit
set oWordApp = nothing
End Sub
an "automation error - out of memory" whenever I use the Find mechanism to
locate some text for replacement and am looking for some ideas as to what it
might be.
I stripped down the app to the absolute minimum I need to test word
automation (reproduced below) and find that I consistently get this error on
the Find.Execute statement.
The machine having the problem is Windows/XP Pro SP1, Office/XP SP2. It has
lots of free disk space and plenty of memory. The temp folder isn't
particularly full of files. The app is written in VB6 SP5. This code works
fine on several dozen other machines (including another machine with the
above spec).
Anyone help?
Steve
The code I have been using for testing is:
Dim oWordApp As Word.Application
Dim oWordDoc As Word.Document
Public Sub TestWord()
Dim sFileName As String
Dim nChange As Integer
Dim sChangeFrom As String
Dim sChangeTo As String
Dim oRange As Word.Range
Dim bFound As Boolean
'*** Create instance of word and make it visible so I can
'*** see what's going on.
Set oWordApp = CreateObject("Word.application")
oWordApp.Visible = True
'*** Load the document we want to change
sFileName = App.Path & "\mytest.doc"
Set oWordDoc = oWordApp.Documents.Open(sFileName, False, False, False)
'*** Save with a new name so we don't overwrite the original
sFileName = App.Path & "\MySaved.doc"
oWordDoc.SaveAs sFileName
'*** Find and change the stuff we want changed
For nChange = 1 To 10
sChangeFrom = "%n" & Trim$(Str$(nChange)) & "%"
sChangeTo = "Field" & Trim$(Str$(nChange))
bFound = True
'*** Looks for %n1% and replaces with Field1 (same for 2 thru 10)
Do While bFound
Set oRange = oWordDoc.Range
With oRange.Find
.ClearFormatting '************************This works
bFound = .Execute(sChangeFrom, True, False, False, False)
'****** Fails
If bFound Then
oRange.Text = sChangeTo
End If
End With
Loop
Next
'*** Save the changes
oWordDoc.Save
'*** Close Word
oWordApp.Quit
set oWordApp = nothing
End Sub