从Word中的表格中读取日期,而无需其他字符 [英] Reading date from table in Word without additional characters

查看:98
本文介绍了从Word中的表格中读取日期,而无需其他字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我昨天开始使用VBA,并不断遇到麻烦.从长远来看,我试图创建一个Word模板,以检查它是否仍是最新的或是否需要修订.

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 revision.

现在,我想将文档中的日期存储在变量中.我找不到一种方法来直接读出日期格式的内容,所以现在我正在使用Selection.Text和CDate,但这会给我一个错误(不兼容的类型),因为我的选择似乎包含另一个字符或标记([]) .我猜想这与书签在Word文档中的表格单元格上有关,因为它在运行的文本中可以正常工作.

Right now I want to store a date from the document in a variable. I couldn't find a method to directly read out something in date format so now I'm using Selection.Text and CDate but that gives me an error (incompatible types) because my selection seems to contain another character or marker ([]). I'm guessing it has something to do with the fact that the bookmark is on a cell of a table within my Word document because it works fine in the running text.

我正在表格中进行此操作,因为这样可以确定相关日期在文档中的位置,并且由于不确定日期更改后如何重置书签.

I'm doing this in a table because this way I can be sure where the date in question is in the document and because I'm not sure how to reset the bookmark after the date has been changed.

我试图通过使用将选择限制为日期 Selection.SetRange开始:= 0,结束:= 8(还有一些变体),但是只选择一个空格和不祥标记(或完全选择另一个单元格).

I tried to limit the selection to the date by using Selection.SetRange Start:=0, End:=8 (and a few variations) but that selects only a space and the ominous marker (or another cell entirely).

我也研究了Ranges,但据我所知它不能解决我的问题,我还不能真正使用它们,所以现在我坚持选择.

I have also looked into Ranges but as far as I can tell it doesn't solve my problem and I can't really use them yet, so for now I'm sticking to selection.

这是我的代码:

Sub ChangeNextRev()

Dim nextRevision As Date
Dim RevisionDate As Date
Dim temp As String

'Selection.GoTo what:=wdGoToBookmark, Name:="lastRevision"
'Selection.SetRange Start:=0, End:=8

'Selection.GoTo what:=wdGoToBookmark, Name:="lastRevision"
Selection.GoTo what:=wdGoToBookmark, Name:="runningText"

temp = Selection.Text

RevisionDate = CDate(temp)
Debug.Print (RevisionDate)

nextRevision = RevisionDate + 14

With Selection

.GoTo what:=wdGoToBookmark, Name:="nextRevision"
.TypeText Text:=Format$(nextRevision, "DD.MM.YY")

End With

End Sub

有人可以指出我正确的方向吗?如何只选择需要的日期?除了表格以外,还有其他更简单的方法来控制输入日期或在之后找到日期吗? 对我要去哪里的任何帮助将不胜感激:)

Can someone point me in the right direction? How can I only select the date I need? Is there an easier way besides a table to control where the date is entered or to find it afterwards? Any help on where I'm going wrong would be greatly appreciated :)

推荐答案

您对表格单元格的猜测是正确的,但是您可以通过裁剪多余的字符来解决此问题.单元格末尾是Chr(13)+ Chr(7)(词段加单元格结构标记).

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

这篇关于从Word中的表格中读取日期,而无需其他字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