2017-06-11 26 views
1

前綴:我正在運行代碼,根據Text1字段中的值更改任務行的格式。在MS-Project 2007中的FormatTask之後無法使用「撤消」宏運行

所以,如果我改變Duration,或Finish或當我更新時間表一些其它值,Text1(定製字段)的值被modied爲好。作爲該值的結果,我正在格式化背景顏色和字體顏色。

問題:一旦我運行此代碼,我不能使用常規的「撤消」,並且我無法讓值返回到更新前的先前狀態。

任何幫助如何創建「自定義撤消」高度讚賞。

ThisProject代碼

Private Sub Project_Change(ByVal pj As Project) 
' enable class to modify the Task format on Project change (when a task is changed) 

StatusRYGFieldUpdate 

End Sub 

常規代碼模塊

Option Explicit 

Public StatusRYGView    As New clsTskUpdate 
Public UpdateViewFlag    As Boolean 
Public TskIDChanged     As Long 


Sub StatusRYGFieldUpdate() 

' this Sub is triggered once a task is modified 
' if the Field being modifed is related to "Text1" 

Dim CurTskID As Long 

Set StatusRYGView.ProjApp = Application 

Application.Calculation = pjManual 
Application.ScreenUpdating = False 

If UpdateViewFlag Then 
    CurTskID = TskIDChanged ' save Row ID 
    FormatTask (TskIDChanged) ' call the Sub that formats the cell (send the taskId) 
End If 

Application.Calculation = pjAutomatic 
Application.ScreenUpdating = False 

End Sub 

'=========================================================== 

Sub FormatTask(TskID) 

Dim Tsk   As Task 

If UpdateViewFlag Then 

    SelectTaskField TskID, "Text1", False 
    Set Tsk = ActiveCell.Task ' set the Task to current cell's Task 
    SelectRow Row:=TskID, RowRelative:=False 

    ' format entire row first 
    Select Case Tsk.Text1 ' Get the Field's used field, not name 
     Case "R" 
      FontEx CellColor:=7, Color:=0 
      FontEx Italic:=False 

     Case "Complete" 
      FontEx Italic:=True 
      FontEx CellColor:=15, Color:=14 ' Background Silver ; font Gray 

    End Select 

    ' format "Status" field 
    SelectTaskField TskID, "Text1", False 

    Select Case Tsk.Text1 ' Get the Field's used field, not name 
     Case "R" 
      ' Font Color:=pjWhite ' Font White 
      FontEx Italic:=False 
      FontEx CellColor:=1, Color:=7 ' Background Red ; font White 

     Case "Complete" 
      FontEx Italic:=True ' Font Italic 
      FontEx CellColor:=15, Color:=14 ' Background Silver ; font Gray 

    End Select 
End If ' UpdateViewFlag is True 

End Sub 

clsTskUpdate類模塊

Option Explicit 

Public WithEvents ProjApp As Application 

Private Sub ProjApp_ProjectBeforeTaskChange(ByVal Tsk As Task, ByVal Field As PjField, ByVal NewVal As Variant, Cancel As Boolean) 

' Sub (in "clsTskUpdate" Class) is triggered once a task is modified 
' if the Field being modifed is related to "Text1" 
' then the UpdateViewFlag is being raised, and the Tsk.ID (task's row) is saved to TskIDChanged variable 

UpdateViewFlag = False 
TskIDChanged = 0 

Select Case Field 
    Case pjTaskActualFinish 
     If Not NewVal Like Format(Tsk.ActualFinish, myDateFormat) Then ' need to modify date format to "dd/mm/yy" 
      LastValue = Tsk.ActualFinish 
      UpdateViewFlag = True 
      TskIDChanged = Tsk.ID 
     End If 

    Case pjTaskStart 
     If Not NewVal Like Format(Tsk.Start, myDateFormat) Then ' need to modify date format to "dd/mm/yy" 
      LastValue = Tsk.Start 
      UpdateViewFlag = True 
      TskIDChanged = Tsk.ID 
     End If 

    Case pjTaskDuration 
     If Not NewVal Like (Tsk.Duration/480) & "*" Then ' need to divide by 480 (in minutes) and add `*` wild-card for "days" 
      LastValue = Tsk.Duration/480 
      UpdateViewFlag = True 
      TskIDChanged = Tsk.ID 
     End If 

    Case pjTaskPercentComplete 
     If Not NewVal Like Tsk.PercentComplete Then 
      LastValue = Tsk.PercentComplete 
      UpdateViewFlag = True 
      TskIDChanged = Tsk.ID 
     End If 

    ' other possible Case Scenarios in the future 

End Select 

End Sub 
+0

請說明您是通過更新的問題標題中使用其MS Project版本。 –

+0

@RachelHettinger它是2007年, –

回答

1

的Microsoft Project 2007年增加了一對方法,OpenUndoTransactionCloseUndoTransaction,這爲用戶創造撤消整個宏觀單撤銷條目。

添加這些方法步驟StatusRYGFieldUpdate這樣的:

Sub StatusRYGFieldUpdate() 

    Dim CurTskID As Long 

    Set StatusRYGView.ProjApp = Application 

    Application.OpenUndoTransaction "Status RYG Field Update" 
    Application.Calculation = pjManual 
    Application.ScreenUpdating = False 

    If UpdateViewFlag Then 
     CurTskID = TskIDChanged ' save Row ID 
     FormatTask (TskIDChanged) ' call the Sub that formats the cell (send the taskId) 
    End If 

    Application.Calculation = pjAutomatic 
    Application.ScreenUpdating = True 
    Application.CloseUndoTransaction 

End Sub 
+0

謝謝瑞秋!這是我在過去幾年中無法在Google中找到的一項強大功能。我想我並不完全知道搜索詞。和在哪裏搜索。有一件事,當使用它時,它實際上不會返回到上一步。當我選擇之前的一個步驟(在撤消列表中),即「Set TaskField」時,它將起作用(這是最後一個「狀態RYG字段更新」之前的1個撤消列表)。你知道爲什麼嗎? –

+0

@ ShaiRado成對的Undo方法應該包含所有的代碼;我編輯了答案,把'OpenUndoTransaction'方法作爲第一個被調用的方法,並把'CloseUndoTransaction'作爲最後一個方法,同時注意,最後一次調用' ScreenUpdating'方法應該設置爲True。 –

+0

這非常好,非常感謝!你是我的MS-Project VBA(等待下一個)的人(女孩) –