将Excel图像粘贴到Word标题不在Office 2013中工作

时间:2014-08-07 13:13:14

标签: excel vba ms-word word-vba

我有一个生成Word文档的Excel工作簿。有一次,它将Excel图像复制到生成的Word文档的标题中。此代码在Office 2003,2007和2010中有效,但现在在2013年,它在Paste语句中失败,并在Paste方法中出现通用自动化错误。我希望有人可以帮我在Office 2013中完成这项工作。要复制,请使用包含单个形状的名为“Logo”的工作表定义宏工作簿,添加以下代码,然后执行 doctop 子程序:

Const wdCollapseEnd = 0
Const wdHeaderFooterPrimary = 1
Const wdPrintView = 3
Const wdSeekCurrentPageHeader = 9
Const wdSeekMainDocument = 0

Sub doctop()
    Set wd = CreateObject("Word.Document")
    wd.Application.Visible = True

    ' insert heading with logo
    Worksheets("Logo").Shapes(1).CopyPicture xlScreen, xlBitmap
    With wd.ActiveWindow.View
        .Type = wdPrintView
        .SeekView = wdSeekCurrentPageHeader
    End With
    With wd.sections(1).headers(wdHeaderFooterPrimary).Range
        .Collapse wdCollapseEnd
        .Paste
    End With
    wd.ActiveWindow.View.SeekView = wdSeekMainDocument

End Sub

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

虽然我没有2013年进行测试,但即使在2010年,我也遇到了某些问题,这些问题并未通过PastePasteSpecial选项公开;特别是那些与应用程序之间粘贴和保留格式/等有关的那些。

考虑到这一点,我猜测Mso程序应该有效。尝试:

wd.Application.CommandBars.ExecuteMso "PasteAsPicture" 

其他几个你也可以测试:

"PasteBitmap" 
"PasteGif"
"PastePng"
"PasteJpeg"