循环遍历Microsoft项目添加到组合框中的所有表字段



我试图在VBA中创建一个用户表单,并使用一个列出所有可能的TableFields(?(的组合框。

更新的代码: 使用@dbmitch提供的代码和一些自由泳。 这将列出一个包含"原始"和"自定义"字段名称(如果存在(的两列组合框。它只列出活动项目中使用的字段。并非所有可能的字段。但是,如果该字段无论如何都不在活动项目中使用......也许这是最好的!

Public strResult2 As String ' Used for custom field names
Private Sub UserForm_Initialize()
Dim objProject      As MSProject.Project
Dim tskTable        As MSProject.Table
Dim tskTables       As MSProject.Tables
Dim tskTableField   As MSProject.TableField
Dim strFieldName    As String

'ComboBoxColA.ListWidth = "180" 'Uncomment for wider dropdown list, without wider box
Set objProject = Application.ActiveProject
Set tskTables = objProject.TaskTables

With ComboBox1 'Adds one blank line at the top
.ColumnCount = 2
.AddItem ""
.Column(1, 0) = "BLANK"
End With
' Loop through all tables
For Each tskTable In tskTables
' Loop through each field in each table
For Each tskTableField In tskTable.TableFields
strFieldName = GetFieldName(tskTableField)
If Len(strFieldName) = 0 Then GoTo SKIPHERE
With ComboBox1
.Value = strFieldName
' Check if allready exists
If .ListIndex = -1 Then
' Then sort alphabetically
For x = 0 To .ListCount - 1
.ListIndex = x
If strFieldName < .Value Then
.AddItem strFieldName, x
.Column(1, x) = strResult2
GoTo SKIPHERE
End If    
Next x
.AddItem strFieldName
End If
End With
SKIPHERE:
Next
Next
Set objProject = Nothing
Set tskTable = Nothing
Set tskTables = Nothing
Set tskTableField = Nothing
End Sub

功能

Private Function GetFieldName(ByVal objField As MSProject.TableField) As String
' find the field name and column header for a field (column) in a data table
'strResult is placed in column 0 in ComboBox
'strResult2 is placed in column 1 in ComboBox
Dim lngFieldID As Long
Dim strResult As String
lngFieldID = objField.Field
With objField.Application
strResult = Trim(.FieldConstantToFieldName(lngFieldID))
On Error GoTo ErrorIfMinus1 ' CustomField does not handle lngFieldID= -1
If Len(Trim(CustomFieldGetName(lngFieldID))) > 0 Then strResult2 = " (" & Trim(CustomFieldGetName(lngFieldID)) & ")" Else strResult2 = ""
End With
GetFieldName = strResult
Exit Function
ErrorIfMinus1:
strResult2 = ""
Resume Next
End Function

@dbmitch帮助我让这段代码工作。谢谢!

该链接非常有用,因为它显示了通过 MS Project 对象模型可用的属性和方法。您应该能够通过稍微更改它来将其修改为 VBA 格式。

更有用的是显示您提到的代码......

我找到了让我列出当前表中的所有字段的代码

无论如何,查看此代码是否按照您的问题中所述执行您想要的操作

Sub LoadFieldNames()
Dim objProject      As MSProject.Project
Dim tskTable        AS MSProject.Table 
Dim tskTables       AS MSProject.Tables
Dim tskTableField   AS MSProject.TableField 
Dim strFieldName    AS String
Set objProject = Application.ActiveProject
Set tskTables  = objProject.TaskTables
' Loop thru all tables
For Each tskTable In tskTables
' Loop through each field in each table
For Each tskTableField in tskTable.TableFields
strFieldName = GetFieldName(tskTableField)
ComboBox1.AddItem strFieldName
Next
Next
Set objProject = Nothing
Set tskTable = Nothing
Set tskTables = Nothing
Set tskTableField = Nothing
End Sub

尝试从这篇文章中添加函数以创建函数GetFieldName...它应该编译

Private Function GetFieldName(ByVal objField As MSProject.TableField) As String
' find the field name (actually colmn heading) for a field (column) in a data table
Dim lngFieldID As Long
Dim strResult As String
lngFieldID = objField.Field
With objField.Application
strResult = Trim(objField.Title) ' first choice is to use the title specified for the column in the table
If Len(strResult) = 0 Then
' try to get the custom field name- this will come back blank if it's not a custom field
strResult = Trim((CustomFieldGetName(lngFieldID)))
End If
If Len(strResult) = 0 Then
strResult = Trim(.FieldConstantToFieldName(lngFieldID)) ' use the field name
End If
End With
GetFieldName = strResult
End Function

相关内容

  • 没有找到相关文章

最新更新