phimuemue
phimuemue

Reputation: 36061

VBA/PowerPoint: Split date-time field into lines

I have a PowerPoint presentation, where I add an automatically updating date-time field to a text box (via Insert -> Text -> Date & Time, with checked "Update Automatically"). The textbox is resized so that the text (Wednesday, March 31, 2021) is split across two lines, for example:

  Wednesday,
March 31, 2021

I would like to process the information line-wise, trying the following VBA:

Sub TryLines()
    For Each Line In ActiveWindow.Selection.TextRange2.Lines
        Debug.Print Line
    Next Line
End Sub

If I call this function with the whole date-time field (distributed across two lines) selected, I get the following output:

Wednesday, March 31, 2021
Wednesday, March 31, 2021

I.e. PowerPoint recognizes that there are actually two lines, but is apparently not able to "break up" the date-time field into actual separate lines, and instead treats the date-time field as a monolith. A similar thing seems to happen for Characters.

Actual question: Is there a way to retrieve the text within a field line-wise? I would like to be independent of date format and of the actual wrapping.

Some more background, if helpful: At last, I would like to compute the polygon formed by the text-selection highlightning. Since I did not find a method to do this in TextRange2, I thought about splitting into lines and compute rectangles per line:

Sub TryToComputeLineBounds() ' call this while the date-time field is selected
    For Each Line In ActiveWindow.Selection.TextRange2.Lines
        Dim x(4) As Single, y(4) As Single
        Line.RotatedBounds x(1), y(1), x(2), y(2), x(3), y(3), x(4), y(4)
        Debug.Print Line
        For i = 1 To 4
            Debug.Print x(i) & " | "; y(i)
        Next i
    Next Line
End Sub

I realized that the measured points are the same per (what I assumed) line, and then found out that Lines does not actually give me the lines within a date-time field.

Upvotes: 0

Views: 116

Answers (2)

John Korchok
John Korchok

Reputation: 4923

As Morboss surmised, a date field is read as one line regardless of how it appears. In the user interface, if you click on a multiline text, the insertion point appears where you click. Not so with a date: the first time you click on it the entire field is selected and no insertion point appears.

As a workaround, get the day name and date with string parsing:

Sub GetDayName()
    Dim SpacePos As Long
    Dim FieldText As String, DayName As String, DateValue As String
    
    FieldText = ActiveWindow.Selection.TextRange2.Text
    SpacePos = InStr(FieldText, " ")
    DayName = Left(FieldText, SpacePos)
    DateValue = Right(FieldText, (Len(FieldText) - SpacePos))
    MsgBox DayName
    MsgBox DateValue
End Sub

Upvotes: 0

morboss
morboss

Reputation: 16

I think you're getting the Lines and Characters as a monolith because there is something special about inserted Date & Time that means it will never "break up". I tried your code with a text box that had some characters I typed in and it worked - I was able to read lines and characters one at a time.

So maybe make a temporary copy of the text box but without automatic date inserted, i.e. a Text value equivalent to the date, then use the copy to compute the polygon.

Upvotes: 0

Related Questions