VBA宏 - 从Excel复制粘贴到Word [英] VBA Macro - Copy Paste from Excel to Word

查看:1289
本文介绍了VBA宏 - 从Excel复制粘贴到Word的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过宏快捷键将打开的Excel电子表格中的单元格数据提取到打开的Word文档中。我需要粘贴数据的word文档中的部分是NAME,ADDRESS,CITY / STATE / ZIP的典型三行格式。我需要复制的电子表格
中的单元格是任何给定行中的前五个。例如A1 = John Smith,B1 = 123 Somewhere Lane,C1 = anywhereville,D1 = NJ,E1 = 01234。之后有后续单元格有附加数据,但我不需要它们。我希望
能够简单地点击我正在处理的任何名称的第一个单元格并将宏复制到该单元格和接下来的四个单元格,然后粘贴(仅文本 - 无格式) 在我之前提到的
的单词文档部分的适当位置。我知道如何使用邮件合并,但这是一大块字母。有时我只需偶尔发出一个字母,这个宏可以帮助加快速度,而不是从电子表格中复制名称,将其粘贴到单词中,然后按
等等。这对某些人来说可能看起来很容易,但只要我有这个解决方案,我就处于一种冲击状态。任何帮助将不胜感激。感谢您的时间和耐心。 

解决方案

我已经多次这样做了。最简单和最简单的解决方案是创建一个具有基本字母结构的Word模板。在您想要名称,地址等的位置插入书签。


在电子表格名称中,单元格具有相同的数据。因此,Excel中的命名单元格名称和Word中的书签名称。代码是:


WrdApp.ActiveDocument.Bookmarks(" Name")。Range.text = Range(" Name")。text


Excel中的单元格和Word中的名称等可以移动,但只要名称单元格和书签仍然存在,代码即可继续工作。


WrdApp是指向单词的指针。


Dim wrdApp as Object


设置wrdApp = GetObject(,"Word.Application"")指向活动工作应用程序。


I want to extract cell data from an open Excel Spreadsheet to an open Word Document by means of a Macro Shortcut Key. The section in the word document I need to paste data to is a typical three line format for NAME, ADDRESS, CITY/STATE/ZIP. The cells in the Spreadsheet I need to copy from are the first five in any given row. For instance A1 = John Smith, B1 = 123 Somewhere Lane, C1 = anywhereville, D1 = NJ, E1 = 01234. There are subsequent cells afterwards with additional data, but I don't need them. I want to be able to simply click on the first cell of whatever name I'm working on and have the macro  copy that cell and the next four, then paste that (text only - no format)  into their appropriate places in the section of the word document I mentioned earlier. I know how to use mail merge, but that's for a large chunk of letters. Sometimes all I need is to occasionally send out one letter, and this macro would help speed things up instead of copying the name from the spreadsheet, paste it into word, and so on and so on. This might seem laughably easy to some, but looking as long as i have for this solution, I'm at an impass. Any help would be greatly appreciated. Thanks your time and patience. 

解决方案

I've done this before a number of times. The simplest and easiest solution is to create a Word template that has the basic letter structure. Insert a bookmark where you want the Name, Address etc to go.

In the spreadsheet name the cells with teh same data. So a named cell Name in Excel and a bookmark Name in Word. Code is:

WrdApp.ActiveDocument.Bookmarks("Name").Range.text=Range("Name").text

The cells in Excel and the Name etc in Word can move, but as long as teh named cell and bookmark still exist, teh codee continues to work.

WrdApp is a pointer to word.

Dim wrdApp as Object

Set wrdApp=GetObject(,"Word.Application") points iot to active work application.


这篇关于VBA宏 - 从Excel复制粘贴到Word的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