MS 项目:如果列在任务视图中可见,如何签入 VBA?
MS project : how to check in VBA if a column is visible in tasks view?
我在 MSP 中写了一个 VBA 宏来插入一个列 (TaskColumn Flag20) 来显示特定的指标。除了每次打开项目文件时都会创建列外,它运行良好。因此,我正在寻找一种方法来检查该列是否存在并且在打开文件时是否可见。我找不到任何关于这种可能性的信息。
非常感谢。
一个更好的主意可能是 create/edit 具有包含所需列的自定义 table 的特定视图。然后你不需要检查任何东西,只需选择那个视图,它总是会给你你想要的。
如果您想自动显示视图,请编写一个使用 Application.ViewApplyEx 方法的宏。
这是一种以编程方式获取所有可见列的方法
'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
最终的工作代码,感谢 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
如果感兴趣的是测试单个列,请试试这个。
适用于其常量名称或用户分配的自定义名称。
Public Function IsColumnVisible(Name As String)
IsColumnVisible = False
On Error GoTo LeaveIsColumnVisible
SelectTaskColumn Column:=Name ' Will error out if the column is hidden
IsColumnVisible = True
LeaveIsColumnVisible:
End Function
我在 MSP 中写了一个 VBA 宏来插入一个列 (TaskColumn Flag20) 来显示特定的指标。除了每次打开项目文件时都会创建列外,它运行良好。因此,我正在寻找一种方法来检查该列是否存在并且在打开文件时是否可见。我找不到任何关于这种可能性的信息。
非常感谢。
一个更好的主意可能是 create/edit 具有包含所需列的自定义 table 的特定视图。然后你不需要检查任何东西,只需选择那个视图,它总是会给你你想要的。
如果您想自动显示视图,请编写一个使用 Application.ViewApplyEx 方法的宏。
这是一种以编程方式获取所有可见列的方法
'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
最终的工作代码,感谢 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
如果感兴趣的是测试单个列,请试试这个。 适用于其常量名称或用户分配的自定义名称。
Public Function IsColumnVisible(Name As String)
IsColumnVisible = False
On Error GoTo LeaveIsColumnVisible
SelectTaskColumn Column:=Name ' Will error out if the column is hidden
IsColumnVisible = True
LeaveIsColumnVisible:
End Function