我在MSP中编写了一个VBA宏来插入一列(TaskColumn Flag20)来显示特定指标。除了每次打开项目文件时都创建列,它运行良好。因此,我正在寻找一种方法来检查列是否存在,并在打开文件时可见。我找不到有关这种可能性的任何信息。
非常感谢。
这是一种以编程方式获取所有可见列的方法
'The function returns all of the visible column names as a delimiter separated string.
' Call with a string as the first parameter to represent a custom delimiter, or leave
' blank to use the default of a comma ,
Function GetColumns(Optional customDelimeter As String) As String
If customDelimeter = "" Then customDelimeter = "," 'handle custom delimeter
Dim viewableColumns As String 'create return value
SelectRow Row:=1, RowRelative:=False 'select the 1st row then parse all columns composing that row
For Each lngFieldID In MSProject.ActiveSelection.FieldIDList
Dim columnName As String
If lngFieldID > 0 Then
'convert the column ID to a string of the field name, either custom or built-in
columnName = Trim((CustomFieldGetName(lngFieldID)))
If Len(columnName) = 0 Then
columnName = Trim(FieldConstantToFieldName(lngFieldID)) ' use the built-in field name
End If
'append return value
viewableColumns = viewableColumns & customDelimeter & columnName
End If
Next
'get rid of the first delimeter
If Len(viewableColumns) > 0 Then
viewableColumns = Right(viewableColumns, Len(viewableColumns) - 1)
End If
GetColumns = viewableColumns
End Function
更好的想法可能是使用包含所需列的自定义表创建/编辑特定视图。然后你不必检查任何东西,只需选择那个视图,它总会给你你想要的东西。
如果要自动显示视图,请编写一个使用Application.ViewApplyEx方法的宏。
最终的工作代码,感谢Jerred S.
Public Sub CheckFlag20Column()
SelectRow Row:=1, RowRelative:=False 'select the 1st row then parse all columns composing that row
For Each lngFieldID In MSProject.ActiveSelection.FieldIDList
Dim columnName As String
If lngFieldID > 0 Then
columnName = Trim(FieldConstantToFieldName(lngFieldID)) ' use the built-in field name
If columnName = "Flag20" Then
SelectTaskColumn Column:="Flag20"
ColumnDelete
End If
End If
Next
End Sub