VBA Excel 无法从 MS Project 找到自定义值

发布于 2025-01-19 12:12:28 字数 979 浏览 3 评论 0 原文

我一直在尝试使用 Excel VBA .Find 方法从 MS Project 查找 Text1,但它似乎不起作用。我有一个工作代码,但它非常慢,因为它循环执行每个任务。这是我的代码:

Sub Exp()
    With Application.FileDialog(msoFileDialogOpen)
    .Filters.Clear
    .Filters.Add "MPD", "*.mpp*"
       If .Show = True Then
          Dim getfilename As String
          Dim appProj As MSProject.Application
          Dim Found As Boolean
          Dim TaskObject As Object
          getfilename = .SelectedItems(1)
          Set appProj = CreateObject("Msproject.Application")
             appProj.FileOpen (getfilename)
             appProj.Visible = True
          Found = appProj.Find (Field:="Text1", Test:="equals", Value:="Exp1")
          If Found Then
             Set TaskObject = appProj.ActiveCell.Task
             Debug.Print TaskObject.UniqueID & " - " & TaskObject.Name
          Else
             MsgBox "Not Found"
          End If
       End If
    End With
   End Sub

我怎样才能让这个代码工作或者有其他方法可以做到这一点? 感谢您的回答。

I've been trying to find Text1 from MS Project using Excel VBA .Find method but it doesn't seem to be working. I have a working code but it is very slow because it loops through every task. Here is my code:

Sub Exp()
    With Application.FileDialog(msoFileDialogOpen)
    .Filters.Clear
    .Filters.Add "MPD", "*.mpp*"
       If .Show = True Then
          Dim getfilename As String
          Dim appProj As MSProject.Application
          Dim Found As Boolean
          Dim TaskObject As Object
          getfilename = .SelectedItems(1)
          Set appProj = CreateObject("Msproject.Application")
             appProj.FileOpen (getfilename)
             appProj.Visible = True
          Found = appProj.Find (Field:="Text1", Test:="equals", Value:="Exp1")
          If Found Then
             Set TaskObject = appProj.ActiveCell.Task
             Debug.Print TaskObject.UniqueID & " - " & TaskObject.Name
          Else
             MsgBox "Not Found"
          End If
       End If
    End With
   End Sub

How can i get this code working or is there another way of doing this?
Thanks for your answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞笑我太脆弱 2025-01-26 12:12:28

查找是应用对象的一种方法,不是项目对象。因此,正确使用它的方法是:

Found = Application.Find(Field:="UniqueID", Test:="equals", Value:="130")

但是,有一种更快的方法可以通过使用 uniqueId 任务对象的属性:

Set TaskObject = ActiveProject.Tasks.UniqueID(130)

Find is a method of the Application object, not the Project object. So the correct way to use it is this:

Found = Application.Find(Field:="UniqueID", Test:="equals", Value:="130")

However, there is a faster way to get a reference to a task object by way of its UniqueID by using the UniqueID property of the Tasks object:

Set TaskObject = ActiveProject.Tasks.UniqueID(130)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文