MS Project VBA: Попытка вставить скопированные задачи из .MPP в Excel - PullRequest
0 голосов
/ 13 марта 2019

Я пытаюсь скопировать задачи из определенного фильтра в 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

1 Ответ

0 голосов
/ 13 марта 2019

EditPaste - это метод Project, поэтому он может просто копировать и вставлять одно и то же содержимое.

Кроме того, действие в Excel может привести к отмене процесса копирования.

Переместите EditCopy дальше вниз и используйте xlSheet.Paste или PasteSpecial метод Range для получения содержимого в 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 после вставки.Два шага не зависят.

...