VBS:向 MSWord 表追加两行。怎么做呢?

发布于 2024-12-17 23:57:02 字数 1239 浏览 1 评论 0原文

这里< /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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许仙没带伞 2024-12-24 23:57:03

您忘记了第一个 For 循环的 Next。

You forgot the Next for your first For loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文