Using word automation from VB.NET

E

elziko

I'm using the following code in VB.NET to try and create a table in word:

Dim objDoc As Object
Dim objWordApp As Object
Try
objWordApp = GetObject(, "Word.Application")
Catch e As Exception
objWordApp = CreateObject("Word.Application")
objDoc = objWordApp.Documents.Add
End Try
objWordApp.DisplayAlerts = False
If objWordApp.Documents.Count < 1 Then
objWordApp.Documents.Add()
End If
objDoc = objWordApp.Documents(1)
'------------------------------------------------------------------

Dim rows As Integer = sourceTable.Rows.Count
Dim columns As Integer = sourceTable.Columns.Count
Dim qry As Object
Dim rTable As Object = objWordApp.ActiveDocument.Content
qry = objDoc.Tables.Add(rTable, rows, columns) '***
For c As Integer = 1 To columns
For r As Integer = 1 To rows
Dim rngData As Object = qry.Cell(r, c).Range
rngData.Text = sourceTable.Rows(r - 1).Item(c - 1)
Next
Next

'------------------------------------------------------------------
objWordApp.Visible = True
objWordApp.WindowState = 0
objWordApp.Activate()
objDoc = Nothing
objWordApp = Nothing

However, at runtime I get an exception telling me that the range
cannot be deleted on the line shown with '***. Do you have any idea why this
may be happening?
 

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