简体   繁体   English

使用vba从xls文档复制到word

[英]copy from xls document into word using vba

I'm trying to make a code in which to copy charts from a xls file into a word document using the PasteSpecial property (picture(enhanced metafile). I would like to change the existing charts of the document to new ones. So, I thought that using bookmarks for the existing charts would be OK. I'm using OFFICE 2007. 我正在尝试创建一个代码,使用PasteSpecial属性(图片(增强型图元文件)将图表从xls文件复制到word文档。我想将文档的现有图表更改为新图表。所以,我我认为使用现有图表的书签可以。我正在使用OFFICE 2007。

I've written the following code: 我写了以下代码:

        Dim YMApp As Word.Application
        Dim YMDoc As Word.Document
        Dim B as Bookmark
        paaath = "D:\"
        dime = "NameOld.doc"
        dime2 = "NameNew.doc"
        Set YMApp = New Word.Application
        YMApp.Visible = True
        Set YMDoc = YMApp.Documents.Open(paaath & dime)
        Word.Documents(dime).SaveAs (paaath + dime2)
        For k = 1 To 6
            Windows("New.xls").Activate
            Sheets("graph").Select
            Range("L" + Trim(Str(br(k))) + ":V" + Trim(Str(br(k) + 24))).Select
            Selection.Copy
            ddd = "bm" + Trim(Str(k))
            Set B = YMDoc.Bookmarks(ddd)
            YMApp.Selection.PasteSpecial DataType:=wdPasteMetafilePicture, Placement:=B
        Next k
        YMDoc.Close
        YMApp.Quit
        Application.CutCopyMode = False
        ActiveWorkbook.Close
    End
End Sub

The problem is that by this code the bookmarks which are already created are not recognized. 问题在于,通过此代码,无法识别已创建的书签。 How to cope with the problem? 如何应对这个问题?

The Placement argument of PasteSpecial does not accept a Bookmark object: PasteSpecialPlacement参数不接受Bookmark对象:

Set B = YMDoc.Bookmarks(ddd)
YMApp.Selection.PasteSpecial DataType:=wdPasteMetafilePicture, Placement:=B

Instead, it takes a WdOLEPlacement constant . 相反,它需要一个WdOLEPlacement常量

I think you'll need to select the bookmark before you do the PasteSpecial . 我认为您需要在执行PasteSpecial之前选择书签。 You may need to delete existing chart (if any), also. 您可能还需要删除现有图表(如果有)。

Untested, but I think you need something like this: 未经测试,但我认为你需要这样的东西:

Dim wdRange as Word.Range
Set B = YMDoc.Bookmarks(ddd)
Set wdRange = B.Range

YMApp.Selection.GoTo What:=wdGoToBookMark, Name:=B.Name

' Delete existing shapes & bookmark if any:
On Error Resume Next
YMDoc.ShapeRange(1).Delete
wdRange.Delete
On Error GoTo 0
YMApp.Selection.PasteSpecial DataType:=wdPasteMetafilePicture, Placement:=0 'Or 1

'Add the bookmark back in place:
MDoc.Selection.Bookmarks.Add Name:=ddd, wdRange

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

相关问题 使用vba将word文档的内容复制到另一个word文档中 - Using vba to copy the contents of a word document into another word document 使用vba将ComboBox从Word文档复制到另一个Word文档 - Copy ComboBox from a Word Document to another Word Document with vba 使用VBA将活动的Word文档复制并粘贴到活动的Excel文档中 - Copy and Paste active Word document to active Excel document using VBA VBA Word 从特定单词复制文本到文档末尾 - VBA Word Copy text from specific word till the end of the document 使用Excel宏VBA将Word文档中的第一个表复制到Excel - Copy first table from word document to an excel using excel macro VBA select 使用 vba 并复制到另一个文档的末尾并保留格式的一个 Word 文档中的一系列文本 - select a range of text from one Word doc using vba and copy to end of another document and RETAIN formatting 使用 Word 宏/VBA 将表格从一个 Word 文档复制到另一个 Word 文档 - Use Word Macro/VBA to Copy Tables from One Word Document to Another Word Document 如何使用 Lotusscript 和/或 VBA 将格式化的 Richtext 复制到 Word 文档中? - How to copy formatted Richtext into a Word document using Lotusscript and/or VBA? 使用 VBA 从 Word 文档中检索段落 - Retrieving a paragraph from a word document using VBA 不使用剪贴板(VBA)复制Word文档内容 - copy Word document contents without using clipboard (VBA)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM