VBA for Publisher vs Word

时间:2013-10-15 19:42:37

标签: vba publisher

尝试构建宏以将数据从Excel复制到MS Publsiher。我有MS Word的代码,但它在应用于Publisher时似乎不起作用。它在这行appPub.ActiveWindow.Bookmarks(“增长”)失败。粘贴

Word VBA:

    Sub SendData()
    Dim WordApp As Object
    Set WordApp = CreateObject("Word.Application")
    Dim ws As Worksheet
    ' Sheet1 is the codename for the sheet with the named range you want to copy,
    ' this is the name of the sheet in brackets in the VBAProject explorer, not the
    ' friendly name given on the worksheet tab itself visible to the end user.
    Set ws = Sheet4
    ' This is the constant string which holds the filepath to your Word document
    Const WORDDOC As String = "C:\Quarterly Reports - Word Version\Growth.docx"

    WordApp.Visible = True

    WordApp.Documents.Open WORDDOC

    ' Copies the named range "OrderRange" from the Excel book 
        'you are running this from.
    ws.Range("Growth").Copy
    ' Pastes it to the bookmark "OrderBookmark" in your Word doc template.
    WordApp.ActiveDocument.Bookmarks("Growth").Range.PasteAppendTable
    ' Sets your printer in Word to Adobe PDF and then prints the whole doc.
    ' WordApp.ActivePrinter = "Adobe PDF"
    ' WordApp.ActiveDocument.PrintOut
    Set WordApp = Nothing
    End Sub

发布商VBA:

    Sub SendDataPB()
    Dim appPub As Object
    Set appPub = CreateObject("Publisher.Application")
    Dim ws As Worksheet
    ' Sheet1 is the codename for the sheet with the named range you want to copy,
    ' this is the name of the sheet in brackets in the VBAProject explorer, not the
    ' friendly name given on the worksheet tab itself visible to the end user.
    Set ws = Sheet4
    ' This is the constant string which holds the filepath to your Publisher document
    Const PublisherDOC As String = "C:\Quarterly Reports - Publisher     Version\Growth.pub"

    appPub.ActiveWindow.Visible = True

    appPub.Open PublisherDOC

    ' Copies the named range "OrderRange" from the Excel book
    '     you are running this from.
    ws.Range("Growth").Copy
    ' Pastes it to the bookmark "OrderBookmark" in your Publisher doc template.
    appPub.ActiveWindow.Bookmarks("Growth").Paste
    ' Sets your printer in Publisher to Adobe PDF and then prints the whole doc.
    ' PublisherApp.ActivePrinter = "Adobe PDF"
    ' PublisherApp.ActiveDocument.PrintOut
    Set appPub = Nothing
    End Sub

1 个答案:

答案 0 :(得分:0)

ActiveWindow似乎不包含任何.Bookmarks集合:https://msdn.microsoft.com/EN-US/library/office/ff939707.aspx

尝试struct或许......运气好,将复制的表格粘贴为新形状。从那时起,你只需要想出一个聪明的方法来放置和找到占位符,除非你设法在对象模型的其他地方找到一个可用的书签集合......祝你好运!