Reading date from table in Word without additional characters

我只是一个虾纸丫 提交于 2019-12-02 10:40:15

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

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), ".", "/"))

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!