How can I "address" nested tables in Word using VBScript?
-
2012년 5월 3일 목요일 오후 6:57
I s it possible to reference a nested table in VBScript? I know it's possible in VBA, but there are so many things VBA can do that VBScript wasn't designed to do, I thought I'd ask the question if it IS possible, how?
Thanks,
DAS
모든 응답
-
2012년 5월 3일 목요일 오후 7:06중재자
Hi,
I would create a working prototype in VBA and then try translating to VBScript. At that point, you can start asking questions if you run into problems.
Bill
-
2012년 5월 3일 목요일 오후 7:51
Well, turns out I already tried that route. In fact I never dreamed of nesting them in the first place until I saw it was doable in VBS. Alas, I've put in enough time trying to convert the working VBA that I reached the point of realizing it might not be possible, thus the question.
There is a short way out I'd prefer not to take, that is setting up the 10 (what are now nested) tables and playing for hours and hours to get it working like I did the first go-round. First one was Avery labels, now I'm trying to do business cards we use for a different purpose.
DAS
-
2012년 5월 3일 목요일 오후 8:59
one way if you know where the table is:
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()
objdoc.Tables.Add objdoc.Range(),1,3 'add outer table to new doc
objdoc.Tables(1).Cell(1,1).Tables.Add objdoc.Tables(1).Cell(1,1).Range,1,3 'add a new table in cell 1,1 of outer table
objdoc.Tables(1).Cell(1,2).Range.Text="some text" 'add text to outer table
objdoc.Tables(1).Cell(1,1).Tables(1).Cell(1,2).Range.Text="other text" 'add text to inner table
-
2012년 5월 3일 목요일 오후 10:01
Just a small note. When you do a tane 'add' the tabel is returned as an object. Thisis for convenince of having a refererence toteh object just created.
This:
objdoc.Tables(1).Cell(1,1).Tables.Add objdoc.Tables(1).Cell(1,1).Range,1,3Can be done this way:
Set table1 = objdoc.Tables(1).Cell(1,1).Tables.Add(objdoc.Tables(1).Cell(1,1).Range,1,3)
Now you have table1 to use to refer to the new table whenever you need it.
This design pattern repeats itself everywhere in Microsoft objects.
Consider also using named ranges to find things.
¯\_(ツ)_/¯
-
2012년 5월 3일 목요일 오후 10:37
Demo:
objWord.Visible=True Set objDoc = objWord.Documents.Add() Set t1 = objdoc.Tables.Add( objdoc.Range(),3,3) t1.Borders.Enable=True Set t2 = objdoc.Tables(1).Cell(1,1).Tables.Add( t1.Cell(1,1).Range,2,3) t2.Borders.Enable=True t2.Cell(1,2).Shading.BackgroundPatternColor=RGB(200,100,100) t1.Cell(1,2).Range.Text="Table 1 Text" t2.Cell(1,1).Range.Text="Table 2 Text" For Each t In objDoc.Tables t.Range.Bold=True Next
¯\_(ツ)_/¯
-
2012년 5월 3일 목요일 오후 11:02
How to use a bookmark to find a table. (named tables)
Set word = CreateObject("Word.Application") word.Visible=True Set doc = word.Documents.Add() Set t1 = doc.Tables.Add( doc.Range(),3,3) ' ' add a bookmark to the table. doc.Bookmarks.Add "table1",t1.Range t1.Borders.Enable=True Set t2 = doc.Tables(1).Cell(1,1).Tables.Add( t1.Cell(1,1).Range,2,3) t2.Borders.Enable=True t2.Cell(1,2).Shading.BackgroundPatternColor=RGB(200,100,100) t1.Cell(1,2).Range.Text="Table 1 Text" t2.Cell(1,1).Range.Text="Table 2 Text" ' ' address table by name doc.Bookmarks("table1").Range.Tables(1).Cell(2,1).Range.Text="New Text"¯\_(ツ)_/¯
-
2012년 5월 4일 금요일 오전 12:40
Well, good suggestions, but I broke with tradition and tried to make a short post. I doing so I left out critical details that make the "add" options less palitable.
The thing the outside table does for me is eliminate large positioning issues. The bounds of those cells are a perfect match for the business card stock.
The inside tables carry a corporate graphic to the left, and the table to the right is dimensioned as 1 column 2.5" wide by 11 rows tall, and the height of these rows
varies in order to provide for large bold text in some lines, medium bold text in another, and "standard" text in others, while a few are set to tiny font size in order to provide much needed line spacing in between the elements. The goal was to match a provided example.So then, all of these tables and cells are crafted to provide near absolute positioning, something I don't think I can achieve with any of the methods mentioned.
I would like to thank you all for taking the time, the examples have been archived for future reference, but I'm going to get started on the array of individual tables (sans outer table) I had to use for the similar yet different implementation I had before.
Thanks for your time, and I apologize for not making my traditional detailed post at the beginning, many of you have the right to complain this could seem like a "moving target".
DAS
-
2012년 5월 4일 금요일 오전 3:08
Well I guess you have created an impossible Word document that cannot be read by and script or devide.
The rest of use see, to be able to do ti. YOu seem to think it is impossible. Why?
Just set a bookmarks on your tables and retrieve them in code. I showed you how to do that. It is very explixit in the last example I posted.
I,very often, create template with named ranges and tables in them. I just retrive the table and address it like a spreadsheet.
This retrieves a named table:
Set tbl1 = doc.Bookmarks("table1")
¯\_(ツ)_/¯
-
2012년 5월 4일 금요일 오전 11:27
I'll give Bookmarking a try, thanks JRV.
DAS
-
2012년 5월 4일 금요일 오후 1:27
JRV, I'm simply not getting this whole thing.
I went back into my template and dropped the following using the bookmarking tool from the Insert tab:
The 5Row x 2Col outer table I bookmarked as T0 (zero)
Then I bookmarked the first Inside table (inside T0.cell(1,1)) as T1, going down that col to T5, the inside table at upper left (inside T0.Cell(1,2)) is T6 ending at T10 in that column of the outside table.
I'm having a hard time referencing any of the tables by name, so if you don't mind, can you show me the code to reference T6 for example?
Something is simply not gelling for me, it's obvious I don't understand the basics. I aim to, but I'm missing core element in understanding.
This does not work:
Set Itable = objdoc.Bookmarks("T6")
ITable.Cell(4, 1).Range.Font.Size = 11
ITable.Cell(4, 1).Range.Font.Name = "Arial Bold"
ITable.Cell(4, 1).Range.Text = UserNameI get an error at the first (font) reference, "Error: Object doesn't support this property or method: 'ITable.Cell'"
DAS
-
2012년 5월 4일 금요일 오후 1:48
Win7
Put together a short piece of code that shows the problem. I wil also need the word template. You cannot do this by editing teh template directly. YOu must use teh template to 'Add' a documents so that you have a new document.
¯\_(ツ)_/¯
-
2012년 5월 4일 금요일 오후 2:15So this is inaccurate?
"I,very often, create template with named ranges and tables in them. I just retrive the table and address it like a spreadsheet."
I had completely given up until I read that, because that's of course what I want, a pre-cast template fully set up in every detail that I can just drop data into the cells.
If I can't access the elements of a nested table, how can I use them as a template to create another document with non-stale references? (I'm assuming the act of adding makes the reference visible to the code that created it).
OK, one two-part question should let me know if any further pursuit is warranted:
Are you saying the "template" needs to be the "outer" table only (in my case the 5row, 2col full page table), and the inner tables need to be generated by code in order to have handles available?
If that's the case, then I have to learn how to set up row height, row width, and how to position my graphic. Ughh.
DAS
-
2012년 5월 4일 금요일 오후 2:18중재자
Hi,
I think this has gotten off-topic for a general scripting forum - seems more like a "Word for Developers" topic.
Bill
-
2012년 5월 4일 금요일 오후 2:25
Hi,
I think this has gotten off-topic for a general scripting forum - seems more like a "Word for Developers" topic.
Bill
Well - maybe - but it is a pure VBSAcript issue.
Yes MSWord VBA Pro would be a better palce to start but I checked with those guys nand they don't seem to knwo much.
The Developer Word guys only knwo Net Interop these days which will work to some degree in PowerSHell but not at all in VBScript. in general VBA guys don't know VScript.
Don't you think this is a kind of "Catch 22" situation?
¯\_(ツ)_/¯
-
2012년 5월 4일 금요일 오후 2:28중재자
Asking questions about whether you can access nested tables in Word sounds to me like a Word development question, not a general Windows scripting question, whether it's done in VBScript or not. I think my original advice is right: Get it working in VBA first; then you can attempt a VBScript translation. At that point we can deal with VBScript specifics.
Bill
-
2012년 5월 4일 금요일 오후 2:37
So this is inaccurate?
"I,very often, create template with named ranges and tables in them. I just retrive the table and address it like a spreadsheet."
I had completely given up until I read that, because that's of course what I want, a pre-cast template fully set up in every detail that I can just drop data into the cells.
If I can't access the elements of a nested table, how can I use them as a template to create another document with non-stale references? (I'm assuming the act of adding makes the reference visible to the code that created it).
OK, one two-part question should let me know if any further pursuit is warranted:
Are you saying the "template" needs to be the "outer" table only (in my case the 5row, 2col full page table), and the inner tables need to be generated by code in order to have handles available?
If that's the case, then I have to learn how to set up row height, row width, and how to position my graphic. Ughh.
DAS
No -'template' is a Word template. A DOT or DOTX, DOTM file.
Ther are no handles. These are bookmarks. The bookmark names a range of Cells. The range of Cells mape teh CElls in the table.
You can approach it a different way. add a Bookmark the the Cell that is in the table. Just get and update through the bookmark and don't worry about it being in a table.
A bookmark names a range of Cells which can be just one cell.
objdoc.Bookmarks("Firstname").Range.Text = "John"
For wwhat you are doing this would be the easiest.
Another approach is to defin customproperties and then assign them in the word template. Create a new document using the template and then assign the properties. They will show up where you have placed the fields.
¯\_(ツ)_/¯
-
2012년 5월 4일 금요일 오후 2:55
The following works perfectly everytime. My template has bookmarks in cells in teh innertable which are filled in by this script.
Set word = CreateObject("Word.Application") word.Visible=True Set doc = word.Documents.Add("e:\test2\testbm.dotx") doc.Bookmarks("Firstname").Range.Text="John" doc.Bookmarks("Lastname").Range.Text="Smith"This is probaby easier than what you were trying as it ignores the tables abd is much easier to code.
I have a more extensive example of using a template to create signatures. We can change the template any time. The code wil alwyas scan for all bookmarks and update only those that it finds. The code is mapped to WMI, Active Directory and the environment. The template shooses which items it wants by naming the bookmark tot be the asme as the external variable. The template can use a table/tables or any other method for layout becuse the script only references bookmarks.
The other method that is usable is to place an AD query into the template as a macro. When the template opens it file in teh correct values and we only need save teh signatures. Tis version is very short. I did it for a client. I don't think they are using that version so I will try to get permisisoins to use it as the code I wrote belongs to the client.
I may rewrite some and place in the repositroy as an example. Maybe later.
¯\_(ツ)_/¯
- 답변으로 표시됨 Win7Tester 2012년 5월 4일 금요일 오후 6:19
-
2012년 5월 4일 금요일 오후 3:06
Thanks JRV, I'll check into it.
Sorry Bill if this is walking the line between sys admin and Word power user stuff, but I have deployment teams to support, so I try and work with what I know instead of throwing my hands in the air.
DAS
-
2012년 5월 4일 금요일 오후 3:12중재자
Hi DAS,
No problem -- just wanted to point out that you're really asking more of a Word development question than a general VBScript question.
Bill
-
2012년 5월 4일 금요일 오후 6:19
JRV:
Thank you for hanging in there for me, the individual cell bookmarking did the trick in grand fashion.
I sat there dumbfounded as the cells got populated...
Again, thanks for your effort, I'm good to go, topic closed.
DAS
-
2012년 5월 4일 금요일 오후 6:40
JRV:
Thank you for hanging in there for me, the individual cell bookmarking did the trick in grand fashion.
I sat there dumbfounded as the cells got populated...
Again, thanks for your effort, I'm good to go, topic closed.
DAS
As soon as I knew what youy were trying to do it was clear. Who cares about tables. All you wanted was to update the information by assigning some strings. This is the easiest way to do it. Believe me it tool me a bit to see how easy it was the first time aI tried it so you are not alone.
¯\_(ツ)_/¯

