2

Excel から MS Publsiher にデータをコピーするマクロを作成しようとしています。MS Word のコードを持っていますが、Publisher に適用すると機能しないようです。この行 appPub.ActiveWindow.Bookmarks("Growth").Paste で失敗します

ワード 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
4

1 に答える 1

0

ActiveWindow には .Bookmarks コレクションが含まれていないようです: https://msdn.microsoft.com/EN-US/library/office/ff939707.aspx

おそらく試してみてくださいActiveDocument.Pages(YourPage).Shapes.Paste... 運が良ければ、コピーしたテーブルが新しい形状として貼り付けられます。それ以降は、オブジェクト モデルの別の場所で使用可能な Bookmarks コレクションを見つけることができない限り、プレースホルダーを配置して見つけるための賢い方法を考える必要があります...頑張ってください!

于 2015-09-22T20:51:20.950 に答える