PDA

View Full Version : Solved: sos-bookmark table



sangeeta
05-23-2005, 10:41 PM
hi all!
i need to extract data from an excl sheet and put it into a table in word.i have been able to do that using the table index but the problem is that the doc itself is organised using a table so that it is not possible for me to get the table index reliably.
is there any way to name a table and use this name as in tables("tablename")
instead of tables(index number)?
alternatively,can i bookmark a table and then use this bookmark to insert data into the table although i m less keen on this solution.
hope i have made my question clear!
thanks in advance

Howard Kaikow
05-23-2005, 11:07 PM
hi all!
i need to extract data from an excl sheet and put it into a table in word.i have been able to do that using the table index but the problem is that the doc itself is organised using a table so that it is not possible for me to get the table index reliably.
is there any way to name a table and use this name as in tables("tablename")
instead of tables(index number)?
alternatively,can i bookmark a table and then use this bookmark to insert data into the table although i m less keen on this solution.
hope i have made my question clear!
thanks in advance

if there's only 1 table in the document, the table index does not change.

if there's more than one table, create a Table object for each table of interest and work via the Table object for the table.

check the Object Browser for info on the Table object.

sangeeta
05-23-2005, 11:25 PM
thanks a lot but i m still not clear on how to do that.could you please help me with an example?

sandam
05-24-2005, 01:28 AM
'n = an integer between 1 and the total number of tables in the document

With Activedocument.Content.Tables(n)

.Cell(1,1).Range.Text = "This is the first cell in the nth table"

End With

TonyJollans
05-24-2005, 01:49 AM
Tables in Documents do not have names; they can only be referenced by index number and they are not, themselves, objects which you can reliably identify. If you cannot guarantee enough about the document to be sure, using a bookmark is by far your best option.

If you have a bookmark somewhere in the table you can use
ActiveDocument.Bookmarks("BookmarkName").Range.Tables(1)
to reference it

sangeeta
05-24-2005, 04:14 AM
I don't think i made myself clear.The problem is that there are so many tables in my doc(in fact the entire doc is within one big table,there are tables within the table) that using the index number is cumbersome and unreliable.That's the reason i thought there should be some other way of accessing the tables,say by using a name or bookmarking them in some way.
sorry for all the trouble and thanks in advance.

TonyJollans
05-24-2005, 04:59 AM
I'm quite prepared to believe that using the index number is unreliable, but cumbersome!

Anyway, as I said, you can't identify the table in any direct way and therefore you will have to associate something else with it, which you can identify and a bookmark is the simplest. What is your problem with a bookmark?

MOS MASTER
05-24-2005, 09:30 AM
Hi and Welcome to VBAX! :hi:

Perhaps you can post a little sample of your table document so we can see if these are normal tables or nested ones. And perhaps give some more clues on what you're trying to do so we can be of more assistance?

Nested or not I'd go for shure with the bookmark (or any other reference) method of Tony! (That's most reliable one I know of)

If you say your table is so complicated then why do not take another approach:
* Have your Excel document set up as how you'd like your data to look
* Possibly name the range for easy retrievel
* Copy this range and paste it in to Word as table...(This could save you time depending on the full range of your question)

You could write some code to clean or alter the table afterwards...but don't know enough detail now to answer that!

Enjoy! :whistle:

Howard Kaikow
05-24-2005, 12:38 PM
thanks a lot but i m still not clear on how to do that.could you please help me with an example?

I was hinting at doing somethong loke the following:

Dim myTable as Word.Table

' If you want to work with the 12th table in the document.
Set myTable = activeDocument.Content.Tables(12)

Then use myTable to refer to the table where you may have previous used, say:

Use:

myTable.Whatever

instead of

ActiveDocument.Content.Tables(12).Whatever

AFAIK, the myTable object will always point to the correct table, even if the table's index changes.

sangeeta
05-24-2005, 10:17 PM
thanks a ton! i guess i should be able to take it from here.will get back to u all in case of any problem.
by the way,i m going with the bookmark solution.
thanks for your time!

sangeeta
05-25-2005, 02:47 AM
hi all! i bookmarked a cell in my table and i m trying to run the following code from excel sheet:
Set WordApp = CreateObject("Word.Application")
Set WordDoc = GetObject("C:\Documents and Settings\sb008078\Desktop\AUTOMATION\Automating word\automation.doc")
With WordApp
.Visible = True
.WindowState = wdWindowStateMaximize
End With
With WordDoc
Selection.GoTo What:=wdGoToBookmark, Name:="bk1"
Selection.Find.ClearFormatting
With Selection.Find
.text = ""
.Replacement.text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
While Rindex < 11
Rindex = Rindex + 1
Cindex = 0
Selection.MoveDown Unit:=wdLine
While Cindex < 11
Selection.TypeText text:=Worksheets("Sheet1").Cells(Rindex, Cindex).Value
Selection.MoveRight Unit:=wdCell
Wend
Wend
End With

