删除内容控制文本框中的尾随空格

问题描述 投票:0回答:1

我在工作中涉及很多标准租赁。租赁模板很长,但很简单。我们的律师助理只需将客户特定信息从我们的数据库复制并粘贴到相关字段即可。但这些字段分散在模板文档中,这意味着无意的用户错误并不罕见。

我创建了一个 .dot 文件并添加内容控件,以便我们的律师助理收到一个对话框提示,并且可以在一处输入所有需要的信息。问题是,他们喜欢双击源数据库中的信息,当粘贴到 .dot 字段时,这些信息总是有一个尾随空格。如何修改我的脚本以便自动忽略所有尾随空格?

我的VBA代码:

Private Sub UserForm_Initialize()
Caption = "Client Long-form Agreement Template"
Label1.Caption = "Site Name"
Label2.Caption = "Site Number"
Label3.Caption = "Lease Title and Date"
Label4.Caption = "Monthly Fee"
Me.TextBox4.Value = "6,500"
CommandButton1.Caption = "OK"
CommandButton2.Caption = "Cancel"

End Sub

Private Sub CommandButton1_Click()
'Hide the userform
Hide
'Loop through the controls in the document
For Each oCC In ActiveDocument.ContentControls
If oCC.Title = "Site Name" Then oCC.Range.Text = TextBox1.Text
If oCC.Title = "Site Number" Then oCC.Range.Text = TextBox2.Text
If oCC.Title = "Lease Title and Date" Then oCC.Range.Text = TextBox3.Text
If oCC.Title = "Monthly Fee" Then oCC.Range.Text = TextBox4.Text
Next oCC
Dim oHeader As HeaderFooter
'Loop through the headers in Section 1. If there are more sections, you will also have to loop through the sections
For Each oHeader In ActiveDocument.Sections(1).Headers
'Loop through the headers in Section 1
For Each oCC In oHeader.Range.ContentControls
If oCC.Title = "Site Name (H)" Then oCC.Range.Text = TextBox1.Text
If oCC.Title = "Site Number (H)" Then oCC.Range.Text = TextBox2.Text

Next oCC
Next oHeader

'Unload the form
Unload Me
End Sub

Private Sub CommandButton2_Click()
'User has cancelled so unload the form
Unload Me

End Sub

老实说我不知道该怎么做。我猜解决方案是有一个 if 语句。但是,虽然某些字段仅包含一个单词,但其他字段可能包含多个单词。所以我不能删除所有空格。

vba ms-word
1个回答
0
投票
  • Trim
    函数删除前导空格和尾随空格
If oCC.Title = "Site Name" Then oCC.Range.Text = Trim(TextBox1.Text)
© www.soinside.com 2019 - 2024. All rights reserved.