简体   繁体   English

使用vba将ComboBox从Word文档复制到另一个Word文档

[英]Copy ComboBox from a Word Document to another Word Document with vba

I have a combo box (combo1) in a word document and I would like to copy it to another word document. 我在Word文档中有一个组合框(combo1),我想将其复制到另一个Word文档中。 (end game will be looping through 100's of docs). (最终游戏将遍历100多个文档)。

I can't for the life of me work out how to select and/or copy the combo box, although its easy enough to do outside of vba. 我无法终生解决如何选择和/或复制组合框,尽管它很容易在vba之外进行。

So far I've tried turning it into a bookmark, seems to select ok, but won't copy. 到目前为止,我尝试将其转换为书签,似乎选择了OK,但不会复制。

ActiveDocument.Bookmarks(combo1_bm).Select
Selection.Copy

I thought it would be able to done as an inline shape (as that is how they are added?), however again the selection appears to work, but the Copy doesn't. 我以为它可以作为内联形状来完成(因为这是它们的添加方式?),但是再次选择似乎起作用了,但“复制”却没有。

ActiveDocument.InlineShapes(combo1).Select
Selection.Copy

Any ideas on what I can try next? 关于下一步可以尝试的任何想法?

Cheers, Michael 干杯,迈克尔

Your attempt with bookmark was quite good. 您使用书签的尝试非常好。 You just need to extend your code a bit: 您只需要扩展一下代码即可:

ActiveDocument.Bookmarks("combo1_bm").Range.Copy
....
Selection.Paste        'or different pasting procedure

Keep in mind that you don't need to select object before copy it. 请记住,复制之前不需要选择对象。 Just try doing as I show above. 就像我上面显示的那样尝试。 Moreover, don't miss quotation marks for names or use bookmark index to work with appropriate one. 此外,请不要错过名称的引号或使用书签索引来使用适当的名称。 Copy method will copy inside range of the bookmark and keep original bookmark in place, unchanged. Copy method将在书签范围内进行复制,并保持原始书签不变。

This should do the trick. 这应该可以解决问题。

Set ComboBox1Range = ActiveDocument.Range(Start:=ActiveDocument.Bookmarks("combo1_bm").Range.Start - 1, _
                             End:=ActiveDocument.Bookmarks("combo1_bm").Range.End)
ComboBox1Range.Expand Unit:=wdParagraph
ComboBox1Range.Copy

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

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