使用 VBA 代码从 Microsoft Access 更新 TFS WorkItem






4.50/5 (2投票s)
本文档旨在从 VBA 代码访问 TFS,因为我们无法直接从 VBS 引用 Team Foundation Server DLL。
引言
许多 IT 用户不熟悉 C# 代码或其他语言,无法创建小型应用程序。他们使用 Microsoft Access/Excel 构建应用程序,因为它不需要任何编程经验。他们可以在不编写任何代码行的情况下添加表单/报表。在我的组织中,一些部门,如发布管理和 IT 测试,使用 Microsoft Access 或 Microsoft Excel 开发小型应用程序来跟踪他们自己的工作,但当变更管理实施 Team Foundation Server 并使用 TFS 工作项
作为任务、Bug 等工作项的存储库时,在大多数情况下,需要在 Access 或 Excel 和 TFS 之间进行大量手动工作,并且这些应用程序之间没有集成。
步骤 1
创建一个新的 C# 类库项目

添加 System.Runtime.InteropServices
引用,以便将该类作为 COM 暴露,从而从 VBA 使用该类。
添加这些 TFS DLL 引用,以便在使用管理 工作项
时使用它们。
- Microsoft.TeamFoundation.Client
- Microsoft.TeamFoundation.WorkItemTracking.Client
当你打开调用时,代码会添加以下内容
[ComVisible(true),
ClassInterface(ClassInterfaceType.AutoDual)] //this command to expose the class
public class WorkItemTraking
{
}
使用以下方法添加与 TFS 的连接
private void TFSconnect()
{
try
{
System.Net.NetworkCredential _credential =
(System.Net.NetworkCredential)System.Net.CredentialCache.DefaultCredentials;
Uri _path = new Uri("http://devprosrv07:8080/tfs/CoreES");
TfsTeamProjectCollection server = new TfsTeamProjectCollection(_path);
server.Authenticate();
store = server.GetService(typeof(WorkItemStore)) as WorkItemStore;
if (false == server.HasAuthenticated)
{
throw new Exception("Could not authenticate while connecting to tfs");
}
}
catch (Exception _exp)
{
throw;
}
}
在本例中,我将编写两种方法。首先,读取 WIQ(工作项
查询)上的 工作项
数据库,用户将查询作为 字符串
传递,结果将作为 XML 返回。
因此,我将 datatable
序列化为 XML
[ComVisible(true), Description("Get workitem")]
public string GetWorkItem(string WIQuery)
{
TFSconnect(); //Connect to TFS collection
DataTable _returnResult;
try
{
WorkItemCollection _workitemcol;
_workitemcol = store.Query(WIQuery); // pass the query and return the result
// in collection
_returnResult = CreateDatatable(_workitemcol); // parse the query to
// return it as datatable
//Serialize DataTable to XML
StringWriter _streamData = new StringWriter();
_returnResult.WriteXml(_streamData);
return _streamData.ToString();
}
catch (Exception _exp)
{
throw new Exception(_exp.ToString());
}
}
第二种方法是更改 工作项
字段
[ComVisible(true), Description("Set workitem")]
public Boolean SetWorkItem(int WIID, string WIField, string Value)
{
TFSconnect();
try
{
WorkItem _workitem = store.GetWorkItem(WIID); // read WorkItem ID
_workitem.Fields[WIField].Value = Value;
_workitem.Save();
return true;
}
catch (Exception _exp)
{
throw new Exception(_exp.ToString());
}
}
下一步是使用 Assembly Linker 创建并使用强名称对程序集进行签名。从命令提示符,使用此命令生成 snk 文件
sn -k sgKey.snk
从您的应用程序中,右键单击项目并选择“签名”选项卡,然后浏览生成的文件

保存您的应用程序并确保它成功构建。
第二步
在完成构建访问 TFS 的 DLL 应用程序后,我们需要使用 VBA 代码从 Access 连接,但首先应该生成 TLB 文件,以便使用以下命令从 VBA 引用它:
C:\Program Files\Microsoft Visual Studio 9.0\VC> regasm [Source DLL] OutputDLL.tlb /codebase
现在打开一个新的 Access 项目,创建一个新的表单,并打开 Build
事件。
选择“工具”->“引用”->添加 TLB 文件。

然后,添加此代码以调用 工作项
类中的方法
Public Sub CallTFS()
Dim objectServer As New WorkItemTraking.WorkItemTraking
Dim returnXML As String
Dim returnValue As Boolean
'this method changes the workitem filed value
returnValue = objectServer.SetWorkItem(112, "Impact", "Low")
MsgBox (returnValue)
'this method changes the workitem filed value
returnXML = objectServer.GetWorkItem("SELECT [System.Id], [System.Title] _
FROM WorkItems WHERE [System.Id] in (47492, 47512)
ORDER BY [System.Id]")
MsgBox (returnXML)
End Sub
历史
- 2011 年 4 月 18 日:初始发布