memory could not be read error using tables in MS Word 2003

S

Sam

Hi,
I am trying to create a word document programatically in vb.net 2003.The
program would create a blank word document, insert a table and populate it
with some values.
my program does successfully create the word document, inserts table and
populates it.The probelm I am running into is that just before the program
ends I get the following error message:
Application Error: The instruction at "0x5b261e4e" referenced memory at
"0x0454e2c8". The memory could not be "read" Click on OK to terminate the
program.

I dont get this error message if I insert sentences instead of table.

Here's the code snippet that inserts table:

With currentSelection
Dim sTableRng As Word.Range = sWord.Selection.Range

sDoc.Tables.Add(sTableRng, sRowCount, sColCount)

'Add table row headings
With sDoc.Tables.Item(1)
'Format the headings
.Cell(1, 1).Range.Font.Bold = True
.Cell(1, 2).Range.Font.Bold = True
.Cell(1, 3).Range.Font.Bold = True

'Enter heading names
.Cell(1, 1).Range.Text = "Item Name"
.Cell(1, 2).Range.Text = "Type"
.Cell(1, 3).Range.Text = "Width"
End With

'Add values into the table
'Declare variable to iterate through rows of table
Dim sTI As Integer

'Enter field values
For sTI = 2 To sRowCount + 1
With sDoc.Tables.Item(1)
'Format the headings
.Cell(sTI, 1).Range.Font.Bold = False
.Cell(sTI, 2).Range.Font.Bold = False
.Cell(sTI, 3).Range.Font.Bold = False

'Enter heading names
.Cell(sTI, 1).Range.Text = "Item1"
.Cell(sTI, 2).Range.Text = "Type1"
.Cell(sTI, 3).Range.Text = "Width1"
End With
Next
sTableRng = Nothing
End With

I tried the releaseCOMobject method at the end to close any open
connections, but that did not help either.

Any help is greatly appreciated.

Thanks

Sam
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?U2Ft?=,

First of all, put your code in Try...Catch blocks so that you can track down
what line(s) are responsible for the problem.

Is this your exact code, or just a simplified rendition? There's a common
error with Word automation if you do a lot of formatting work. The
scratch/temp/Undo files can't keep up. In the Word interface we get a message
about the formatting being too complex, and since the problem most often
comes up when generating and formatting tables, I have to wonder if that
could be what this is.

If you search the msdn website on my name you should turn up an article about
efficiently generating tables in Word. See if constructing your code along
those lines helps at all. And also note how to work with Word's objects
(Table and Row).
I am trying to create a word document programatically in vb.net 2003.The
program would create a blank word document, insert a table and populate it
with some values.
my program does successfully create the word document, inserts table and
populates it.The probelm I am running into is that just before the program
ends I get the following error message:
Application Error: The instruction at "0x5b261e4e" referenced memory at
"0x0454e2c8". The memory could not be "read" Click on OK to terminate the
program.

I dont get this error message if I insert sentences instead of table.

Here's the code snippet that inserts table:

With currentSelection
Dim sTableRng As Word.Range = sWord.Selection.Range

sDoc.Tables.Add(sTableRng, sRowCount, sColCount)

'Add table row headings
With sDoc.Tables.Item(1)
'Format the headings
.Cell(1, 1).Range.Font.Bold = True
.Cell(1, 2).Range.Font.Bold = True
.Cell(1, 3).Range.Font.Bold = True

'Enter heading names
.Cell(1, 1).Range.Text = "Item Name"
.Cell(1, 2).Range.Text = "Type"
.Cell(1, 3).Range.Text = "Width"
End With

'Add values into the table
'Declare variable to iterate through rows of table
Dim sTI As Integer

'Enter field values
For sTI = 2 To sRowCount + 1
With sDoc.Tables.Item(1)
'Format the headings
.Cell(sTI, 1).Range.Font.Bold = False
.Cell(sTI, 2).Range.Font.Bold = False
.Cell(sTI, 3).Range.Font.Bold = False

'Enter heading names
.Cell(sTI, 1).Range.Text = "Item1"
.Cell(sTI, 2).Range.Text = "Type1"
.Cell(sTI, 3).Range.Text = "Width1"
End With
Next
sTableRng = Nothing
End With

I tried the releaseCOMobject method at the end to close any open
connections, but that did not help either.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
S

Sam

