将 VBA 2-Dim 字符串数组粘贴到 Word 表中

发布于 2024-10-05 00:43:11 字数 357 浏览 4 评论 0原文

我有一个表的抽象,其中包含 Word VBA 中的二维字符串数组。

现在我想将此数组保存到 Word 文档中的表中,而不需要遍历两个维度...据我所知,有一种方法可以从 excel 中获取 Range(...) 内容作为数组...

例如:

Dim rng As Excel.Range
Set rng = excelTabelle.Range("A4:F" + CStr(lastRow))
arrData = rng.Value

是否有将其粘贴到 Word 表格中的类似解决方案?

迭代需要太多的执行时间,所以我正在寻找一种更有效的方法来做到这一点。

问候,波斯克伦

I have an abstraction of a table hold als a two dimensional String array in Word VBA.

Now I want to save this array into a table in a word document, without iteration through both dimensions...afaik there is a method to get a Range(...) content from excel as array...

e.g.:

Dim rng As Excel.Range
Set rng = excelTabelle.Range("A4:F" + CStr(lastRow))
arrData = rng.Value

is there a similar solution for pasting this into a Word table?

Iterating needs too much time for the execution, so I'm looking for a more efficient way to do that.

Greets, poeschlorn

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

七婞 2024-10-12 00:43:11

一种解决方案可能是利用剪贴板来满足您的需求。它不像 Excel 那样容易,但对您来说可能足够快了。下面是一些示例代码,说明如何通过剪贴板将一些文本放入 2x3 字表中(将 Microsoft Forms 2.0 对象库的引用添加到您的 VBA 文档中):

Dim oData As New DataObject
Dim sText As String

' Navigate to the top-left cell of your table (just an example)
Selection.HomeKey Unit:=wdStory

' Put text to the clipboard
sText = "X1" & vbTab & "B1" & vbTab & "C1" & vbCrLf
sText = sText & "A2" & vbTab & "B2" & vbTab & "C2" & vbCrLf

On Error Resume Next
oData.SetText sText
oData.PutInClipboard
On Error GoTo 0

' Select the needed rows and columns of the table'
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.MoveRight Unit:=wdCharacter, Count:=2, Extend:=wdExtend

' Paste the clipboard to the table '
Selection.Paste

我建议您对其进行调整,将任意二维数组放入字符串并以类似的方式粘贴。如果您需要更多帮助,请回来给我们提供更多详细信息。

One solution may be to utilize the clipboard for your needs. It is not so easy as in Excel, but perhaps fast enough for you. Here is some sample code how to get some text through the clipboard into a 2x3 word table (add a reference to Microsoft Forms 2.0 object library to your VBA doc):

Dim oData As New DataObject
Dim sText As String

' Navigate to the top-left cell of your table (just an example)
Selection.HomeKey Unit:=wdStory

' Put text to the clipboard
sText = "X1" & vbTab & "B1" & vbTab & "C1" & vbCrLf
sText = sText & "A2" & vbTab & "B2" & vbTab & "C2" & vbCrLf

On Error Resume Next
oData.SetText sText
oData.PutInClipboard
On Error GoTo 0

' Select the needed rows and columns of the table'
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.MoveRight Unit:=wdCharacter, Count:=2, Extend:=wdExtend

' Paste the clipboard to the table '
Selection.Paste

I suggest that you adapt this to put an arbitrary 2-dimensional array into a string and paste it in a similar manner. If you need more help, come back give us more details, please.

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