VBS:向 MSWord 表追加两行。怎么做呢?
这里< /a> 我了解了一个 VBS 脚本,它可以将一行添加到我的 MS Word 表格中。要添加的行包含四个单元格,每个单元格包含一个单词:
海龟、猎豹、公鸡、枫树
脚本运行良好。它是这样的:
Set wd = CreateObject("Word.Application")
wd.Visible = True
Set doc = wd.Documents.Open ("E:\my_folder\add_to_this_table.doc")
Set r = doc.Tables(1).Rows.Add
aa = Split("turtle,cheetah,rooster,maple", ",")
For i = 0 To r.Cells.Count - 1
r.Cells(i + 1).Range.Text = aa(i)
Next
现在,如果我需要向该表添加两行怎么办?
第二行是:
鬣蜥、熊、鹰、桤木
我尝试了这种方式,但它不起作用:
Set wd = CreateObject("Word.Application")
wd.Visible = True
Set doc = wd.Documents.Open ("E:\my_folder\add_to_this_table.doc")
Set r = doc.Tables(1).Rows.Add
aa = Split("turtle,cheetah,rooster,maple", ",")
For i = 0 To r.Cells.Count - 1
r.Cells(i + 1).Range.Text = aa(i)
Set r = doc.Tables(1).Rows.Add
bb = Split("iguana,bear,hawk,alder", ",")
For i = 0 To r.Cells.Count - 1
r.Cells(i + 1).Range.Text = bb(i)
Next
我在这里做错了什么?
Here I learned about a VBS script that would append a line to my MS Word table. The line to be added contains four cells, each cell containing one word:
turtle,cheetah,rooster,maple
The script works fine. It goes like this:
Set wd = CreateObject("Word.Application")
wd.Visible = True
Set doc = wd.Documents.Open ("E:\my_folder\add_to_this_table.doc")
Set r = doc.Tables(1).Rows.Add
aa = Split("turtle,cheetah,rooster,maple", ",")
For i = 0 To r.Cells.Count - 1
r.Cells(i + 1).Range.Text = aa(i)
Next
Now, what if I need to add two lines to this table?
The second line would be:
iguana,bear,hawk,alder
I tried it this way, but it doesn't work:
Set wd = CreateObject("Word.Application")
wd.Visible = True
Set doc = wd.Documents.Open ("E:\my_folder\add_to_this_table.doc")
Set r = doc.Tables(1).Rows.Add
aa = Split("turtle,cheetah,rooster,maple", ",")
For i = 0 To r.Cells.Count - 1
r.Cells(i + 1).Range.Text = aa(i)
Set r = doc.Tables(1).Rows.Add
bb = Split("iguana,bear,hawk,alder", ",")
For i = 0 To r.Cells.Count - 1
r.Cells(i + 1).Range.Text = bb(i)
Next
What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您忘记了第一个 For 循环的 Next。
You forgot the Next for your first For loop.