You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Excel VBA获取MS Project TaskDependencies属性遇438错误求助

问题描述

编写Excel VBA宏导入MS Project文档数据时,多数属性已正常集成,但使用objTask.TaskDependencies(对应代码中Cells(i,15))触发运行时错误'438'(对象不支持该属性或方法)。需求是:若任务存在前置任务(Predecessors)或后继任务(Successors)则返回True,两者都无时返回False。

解决方案

TaskDependencies是任务依赖关系的集合对象,并非布尔类型属性,因此不能直接赋值给单元格来返回真假值。要实现需求,可通过两种方式判断:

方法1:检查依赖集合数量

直接判断TaskDependencies.Count是否大于0,只要任务存在前置或后继依赖,这个集合的数量就会大于0:

Cells(i, 15) = (objTask.TaskDependencies.Count > 0)

方法2:检查前置/后继任务内容

利用已调用的Predecessors和Successors属性,判断两者是否均为空字符串:

Cells(i, 15) = Not (objTask.Predecessors = "" And objTask.Successors = "")

修改后的完整代码

Sub ImportDataFromProject()
    Dim objProject As Object 'MS Project application
    Dim objTask As Object 'Task object
    Dim strFileName As String 'File name
    Dim i As Long 'Counter
    Dim fd As FileDialog 'File dialog object

    'Create file dialog object and set properties
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    fd.Title = "Wählen Sie eine Project-Datei aus"
    fd.Filters.Clear
    fd.Filters.Add "Project-Dateien", "*.mpp"

    'Show file dialog and get file name
    If fd.Show = -1 Then 'User clicked OK
        strFileName = fd.SelectedItems(1) 'Get first selected item

        'Create MS Project instance and open file
        Set objProject = CreateObject("MSProject.Application")
        objProject.FileOpen strFileName

        'Loop through tasks and copy data to Excel
        i = 4
        For Each objTask In objProject.ActiveProject.Tasks
            If Not objTask Is Nothing Then 'Skip blank rows
                Cells(i, 1) = objTask.ID
                Cells(i, 2) = objTask.Name 'Copy task name to column A
                Cells(i, 3) = objTask.Start 'Startdatum
                Cells(i, 4) = objTask.Finish 'Enddatum
                Cells(i, 5) = objTask.Predecessors 'Vorgänger
                Cells(i, 6) = objTask.Successors 'Nachfolger
                Cells(i, 7) = objTask.Summary ' Sammelvorgang
                Cells(i, 8) = objTask.Milestone 'Meilenstein
                Cells(i, 9) = objTask.Duration 'Dauer
                Cells(i, 10) = objTask.PercentComplete 'Fertigstellung
                Cells(i, 11) = objTask.ConstraintType 'Einschränkungstyp
                Cells(i, 12) = objTask.Active 'Aktive Aktivitäten
                Cells(i, 13) = objTask.FreeSlack 'Freie Puffer
                'Cells(i, 14) = objTask.X
                ' 替换为正确的判断逻辑,二选一即可
                Cells(i, 15) = (objTask.TaskDependencies.Count > 0) '方法1
                'Cells(i, 15) = Not (objTask.Predecessors = "" And objTask.Successors = "") '方法2
                Cells(i, 16) = objTask.Critical ' Kritisch
                Cells(i, 17) = objTask.ConstraintDate 'Einschränkungstermin
                
                i = i + 1 'Increment counter
            End If
        Next objTask

        'Close MS Project file and application
        objProject.FileClose False
        objProject.Quit pjDoNotSave

    Else 'User clicked Cancel or closed the dialog

        MsgBox "Keine Datei ausgewählt."

    End If
End Sub

内容的提问来源于stack exchange,提问作者CKay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 09:02:06