Reading date from table in Word without additional characters

前端 未结 3 602
无人共我
无人共我 2021-01-28 19:28

so I started with VBA yesterday and keep running into walls. In the long run, I\'m trying to create a Word template that checks if it\'s still up to date or if it\'s time for re

相关标签:
3条回答
  • 2021-01-28 19:43

    You could try something along these lines

    Sub test()
    
    Dim d As Date
    
    d = CDate(Replace(ThisDocument.GoTo(wdGoToBookmark, , , "TEST_BM").Text, ".", "/"))
    
    Debug.Print d
    
    End Sub
    
    0 讨论(0)
  • 2021-01-28 20:03

    If the date is the only content in the cell you could use:

    Dim Dt As Date
    Dt = CDate(Replace(Split(ActiveDocument.Bookmarks("runningText").Range.Text, vbCr)(0), ".", "/"))
    
    0 讨论(0)
  • 2021-01-28 20:05

    Your guess about the table cell is correct, but you can work around that by trimming off the extraneous character(s). End-of-cell is a Chr(13) + Chr(7) (Word paragraph plus cell structure marker).

    There are various ways to code this, but I have the following function at-hand:

    'Your code, relevant lines, slightly altered:
      Selection.GoTo what:=wdGoToBookmark, Name:="runningText"
    
      temp = TrimCellText(Selection.Text)
    
      RevisionDate = CDate(temp)
      Debug.Print (RevisionDate)
    
    'Function to return string without end-of-cell characters
    Function TrimCellText(s As String) As String
        Do While Len(s) > 0 And (Right(s, 1) = Chr(13) Or Right(s, 1) = Chr(7))
            s = Left(s, Len(s) - 1)
        Loop
        TrimCellText = s
    End Function
    
    0 讨论(0)
提交回复
热议问题