我有一个模板,打开时会打开指定数据的用户表单。但是,如果我想重新运行宏来添加新数据,如何删除输入的数据(书签)。目前它只是增加了填充的内容。
Private Sub Cancelbut_Click()
PInfo.Hide
End Sub
Private Sub OKbut_Click()
Dim CompamyName As Range
Dim CompanyName2 As Range
Set CompanyName = ActiveDocument.Bookmarks("CName").Range
Set CompanyName2 = ActiveDocument.Bookmarks("CName2").Range
CompanyName.Text = Me.TextBox1.Value
CompanyName2.Text = Me.TextBox1.Value
Dim VendorName As Range
Set VendorName = ActiveDocument.Bookmarks("VName").Range
VendorName.Text = Me.TextBox2.Value
Dim ProjectName As Range
Dim ProjectName2 As Range
Set ProjectName = ActiveDocument.Bookmarks("PName").Range
Set ProjectName2 = ActiveDocument.Bookmarks("PName2").Range
ProjectName.Text = Me.TextBox3.Value
ProjectName2.Text = Me.TextBox3.Value
Dim ProjectCode As Range
Set ProjectCode = ActiveDocument.Bookmarks("PCode").Range
ProjectCode.Text = Me.TextBox4.Value
Me.Repaint
PInfo.Hide
End Sub
有人会如何清除现有数据?感觉这样可以帮助任何人尝试使用书签。我试图这样做,但最终删除了书签。
感谢帮助!
答案 0 :(得分:0)
写入书签(无论是用户还是使用代码)都会删除书签 - 这是设计使然。但是,在使用VBA时,您无法轻松地为用户提供解决方法......
如果您首先将Range
对象分配给书签范围,则可以再次为分配给范围的文本添加书签。由于您要写入大量书签,因此最好将这些操作放入单独的过程中,而不是在整个代码中重复这些操作,如下所述。
提示:如果您为第二个书签添加REF
字段(交叉引用),您可以自己保存一些代码,在那里您可以写两次信息。
Function WriteToBookmarkRetainBookmark(rng As Object, content As String)
Dim sBkmName As String
sBkmName = rng.Bookmarks(1).Name
rng.Text = content
rng.Document.Bookmarks.Add sBkmName, rng
End Function
Private Sub OKbut_Click()
Dim CompamyName As Range
Dim CompanyName2 As Range
Set CompanyName = ActiveDocument.Bookmarks("CName").Range
Set CompanyName2 = ActiveDocument.Bookmarks("CName2").Range
WriteToBookmarkRetainBookmark(CompanyName, Me.TextBox1.Value)
WriteToBookmarkRetainBookmark(CompanyName2, Me.TextBox1.Value)
Dim VendorName As Range
Set VendorName = ActiveDocument.Bookmarks("VName").Range
WriteToBookmarkRetainBookmark(VendorName, Me.TextBox2.Value)
Dim ProjectName As Range
Dim ProjectName2 As Range
Set ProjectName = ActiveDocument.Bookmarks("PName").Range
Set ProjectName2 = ActiveDocument.Bookmarks("PName2").Range
WriteToBookmarkRetainBookmark(ProjectName, Me.TextBox3.Value)
WriteToBookmarkRetainBookmark(ProjectName2, Me.TextBox3.Value)
Dim ProjectCode As Range
Set ProjectCode = ActiveDocument.Bookmarks("PCode").Range
WriteToBookmarkRetainBookmark(ProjectCode, Me.TextBox4.Value)
Me.Repaint
PInfo.Hide
End Sub