but i m getting a run time error 438:object does not support this method or property with the following line highlighted:
Selection.GoTo What:=wdGoToBookmark, Name:="bk1"
what i can't understand is why is it happening?
i have used this property on selection object in word doc and it works fine.

help please! i need to get this done.....

TonyJollans
05-25-2005, 05:28 AM
The reason for your error is that you are using the Excel Selection, instead of the Word one. Inside your With you should use .Selection, HOWEVER the Word Selection does not belong to the Document so you should be using With WordApp.

Although it is unlikely to go wrong, the way you open your Document with GetObject is not guaranteed to use the instance of Word you have created with the CreateObject. As you won't be using the WordDoc object after you make the change above I would suggest doing it this way

Set WordApp = CreateObject("Word.Application")
With WordApp
.Documents.Open "C:\Documents and Settings\sb008078\Desktop\AUTOMATION\Automating word\automation.doc"
.Visible = True
.WindowState = wdWindowStateMaximize
.Selection.GoTo What:=wdGoToBookmark, Name:="bk1"
.Selection.Find.ClearFormatting
With .Selection.Find
.text = ""
.Replacement.text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
While Rindex < 11
Rindex = Rindex + 1
Cindex = 0
.Selection.MoveDown Unit:=wdLine
While Cindex < 11
.Selection.TypeText text:=Worksheets("Sheet1").Cells(Rindex, Cindex).Value
.Selection.MoveRight Unit:=wdCell
Wend
Wend
End With

As a side note - if you enclose your code in VBA .. /VBA tags it is much easier to read.

I don't have your document so can't be sure what you have, but I note as I write this that you don't execute the Find, so what is it doing? I would also suggest that you try and avoid using the Selection object at all. If you have a reference to your table you can use either the Cell method or the Rows and Columns properties.

MOS MASTER
05-25-2005, 10:48 AM
Hi, :yes

Like Tony mentioned there are better ways of coding this using a range and the proper table navigation.

I notice that you're using Late Binding (CreateObject, Dim as Object etc..)
Is there a specific reason for this? (Version problems..etc..)

If you would use Early Binding from Excel (Set reference to Word Object library) then you could use Intellisense to help you coding from Excel and Execution time will be faster.

Would you like a version of your code in early binding? :whistle:

sangeeta
05-25-2005, 07:27 PM
Thanks a lot! yes, the early bindng version would be of great help.
thanks tony!

MOS MASTER
05-26-2005, 10:29 AM
Hi, :yes

Sure here it comes.

Because I don't know exactly how you want your table filled I'll make this an example of how to fill a Word table from excel using an existent table.

Before you run it check the reference in the VBE. (Read code remark)

Option Explicit

Sub FillWordTable()
'Before Run Go to Tools/Reference and check the right reference to:
'Microsoft Word 11.0 Object Library (if you have xp it will be 10.0 / 2000 9.0 etc...)

Dim oApp As New Word.Application
Dim oDoc As Word.Document
Dim oTable As Word.Table
Dim sPath As String, iCol As Integer, iRow As Integer

sPath = ThisWorkbook.Path & _
Application.PathSeparator & "Test.doc"
Application.ScreenUpdating = False

With oApp
Set oDoc = .Documents.Open(Filename:=sPath)
Set oTable = oDoc.Bookmarks("bk1").Range.Tables(1)

For iCol = 1 To oTable.Columns.Count

For iRow = 1 To oTable.Rows.Count

oTable.Cell(iRow, iCol).Range.Text = _
Application.Worksheets("Sheet1").Cells(iRow, iCol).Value

Next

Next

.Visible = True
.Activate
.WindowState = wdWindowStateMaximize
End With
Set oTable = Nothing
Set oDoc = Nothing
Set oApp = Nothing
End Sub


See Attachment

The code is setup in a way you can run it as is. (no need to change the path just run via the button)

Enjoy! :whistle:

sangeeta
05-26-2005, 09:13 PM
thanks a lot !surely this will help a lot.
thanks agian.we can consider this thread solved.

MOS MASTER
05-27-2005, 09:26 AM
You're welcome..glad we could help! :beerchug: