vba 如何将 Microsoft Project Plan 中的任务数据复制到 Excel?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9380117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 12:37:07  来源:igfitidea点击:

how to copy tasks data from Microsoft Project Plan to excel?

excelvbams-project

提问by coder25

'~~> Code to open MPP file in Excel

Sub Sample()

Dim appProj As MSProject.Application
Dim aProg As MSProject.Project
Dim wb As Workbook
Dim ws As Worksheet

Set wb = ActiveWorkbook

'~~> This is the Sheet Where you want the data to be copied
Set ws = wb.Sheets("Sheet1")

Set appProj = CreateObject("Msproject.Application")

'~~> This is a MS Project File. Change path as applicable.
appProj.FileOpen "C:\MS Project.mpp"

Set aProg = appProj.ActiveProject

appProj.Visible = True

'~~> Now you have the MPP file opened, rest of the code goes here
End Sub

I have successfully opened MPP file but I want to copy paticular task data to Excel.But I have no idea how to do it.My MPP has many tasks. Please Help.

我已经成功打开了 MPP 文件,但我想将特定的任务数据复制到 Excel。但我不知道该怎么做。我的 MPP 有很多任务。请帮忙。

I have referred to the following link but I am unable to get the code how to copy tasks data from MPP to excel enter link description here

我已经参考了以下链接,但我无法获取代码如何将任务数据从 MPP 复制到 excel 在此处输入链接描述

回答by JMax

Here is some code (Project VBA) that can inspire you:

以下是一些可以激发您灵感的代码(Project VBA):

'This module contains macros which will export
'tasks to excel and keep the task hierarchy.
'modify as necessary to include other task information

'Copyright Hyman Dahlgren, Feb 2002

Option Explicit
Dim xlRow As Excel.Range
Dim xlCol As Excel.Range
Sub TaskHierarchy()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim Proj As Project
Dim t As Task
Dim Asgn As Assignment
Dim ColumnCount as Integer
Dim Columns as Integer
Dim Tcount As Integer

Set xlApp = New Excel.Application
xlApp.Visible = True
AppActivate "Microsoft Excel"

Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets.Add
xlSheet.Name = ActiveProject.Name

'count columns needed
ColumnCount = 0
For Each t In ActiveProject.Tasks
    If Not t Is Nothing Then
        If t.OutlineLevel > ColumnCount Then
            ColumnCount = t.OutlineLevel
        End If
    End If
Next t

'Set Range to write to first cell
Set xlRow = xlApp.ActiveCell
xlRow = "Filename: " & ActiveProject.Name
dwn 1
xlRow = "OutlineLevel"
dwn 1

'label Columns
For Columns = 1 To (ColumnCount + 1)
    Set xlCol = xlRow.Offset(0, Columns - 1)
    xlCol = Columns - 1
Next Columns
rgt 2
xlCol = "Resource Name"
rgt 1
xlCol = "work"
rgt 1
xlCol = "actual work"
Tcount = 0
For Each t In ActiveProject.Tasks
    If Not t Is Nothing Then
        dwn 1
        Set xlCol = xlRow.Offset(0, t.OutlineLevel)
        xlCol = t.Name
            If t.Summary Then
                xlCol.Font.Bold = True
            End If
        For Each Asgn In t.Assignments
            dwn 1
            Set xlCol = xlRow.Offset(0, Columns)
            xlCol = Asgn.ResourceName
            rgt 1
            xlCol = (Asgn.Work / 480) & " Days"
            rgt 1
            xlCol = (Asgn.ActualWork / 480) & " Days"
        Next Asgn
        Tcount = Tcount + 1
    End If
Next t
AppActivate "Microsoft Project"

MsgBox ("Macro Complete with " & Tcount & " Tasks Written")
End Sub
Sub dwn(i As Integer)
Set xlRow = xlRow.Offset(i, 0)
End Sub

Sub rgt(i As Integer)
Set xlCol = xlCol.Offset(0, i)
End Sub

Source

来源