【问题标题】:MS Project VBA: Trying to Paste Copied Tasks from .MPP to ExcelMS Project VBA:尝试将复制的任务从 .MPP 粘贴到 Excel
【发布时间】:2020-07-01 07:08:04
【问题描述】:

我正在尝试将任务从 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

【问题讨论】:

  • 发生了什么,或者没有发生什么?有错误吗?
  • 没有任何东西粘贴到 excel 文档中。添加了状态日期和项目标题并选择了单元格 A3,但没有粘贴任何内容。如果我在excel中手动“粘贴特殊文本”,信息就会转移。
  • 在代码中将 EditCopy 语句进一步向下移动,也许 Excel 中的所有活动都会导致复制过程停止。 (当在单元格中键入内容时,这会直接在 Excel 中发生。)或者在粘贴后添加项目标题。
  • 实际上,EditPaste 正在处理 Project。它可能只是复制过度粘贴相同的内容。您可能想要xlSheet.Paste 或 range.PasteSpecial。

标签: excel vba ms-project


【解决方案1】:

EditPaste 是一个 Project 方法,因此它很可能只是复制和过度粘贴相同的内容。

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

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

此外,您可以在粘贴后 将标题添加到 Excel。这两个步骤不依赖。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多