Hi Cindy,
Thanks for your response.
I did use Try...Catch block in the code. This error occurs at the "End Sub"
statement of the procedure(after the program exits out of Try...Catch block
successfully).This is what makes it really hard to figure the exact cause of
the error.

Basically I am trying to create a word document which loops through all
files in a folder and inserts a table for each file. The table
specifics(rows,cols) are obtained from the file.The information about the
file is stored in the table.

Here's the code that creates the word document and the table (for clarity I
am skipping the code that loops through the folder):

Dim sWord As New Word.ApplicationClass
Dim sDoc As Word.Document
Dim sApp As Word.Application
Dim userOvertype As Boolean

'Start process of creating report document
sApp = sWord.Application
sDoc = sWord.Documents.Add()
sDoc.SaveAs(sReportFile)

Dim currentSelection As Word.Selection = sApp.Selection

' Store the user's current Overtype selection
userOvertype = sApp.Options.Overtype

' Make sure Overtype is turned off.
If sApp.Options.Overtype Then
sApp.Options.Overtype = False
End If

'Add report title
With currentSelection
.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
.Font.Size = 16
.Font.Bold = True
.Font.Italic = True
.TypeText(strReportTitle)
.TypeParagraph()
.TypeParagraph()
End With

With currentSelection
Dim sTableRng As Word.Range = sWord.Selection.Range

sDoc.Tables.Add(sTableRng, sRowCount, sColCount)

'Add table row headings
With sDoc.Tables.Item(1)
'Format the headings
.Cell(1, 1).Range.Font.Bold = True
.Cell(1, 2).Range.Font.Bold = True
.Cell(1, 3).Range.Font.Bold = True

'Enter heading names
.Cell(1, 1).Range.Text = "Item Name"
.Cell(1, 2).Range.Text = "Type"
.Cell(1, 3).Range.Text = "Width"
End With

'Add file info to the table
'Declare variable to iterate through rows of table
Dim sTI As Integer

'Enter field values
For sTI = 2 To sRowCount + 1
With sDoc.Tables.Item(1)
'Format the headings
.Cell(sTI, 1).Range.Font.Bold = False
.Cell(sTI, 2).Range.Font.Bold = False
.Cell(sTI, 3).Range.Font.Bold = False

'Enter heading names
.Cell(sTI, 1).Range.Text = "Item1"
.Cell(sTI, 2).Range.Text = "Type1"
.Cell(sTI, 3).Range.Text = "Width1"
End With
Next
sTableRng = Nothing
End With

currentSelection.Tables(1).Select()
currentSelection.EndKey(Word.WdUnits.wdLine)
currentSelection.MoveRight(Word.WdUnits.wdCharacter, 1)
currentSelection.MoveDown(Word.WdUnits.wdLine, sRowCount)
currentSelection.TypeParagraph()
currentSelection = Nothing

''Release COM objects
ReleaseComObject(sDoc.Tables(1).Columns)
ReleaseComObject(sDoc.Tables(1).Rows)
ReleaseComObject(sDoc.Tables(1))

sApp.ActiveWindow.ActivePane.View.Zoom.Percentage = 100

' Restore the user's Overtype selection
sApp.Options.Overtype = userOvertype

'Save changes and close the document
sDoc.Save()
sDoc.Close()

sWord = Nothing
sApp = Nothing

ReleaseComObject(sDoc)

I have debogged individual parts of the code as well and I get the error
only when I use the table part of the code.

Thanks again for your help.

Sam
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?U2Ft?=,
Dim sWord As New Word.ApplicationClass
Dim sDoc As Word.Document
Dim sApp As Word.Application
Dim userOvertype As Boolean

'Start process of creating report document
sApp = sWord.Application
Why are you doing this? I should think this should be
enough:

dim sApp as New Word.Application

Beyond that, there's not much point in discussing your code
until you've looked at the article and tightened things up
a bit. Create a Table object and use that. As long as all
the cells in a row are formatted the same, apply formatting
to the Row.Range. Use a Range instead of the Selection
object where ever possible.

There's no need to process the table within a "With
currentSelection" structure.

You're mixing use of the application objects sWord and
sApp. This is probably not helping the situation; could
even be the cause of the problem. You should only have one
application object for the instance you're automating.

You should not need to release COM objects for things
you've never used directly. If you feel you must Release
sDoc, do so before setting the object variable (the
application) from which it was created to Nothing.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update
Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any
follow question or reply in the newsgroup and not by e-mail
:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top