MS 项目 VBA:尝试将复制的任务从 .MPP 粘贴到 Excel

MS Project VBA: Trying to Paste Copied Tasks from .MPP to Excel

我正在尝试将任务从 MS Project 中的特定过滤器复制到 Excel 文档。到目前为止,这是我所拥有的;但是,我无法将任务粘贴到工作簿中。任何帮助都会很棒。

    Public Sub Export_TopbarToExcel()

    Dim xlApp As Excel.Application
    Dim xlBook As Excel.Workbook
    Dim xlSheet As Excel.Worksheet
    Dim t As Task
    Dim pj As Project

    Set pj = ActiveProject
    Set xlApp = New Excel.Application

    xlApp.Visible = True
    'applies filter in project
    FilterApply Name:="TopBarReport"
    'selects filtered tasks and copies them
    SelectAll
    EditCopy

    'adds new workbook
    Set xlBook = xlApp.Workbooks.Add
    Set xlSheet = xlBook.Worksheets(1)

    'Add the project header info in the top 2 rows
    xlSheet.Cells(1, 1).Value = "Status Date"
    xlSheet.Cells(1, 2).Value = pj.StatusDate
    xlSheet.Cells(1, 3).Value = "Project Title"
    xlSheet.Cells(1, 4).Value = pj.Title
    'here is where the issue is...it is not pasting the selected info here
    xlSheet.Activate
        Range("A3").Activate
        EditPaste

    MsgBox "Done", vbInformation

    End Sub

EditPaste是一个项目方法所以它很可能只是复制和over-pasting相同的内容。

此外,Excel 中的 activity 可能会导致复制过程被取消。

EditCopy再往下移动,使用xlSheet.Paste或Range的PasteSpecial方法获取Excel.

中的内容
'EditCopy

'adds new workbook
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)

'Add the project header info in the top 2 rows
xlSheet.Cells(1, 1).Value = "Status Date"
xlSheet.Cells(1, 2).Value = pj.StatusDate
xlSheet.Cells(1, 3).Value = "Project Title"
xlSheet.Cells(1, 4).Value = pj.Title
'here is where the issue is...it is not pasting the selected info here
xlSheet.Activate
    Range("A3").Activate

EditCopy    'happens in Project
    'EditPaste
xlSheet.Paste    'happens in Excel

此外,您可以将 headers 添加到 Excel 粘贴之后。两个步骤互不依赖。