将表格粘贴到打开的Word文档中的书签中 [英] Pasting a table into open Word document at a bookmark

查看:98
本文介绍了将表格粘贴到打开的Word文档中的书签中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个论坛和其他论坛上进行了相当多的搜索,但是我无法获取代码.我知道这是用户错误-我正在为此学习/自学.

I have searched a fair bit on this and other forums but I can't get code to work. I know this is user error - I am learning/self-taught at this.

我想要的是在特定点将特定Excel工作表中的(公认的大)表复制到已经打开的Word文档中.我已经看到使用关键字搜索完成了此操作,但是我宁愿使用书签(并且使书签工作正常!)纯粹是因为最终用户看不到它.我正在尝试尽可能自动地创建文档.

What I want is to copy a (admittedly large) table in a specific Excel worksheet into an already-open Word document, at a specific point. I have seen this done using a keyword search but I would prefer to use a bookmark (and I've made the bookmark thing work!) purely because it's not visible to the end user. I'm trying to automate the creation of a document as much as possible.

下面的代码有效,但是只有在有问题的Word文档关闭时,我才能使其正常工作.如果我在打开doc一词时尝试运行此子程序,它将尝试重新打开它,当然不能.我找不到能使我将数据粘贴到已经打开的文档中的整齐的代码.

The below code works, but I can only get it to work when the Word document in question is closed. If I try to run this sub when the word doc is open, it just tries to re-open it and of course can't. I can't find a neat bit of code that allows me to paste data into an already-open document.

此外,我可以针对一个值而不是某个范围(即我要粘贴的表格)使用此功能.

Also, I can make this work for one value, but not for a range (i.e. the table I want to paste).

Sub ExcelRangeToWord()


Dim objWord As Object
Dim ws As Worksheet

    Set ws = ThisWorkbook.Sheets("Summary")
    Set objWord = CreateObject("Word.Application")

    objWord.Visible = True


'Optimize Code
  Application.ScreenUpdating = False
  Application.EnableEvents = False


  'open the word doc
      objWord.Documents.Open "K:\Exeter Office\Quotebuilder project\testbed\test.docx" 'change as required


'pastes the value of cell I19 at the "heatlosses" bookmark
    With objWord.ActiveDocument
        .Bookmarks("heatlosses").Range.Text = ws.Range("I19").Value
    End With

'Optimize Code
Set objWord = Nothing
  Application.ScreenUpdating = True
  Application.EnableEvents = True

'Clear The Clipboard
  Application.CutCopyMode = False

End Sub

我正尝试一次解决这一问题,因为我有一半的机会更好地理解事情...

I'm trying to tackle this one step at a time, cos then I have half a chance of understanding things a bit better...

如果我尝试复制/粘贴一个范围而不是一个值,那么我正在使用Currentregion来选择B19周围所有使用的单元格:

If I try and copy/paste a range, instead of just one value, I was using Currentregion to select all used cells surrounding B19:

With objWord.ActiveDocument
        .Bookmarks("heatlosses").Range.Text = Range("B19").CurrentRegion
    End With

所有这些操作是将单词"True"粘贴到Word中.

All this does is paste the word "True" into Word.

我感到困惑.请,有人可以提供帮助吗?

I am baffled. Please, can anyone offer assistance?

推荐答案

使用以下代码实现所需的条件:

Use the code below to achieve what you require:

Sub CopyToWord()

    Dim wApp, wDoc

    'Get the running word application
    Set wApp = GetObject(, "Word.Application")

    'select the open document you want to paste into
    Set wDoc = wApp.documents("test.docx")

    'copy what you want to paste from excel
    Sheet1.Range("A1").copy    

    'select the word range you want to paste into
    wDoc.bookmarks("heatlosses").Select

    'and paste the clipboard contents
    wApp.Selection.Paste

End Sub

这篇关于将表格粘贴到打开的Word文档中的书签中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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