View Full Version : [SOLVED:] Error Message Trying to Create a Table - Word 2007
Chiroman
06-16-2015, 12:36 PM
Hello,
There must be a simple answer to this. Have looked here and the web, haven't seen the same problem and searching the error message was fruitless as well. My program creates a Word doc, where I'm trying to put the table, that gets filled with info from a userform. The error message is the following:
[-2147417851
Automation Error
The serve threw an exception]
The line of code causing it is:
objDoc.Tables.Add Range:=Selection.Range, NumRows:=3, NumColumns:=4, DefaultTableBehavior:=wdWord9TableBehavior
The following are declared in General Declarations
Public objWord
Public objDoc
Public objSelection
The document is created in a subroutine with the following code:
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Add("C:\Users\Username\Desktop\Template 1.dotx")
Set objSelection = objWord.Selection
I can use objSelection with no issues, but it's the objDoc that's causing the problems. If I substitute, "ActiveDocument" for, "objDoc" a table will get created on the doc that I'm using to write the program. I've tried using a subroutine to create the table but I get the same error message. Any help will be appreciated.
gmayor
06-16-2015, 09:29 PM
Set objSelection = objWord.Selection
should be
Set objSelection = objWord.Selection.Range
objDoc.Tables.Add Range:=Selection.Range
should be
objDoc.Tables.Add Range:=objSelection
If you are running the code from an application other than Word, Are you running this code from an application other than Word, change 'wdWord9TableBehavior' to '1'.
Thus change the lines below as shown
Set objSelection = objWord.Selection.Range
objDoc.Tables.Add Range:=objSelection, NumRows:=3, NumColumns:=4, DefaultTableBehavior:=1
However the quoted error usually relates to the use of the userform itself and when it is unloaded in relation to the code. Unload the form in the main code when you have finished with it and not in the userform code.
Chiroman
06-17-2015, 10:06 AM
Hi Graham, thanks for responding.
I made the changes you suggested and got this error message on the line with first objSelection:
Run-Time Error:
Object doesn't support this property or method
Everything is done through Word.
Not sure what you mean by, "Unload the form in the main code when you have finished with it and not in the userform code."
Thanks for your help.
gmaxey
06-17-2015, 07:45 PM
If everything is being done in Word to start with then why in the world are you creating and instance of Word and creating all the variant variables
Private Sub CommandButton1_Click()
Dim oDoc As Document
Dim oTbl As Table
Set oDoc = Documents.Add '("C:\Users\Username\Desktop\Template 1.dotx")
Set oTbl = oDoc.Tables.Add(Range:=Selection.Range, NumRows:=3, NumColumns:=4, DefaultTableBehavior:=wdWord9TableBehavior)
oTbl.Cell(1).Range.Text = Me.TextBox1.Text
lbl_Exit:
Exit Sub
End Sub
gmayor
06-17-2015, 08:48 PM
Greg
I took
My program creates a Word doc to mean that the document was being created from another application, otherwise of course your version is all that is required.
Chiroman
Change
Set objSelection = objWord.Selection
to
Set objSelection = objDoc.Range(0,0)
or
Set objSelection = objDoc.Range
objSelection.Collapse 1
Chiroman
06-20-2015, 11:41 AM
He Graham and Greg,
My last message didn't post when I labeled the issue, "Solved". It's solved b/c I'm using the active doc now instead of creating a new one. But now I have a new problem, I get get the insertion point beyond the table and trust me, I've tried everything and have looked everywere. Again, this has got to be a simple thing to do. Any ideas, I'd rather not make a new thread. Thanks guys for your help (also, the last code samples you gave didn't help my original issue either, therefore switched to activedoc).
gmayor
06-20-2015, 10:19 PM
If we can assume the userform is in Word then
Private Sub CommandButton1_Click()
Dim oDoc As Document
Dim oTbl As Table
Dim oRng As Range
Me.Hide
'The following template must exist at the named location
Set oDoc = Documents.Add("C:\Users\Username\Desktop\Template 1.dotx")
Set oRng = oDoc.Range
Set oTbl = oDoc.Tables.Add(Range:=oRng, _
NumRows:=3, _
NumColumns:=4, _
DefaultTableBehavior:=wdWord9TableBehavior)
oTbl.Range.Cells(1).Range.Text = Me.TextBox1.Text
'Move the start of the range to the end of the document
oRng.Collapse 0
oRng.Text = "This is after the table"
lbl_Exit:
Exit Sub
End Sub
You asked earlier what I meant by
"Unload the form in the main code when you have finished with it and not in the userform code."
I meant that I would call the userform from a macro that would do all the processing e.g.
Option Explicit
Sub ProcessDocument()
Dim oFrm As New UserForm1 'The name of the userform
Dim oDoc As Document
Dim oTbl As Table
Dim oRng As Range
With oFrm
.Show
If .Tag = 1 Then
'The following template must exist at the named location
Set oDoc = Documents.Add("C:\Users\Username\Desktop\Template 1.dotx")
Set oRng = oDoc.Range
Set oTbl = oDoc.Tables.Add(Range:=oRng, _
NumRows:=3, _
NumColumns:=4, _
DefaultTableBehavior:=wdWord9TableBehavior)
oTbl.Range.Cells(1).Range.Text = .TextBox1.Text
'Move the start of the range to the end of the document
oRng.Collapse 0
oRng.Text = "This is after the table"
End If
End With
Unload oFrm
lbl_Exit:
Set oFrm = Nothing
Set oDoc = Nothing
Set oTbl = Nothing
Set oRng = Nothing
Exit Sub
End Sub
and in the userform I would have simply
Option Explicit
Private Sub CommandButton1_Click() 'Continue
Me.Tag = 1
Me.Hide
lbl_Exit:
Exit Sub
End Sub
Private Sub CommandButton2_Click() 'Cancel
Me.Tag = 0
Me.Hide
lbl_Exit:
Exit Sub
End Sub
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.