我已经设置了一些书签,当按下按钮时,会运行一个宏以在书签中插入日期,这很好用。如果我再次按下按钮,则在现有日期之前插入新日期。
我想知道使用什么代码删除旧日期并添加新日期。
我确实找到了列出我所有书签的代码。
Dim d As Document
'Set d = Documents.Add("G:\My Drive\Moss\CRM\Work in progress\action plan
\Wordchange.docm")
Set d = ActiveDocument
Dim b As Bookmark
Dim dt As Date
'dt = DateAdd("d", 21, DateTime.Now)
dt = DateAdd("d", 90, Date)
Set b = d.Bookmarks("weeksadd3m")
b.Range.Text = Format(dt, "dd/mm/yyyy")
我想在书签后清除文本,以便可以插入新日期。
感谢所有考虑这一点的人。
苔藓
答案 0 :(得分:0)
必须将书签设置为涵盖其新内容的范围。试试这样的代码:-
Private Sub SetBookMark()
Dim Doc As Document
Dim Rng As Range
Dim Mark As String
Dim Dt As Date
'Set Doc = Documents.Add("G:\My Drive\Moss\CRM\Work in progress\" & _
"action plan\Wordchange.docm")
Set Doc = ActiveDocument
Mark = "weeksadd3m"
Dt = DateAdd("m", 3, Date)
With Doc.Bookmarks
If .Exists(Mark) Then
Set Rng = .Item(Mark).Range
Rng.Text = Format(Dt, "dd/mm/yyyy")
.Add Mark, Rng
End If
End With
End Sub