I created multiple scripts to wrap my default task "Name", however, each one has issues that I can't correct and don't know the proper solution to wrap a column before printing to PDF.
Option #1 – The easiest and best option is to toggle the column to wrap text correctly with just the two lines of code below, but the issue with this is I need to determine if the column is already wrapped or else it will unwrap prior to printing with VBA. This column should always be wrapped to begin with, however, when I filter other columns, the wrapping gets removed sometimes for whatever reason. I also considered making a counter++ to determine my WrapText status, but since the program itself modifies the text wrapping when grouping/filtering, I can't figure it out.
SelectTaskColumn Column:="Name"
WrapText
Option #2 – And it appears the AutoWrap command has no way of checking if the column is already wrapped, because the code below never outputs as "No"
Sub AutoWrap()
If ActiveProject.TaskTables("Entry").TableFields(3).AutoWrap = False Then
MsgBox "No"
SelectTaskColumn Column:="Name"
WrapText
Else
MsgBox "Yes"
End If
End Sub
Option #3 – And finally, my last attempt is able to turn on WrapText with the TableEditEx command, however, this option, only wrap a random amount of rows. Meaning that column is only wrap from rows 1-100, and 101-500 etc. are not wrapped. I figured this was an issue due the the screen needing to refresh, but I haven't confirmed.
Sub AutoWrap_ForceRefresh()
Dim prjApp As MSProject.Application
Dim currentTable As String
Dim tempView As String
Set prjApp = MSProject.Application
prjApp.ScreenUpdating = False
currentTable = ActiveProject.currentTable
' Toggle wrap OFF and ON again to force refresh.
On Error Resume Next
prjApp.TableEditEx Name:=currentTable, TaskTable:=True, FieldName:="Name", NewFieldName:="Name", Width:=50, WrapText:=False, ShowInMenu:=True
prjApp.TableEditEx Name:=currentTable, TaskTable:=True, FieldName:="Name", NewFieldName:="Name", Width:=100, WrapText:=True, ShowInMenu:=True
On Error GoTo 0
' Force a full refresh by switching views. Not sure if it matters.
tempView = prjApp.ActiveProject.Views(1).Name ' Store a temporary view name (e.g., first available view)
prjApp.ViewApply "Gantt Chart" ' Switch to Gantt Chart temporarily
prjApp.ViewApply "Task Sheet" ' Switch back to Task Sheet
' Re-enable screen updating.
prjApp.ScreenUpdating = True
DoEvents
Set prjApp = Nothing
End Sub
I created multiple scripts to wrap my default task "Name", however, each one has issues that I can't correct and don't know the proper solution to wrap a column before printing to PDF.
Option #1 – The easiest and best option is to toggle the column to wrap text correctly with just the two lines of code below, but the issue with this is I need to determine if the column is already wrapped or else it will unwrap prior to printing with VBA. This column should always be wrapped to begin with, however, when I filter other columns, the wrapping gets removed sometimes for whatever reason. I also considered making a counter++ to determine my WrapText status, but since the program itself modifies the text wrapping when grouping/filtering, I can't figure it out.
SelectTaskColumn Column:="Name"
WrapText
Option #2 – And it appears the AutoWrap command has no way of checking if the column is already wrapped, because the code below never outputs as "No"
Sub AutoWrap()
If ActiveProject.TaskTables("Entry").TableFields(3).AutoWrap = False Then
MsgBox "No"
SelectTaskColumn Column:="Name"
WrapText
Else
MsgBox "Yes"
End If
End Sub
Option #3 – And finally, my last attempt is able to turn on WrapText with the TableEditEx command, however, this option, only wrap a random amount of rows. Meaning that column is only wrap from rows 1-100, and 101-500 etc. are not wrapped. I figured this was an issue due the the screen needing to refresh, but I haven't confirmed.
Sub AutoWrap_ForceRefresh()
Dim prjApp As MSProject.Application
Dim currentTable As String
Dim tempView As String
Set prjApp = MSProject.Application
prjApp.ScreenUpdating = False
currentTable = ActiveProject.currentTable
' Toggle wrap OFF and ON again to force refresh.
On Error Resume Next
prjApp.TableEditEx Name:=currentTable, TaskTable:=True, FieldName:="Name", NewFieldName:="Name", Width:=50, WrapText:=False, ShowInMenu:=True
prjApp.TableEditEx Name:=currentTable, TaskTable:=True, FieldName:="Name", NewFieldName:="Name", Width:=100, WrapText:=True, ShowInMenu:=True
On Error GoTo 0
' Force a full refresh by switching views. Not sure if it matters.
tempView = prjApp.ActiveProject.Views(1).Name ' Store a temporary view name (e.g., first available view)
prjApp.ViewApply "Gantt Chart" ' Switch to Gantt Chart temporarily
prjApp.ViewApply "Task Sheet" ' Switch back to Task Sheet
' Re-enable screen updating.
prjApp.ScreenUpdating = True
DoEvents
Set prjApp = Nothing
End Sub
The AutoWrap property for the TableField object is read/write, but....it doesn't quite work like you'd expect. Using the Immediate window, this simple statement appears to give the value. print activeproject.TaskTables("Entry").TableFields(4).autowrap Result > true However, when I manually edit the Entry table and set the Name field text wrapping to false, the above statement still shows "true". Interesting.
So then I tried setting the AutoWrap true or false with the following statement: Activeproject.TaskTables("Entry").TableFields(4).AutoWrap = true (or false)
By doing that, the "read" statement above gives the correct status every time. That leads me to believe setting the text wrap to true as a regular part of your overall macro code will ensure the Name field stays wrapped. What I don't know, and this is why I wanted to try and replicate your "lose text wrapping via filter application" (i.e. "mess with") problem, if the above will actually solve the problem you are seeing.
Just for interest, your Option 2 selects Entry view table field 3 as the Name field. Did you modify the default Entry table? On my installation the default Entry view table Name field is field 4.