PDA

View Full Version : How to make this code to work in VB



nishant
03-08-2006, 10:49 PM
How to make this code work in VB. This code works fine when used as Macro but when I put the same code in VB it doesnot.Can anybody help me out?:banghead:



Option Explicit

Sub AddTable()

Dim Tbl As Table

Set Tbl = ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=4, NumColumns:=2)
Tbl.Range.Font.Bold = True
Tbl.Cell(1, 1).Range.Text = "AAA"
Tbl.Cell(1, 2).Range.Text = "111"

End Sub

matthewspatrick
03-08-2006, 11:25 PM
If you're trying to run this in VB, you will need to set a reference to the Word library, and probably set a variable to the Word.Application object:


Option Explicit

Sub AddTable()

Dim wdApp As Word.Application
Dim Tbl As Table

Set wdApp = New Word.Application
Set Tbl = wdApp.ActiveDocument.Tables.Add(Range:=Selection.Range, _
NumRows:=4, NumColumns:=2)
With Tbl
.Range.Font.Bold = True
.Cell(1, 1).Range.Text = "AAA"
.Cell(1, 2).Range.Text = "111"
End With

End Sub

Killian
03-09-2006, 02:18 AM
Hi nishant,

I think you may be having problems because you're using late-binding and don't have a reference set to the Word Object Model.

So if you use your existing code to create the Word app and document, the method posted above should work fine - but when you declare the variable "Tbl", it must be as an Object rather than a Word.TableDim Tbl as Object

untitled
07-03-2007, 05:06 PM
i found this helpful, thanks

Norie
07-04-2007, 12:21 PM
This means nothing in VB.

Range:=Selection.Range, _
NumRows:=4, NumColumns:=2
Like the other members are suggesting, I think anyway, you need to properly reference objects like Range.

fumei
07-04-2007, 01:39 PM
Actually, we don't know if the OP is using late-binding, or not - although it sort of looks more like it is early-binding.

In any case nishant, the verdict is in. You need to clarify and set proper references to make your code work. Perhaps we can help if you post the code you are using to make the document itself.