我的任务是从 SQL 中获取任务并在 Project Desktop 中更新这些任务,以便它们匹配。 有没有办法在 Project Desktop 和 SQL 之间传递信息? 项目文件位于 SharePoint 上,但它们不是 Project Online 文件,因此我无法使用该连接。
这可能吗?
是的,即使 Project 文件位于 SharePoint 上,也可以在 Microsoft Project Desktop 和 SQL 之间传递信息。您可以使用 Microsoft Project 中的 VBA 连接到 SQL 数据库、检索任务并相应地更新项目文件。 逐步方法:
示例:
Sub UpdateTasksFromSQL()
Dim conn As Object
Dim rs As Object
Dim sql As String
Dim proj As Project
Dim t As Task
' Initialize ADO connection
Set conn = CreateObject("ADODB.Connection")
conn.ConnectionString = "Provider=SQLOLEDB;Data Source=YourServerName;Initial Catalog=YourDatabaseName;User ID=YourUsername;Password=YourPassword;"
conn.Open
' SQL query to fetch tasks
sql = "SELECT TaskID, TaskName, StartDate, FinishDate FROM YourTasksTable"
' Execute the query
Set rs = conn.Execute(sql)
' Reference the active project
Set proj = ActiveProject
' Loop through the recordset and update tasks in Project
Do While Not rs.EOF
' Find the task by ID
Set t = proj.Tasks(rs.Fields("TaskID").Value)
' Update task properties
If Not t Is Nothing Then
t.Name = rs.Fields("TaskName").Value
t.Start = rs.Fields("StartDate").Value
t.Finish = rs.Fields("FinishDate").Value
End If
rs.MoveNext
Loop
' Clean up
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
结束子