VBA从项目中将数据粘贴到Excel中

时间:2017-05-10 03:04:17

标签: excel vba excel-vba ms-project microsoft-project-vba

我正在运行以下代码并获得虚假结果。

由于某种原因,它会将五行代码复制到所需的工作表中,而不是指定的MS Project数据。

任何人都可以帮助新手吗?

五行代码错误地复制到Excel工作表中:

'Open MS Project file
projApp.Application.FileOpenEx "C:File.mpp"

Set projApp = projApp.ActiveProject

'Final set up of code
Set projApp = Nothing

Error Image

Sub OpenProjectCopyPasteData()

Dim appProj As MSProject.Application
Dim aProg As MSProject.Project
Dim sel As MSProject.Selection
Dim ts As Tasks
Dim t As Task
Dim rng As Range
Dim ws As Worksheet

Application.DisplayAlerts = False

'Clear current contents

Set ws = Worksheets("Project Data")
Set rng = ws.Range("A:J")
rng.ClearContents

On Error Resume Next
Set appProj = GetObject(, "MSProject.Application")
If appProj Is Nothing Then
    Set appProj = New MSProject.Application
End If
appProj.Visible = True

'Open MS Project file
projApp.Application.FileOpenEx "C:File.mpp"
Set projApp = projApp.ActiveProject

'Final set up of code
Set projApp = Nothing

appProj.Visible = True

WindowActivate WindowName:=aProg

'Copy the project columns and paste into Excel
Set ts = aProg.Tasks

SelectTaskColumn Column:="Task Name"
OutlineShowAllTasks
OutlineShowAllTasks
EditCopy
Set ws = Worksheets("Project Data")
Set rng = ws.Range("A:A")
ActiveSheet.Paste Destination:=rng

SelectTaskColumn Column:="Task Name"
EditCopy
Set rng = ws.Range("B:B")
ActiveSheet.Paste Destination:=rng

SelectTaskColumn Column:="Resource Names"
EditCopy
Set rng = ws.Range("C:C")
ActiveSheet.Paste Destination:=rng

SelectTaskColumn Column:="Finish"
EditCopy
Set rng = ws.Range("D:D")
ActiveSheet.Paste Destination:=rng

Application.DisplayAlerts = True
appProj.DisplayAlerts = True

End Sub

1 个答案:

答案 0 :(得分:3)

我不确定上面的原始代码是如何工作的,因为您DimSet变量appProj,但稍后尝试使用{{1}打开MS-Project文件}(projApp.Application.FileOpenEx "C:File.mpp"<> projApp)。

尝试以下代码(已测试),它会将3列(appProj"Name""Resource Names")复制到工作表"项目数据"在列" A:C"。

<强>代码

"Finish"