vba - Difficulties with Microsoft Project Wrapping Text within Column. No feature to check if already wrapped - Stack Overflow

admin2025-04-20  0

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
Share Improve this question edited Mar 3 at 0:55 braX 11.8k5 gold badges22 silver badges37 bronze badges asked Mar 2 at 23:41 WhazzzupWhazzzup 111 bronze badge 6
  • why is "on error resume next" there - any chance you are masking an error and not knowing why something is happening? – Ctznkane525 Commented Mar 3 at 0:49
  • I see the concern, and removing it wont impact anything. If anything that script was just another method to text. The first and second option would suffice, if I can determine how to know when the column is unwrapped. – Whazzzup Commented Mar 3 at 1:51
  • What version of Project are you using? You say you sometimes "lose" text wrapping when a filter is applied. What field are you wrapping and what filter are you applying? – john-project Commented Mar 3 at 16:38
  • I'm using Microsoft Project 2021, and the field I'm wrapping is the default "Name" field. If you mess with filters and grouping views on other columns, whether manually or automatically via VBA, you will lose wrapping on some rows depending on the amount of rows/data you have. – Whazzzup Commented Mar 3 at 18:39
  • Okay, "mess with" is rather subjective. How about an example so I can try to replicate what you are seeing. – john-project Commented Mar 3 at 19:03
 |  Show 1 more comment

1 Answer 1

Reset to default 1

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745114313a285772.html

最新回复(0)