Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.tables
Sam Sam is offline
external usenet poster
 
Posts: 68
Default memory could not be read error using tables in MS Word 2003

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
  #2   Report Post  
Posted to microsoft.public.word.tables
Cindy M -WordMVP- Cindy M  -WordMVP- is offline
external usenet poster
 
Posts: 370
Default memory could not be read error using tables in MS Word 2003

Hi ?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 :-)

  #3   Report Post  
Posted to microsoft.public.word.tables
Sam Sam is offline
external usenet poster
 
Posts: 68
Default memory could not be read error using tables in MS Word 2003

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.wdCharacte r, 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



"Cindy M -WordMVP-" wrote:

Hi ?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 :-)


  #4   Report Post  
Posted to microsoft.public.word.tables
Cindy M -WordMVP- Cindy M  -WordMVP- is offline
external usenet poster
 
Posts: 370
Default memory could not be read error using tables in MS Word 2003

Hi ?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
:-)

Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting WordPerfect 12 files to Word 2003 Curious New Users 4 May 19th 23 02:48 PM
Why dont MS just f**king re-write Word from scratch? Its dogsh*t Word Hater Microsoft Word Help 33 May 5th 23 02:52 PM
take yet another lesson from wordperfect "reveal codes" wordperfect is superior Microsoft Word Help 5 May 11th 09 07:58 PM
hard space between words. Sandy L Microsoft Word Help 7 May 5th 06 08:25 PM
Is there a way to "join" tables in Word 2003? Julian Turner Tables 3 October 28th 04 06:51 PM


All times are GMT +1. The time now is 11:59 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"