繁体   English   中英

如何从 Excel VBA 跳转到 Word 文档中的下一个选择?

[英]How to tab to next selection in Word document from Excel VBA?

我有一个设置为文本框网格的 Word 文档和一个包含一行地址的 Excel 文档。

我的目标是将这些地址中的每一个都放在一个文本框中,以便我可以将它们打印出来以与邮寄贴纸对齐。

如果我手动复制/粘贴地址,我需要点击一次以选择下一个文本框。 但是,我找不到通过 VBA 发送“tab”命令的方法。

有没有办法选项卡来选择下一个文本框或形状?

   For row = 2 To last_row
        addr = Cells(row, col)
        wordselection.TypeText (addr) & vbTab ' Tried sending a vbtab, but that doesn't affect selection
        wordselection.moveright Unit:=wdCell 'This gives an error, presumably because I'm not in a table.

        Set shp = wordapp.activedocument.Shapes("Item " & row) ' Tried getting the shape by name, but this gives an error
        shp.Select (True)
         
    Next

除了发送标签之外,可能还有一种更简单的方法来实现我的目标; 我对这里的 XY 问题持开放态度。

原来有一个sendkeys 语句

 For row = 2 To last_row
    addr = Cells(row, col)
    wordselection.TypeText (addr)
    sendkeys "{TAB}" 'Non printed characters have {} names.
     
Next

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM