Hi guys
I've tried to search this but I found nada.
How do I create a tables in Word from my VB app and after that insert some text/pictures into the table?
Regards,
Sveegaard
Printable View
Hi guys
I've tried to search this but I found nada.
How do I create a tables in Word from my VB app and after that insert some text/pictures into the table?
Regards,
Sveegaard
If you record a macro doing what you need and then stop recording, look at the module code it generates and you will see how word does it.
Doh, didn't think about that!
Well, I don't think about very often :D
Not thinking is the #1 cause of headaches say the 4 out of 5 doctors that were surveyed. :D
Hehe..
Hmm, but VBA and VB 6 'talk' too different - why can't I just copy&paste it or CreateObject? :S
How do you mean? Are you needing this to execute from a VB 6 program or ???
Yearh, I need to exe it in VB6
Ok, here are two examples for you. The first will paste in to the document whatever is on the clipboard. The second will create a new table.
VB Code:
Option Explicit 'Add a areference to MS Word xx.0 Object Library Private Sub Command1_Click() Dim oApp As Word.Application Dim oDoc As Word.Document Set oApp = New Word.Application oApp.Visible = True Set oDoc = oApp.Documents.Add oDoc.Activate oDoc.Select oApp.Selection.Paste 'Pastes whatever is on the clipboard. 'Do whatever. 'Clean up objects oDoc.SaveAs FileName:="C:\Command1.doc" oDoc.Saved = True oDoc.Close SaveChanges:=False Set oDoc = Nothing oApp.Quit False Set oApp = Nothing End Sub Private Sub Command2_Click() Dim oApp As Word.Application Dim oDoc As Word.Document Set oApp = New Word.Application oApp.Visible = True Set oDoc = oApp.Documents.Add oDoc.Activate oDoc.Select oDoc.Tables.Add Range:=Selection.Range, NumRows:=10, NumColumns:=5, DefaultTableBehavior:=wdWord9TableBehavior, _ AutoFitBehavior:=wdAutoFitWindow With Selection.Tables(1) If .Style <> "Table Grid" Then .Style = "Table Grid" End If .ApplyStyleHeadingRows = True .ApplyStyleLastRow = True .ApplyStyleFirstColumn = True .ApplyStyleLastColumn = True End With 'Do whatever. 'Clean up objects oDoc.SaveAs FileName:="C:\Command2.doc" oDoc.Saved = True oDoc.Close SaveChanges:=False Set oDoc = Nothing oApp.Quit False Set oApp = Nothing End Sub
Ok Thx
Now VB says 'Method 'Add' of object 'Tables' failed'
What version of Word are you running?
I added oDoc.Select to the example right after oDoc.Activate to help.
I'm running Word 2002
Did you try the updated code? I edited my last post in case you didnt see the change. ;)
Didn't notice that :D
But now it's complaining about 'with' :S
What is the complaint? :D
Something about variable object at the tables.add
You did see that the oDoc.Tables.Add runs across two lines of code with the line continuation character? So it hasnt yet created a table in a word doc yet?
Yup, noticed that.
No, it doesn't make a table and Word is trying to save a .dot :S
Your saying that with this code your app is trying to save a dot (Template)?
VB Code:
Option Explicit 'Add a areference to MS Word xx.0 Object Library Private Sub Command2_Click() Dim oApp As Word.Application Dim oDoc As Word.Document Set oApp = New Word.Application oApp.Visible = True Set oDoc = oApp.Documents.Add oDoc.Activate oDoc.Select oDoc.Tables.Add Range:=Selection.Range, NumRows:=10, NumColumns:=5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitWindow With Selection.Tables(1) If .Style <> "Table Grid" Then .Style = "Table Grid" End If .ApplyStyleHeadingRows = True .ApplyStyleLastRow = True .ApplyStyleFirstColumn = True .ApplyStyleLastColumn = True End With 'Do whatever. 'Clean up objects oDoc.SaveAs FileName:="C:\Command2.doc" oDoc.Saved = True oDoc.Close SaveChanges:=False Set oDoc = Nothing oApp.Quit False Set oApp = Nothing End Sub
Now it works :D
But when I open the document, it says that it has overwritten normal.dot - however, it hasn't :S
Hmm, got no clue how to put in text in a table.
Tried:
But no resultVB Code:
oDoc.Tables(1).Cells(1,1)="Test"
You must have something else going on since this does not reference normal.dot at all. Did you place this code in the normal.dot template or in your vb 6 program?
The code is placed in my VB6 program.
But more important is how to insert a text into a certain cell
:)VB Code:
oDoc.Tables(1).Cell(1, 1).Range.Text = "This is row 1 colum 1 in my first table."
Now it - once again - says that 'Add' is not optional, just as #9
This is really weird. The code is very basic and should not be giving us all these problems. Perhaps your Word or word reference is messed up. Could you try doing a Detect & Repair from the Help menu and then an Office Update?