更新Word 2013 SEQ字段

时间:2016-04-12 20:29:19

标签: field seq word-2013

我正在编写VBA宏来更新Word 2013文档中的仅SEQ字段。我设计它使用GoTo从文档的开始到结束一次一个地访问每个SEQ字段(NOT Update All)以确保我跳过其他字段类型。我努力让每个SEQ字段循环,直到文档结束。我希望它能在任何文档中工作,无论书签或其他结束标记如何。

这是我到目前为止的代码(附评论):

ActiveWindow.View.FieldShading = wdFieldShadingAlways '转到文档顶部    Selection.HomeKey单位:= wdStory '转到第一个SEQ字段

Selection.GoTo What:=wdGoToField, Which:=wdGoToNext, Count:=1, Name:="SEQ"

' Selection.Find.ClearFormatting

'只要有更多字段代码,请更新此字段并转到下一个字段代码 做Selection.GoToNext.wdGoToField = True     Selection.Fields.Update 环

1 个答案:

答案 0 :(得分:1)

有点晚了,但是当我寻找相同答案时,您的问题就出现了。 :-)

出于灵活性考虑,实际工作是作为一个函数完成的,其中,域代码以字符串形式接收。这样,我可以使用任何所需的域代码调用该函数,而不必一定是特定的SEQ或任何域代码。

<span id="config:inptext_date_from" class="date-config">
  <input name="config:inptext_date_from_day"  class="day" size="2" maxlength="2" value="29" />
  <select name="config:inptext_date_from_month" class="month" size="1">
    <option value="1" selected>January</option>      
    <option value="2" selected>Febuary</option>
  <input name="config:inptext_date_from_year" class="year" size="4" maxlength="4" value="2018" /> 
  <input name="config:inptext_date_from_hours" class="hours" size="2" maxlength="2" value="00" />:
  <input name="config:inptext_date_from_minutes" class="minutes" size="2" maxlength="2" value="00" />            
</span>

<span id="config:inptext_date_to" class="date-config">
  <input name="config:inptext_date_to_day"  class="day" size="2" maxlength="2" value="29" />
  <select name="config:inptext_date_to_month" class="month" size="1">
    <option value="1" selected>January</option>      
    <option value="2" selected>Febuary</option>
  <input name="config:inptext_date_to_year" class="year" size="4" maxlength="4" value="2018" /> 
  <input name="config:inptext_date_to_hours" class="hours" size="2" maxlength="2" value="00" />:
  <input name="config:inptext_date_to_minutes" class="minutes" size="2" maxlength="2" value="00" />            
</span>

我们正在遍历活动文档中的所有字段。您可以测试是否Function UpdateSpecificFields(MyFieldCode As String) Dim MyField As Field For Each MyField In ActiveDocument.Fields ' Debug.Print """" & MyField.Code & """" & MyField.Result If InStr(1, MyField.Code, MyFieldCode) <> 0 Then MyField.Update End If Next MyField End Function 以减少不必要的工作。

InStr在奇数间距的情况下存在;有时,字段创建者可能在代码本身之前或之后包含额外的空间,因此我不想太直白。 (我想应该有一个MyField.Type = wdFieldSequence才能删除上述空格,但我有点懒。)

用法:从子菜单中调用该函数。

trim()

我有一个名为Sub UpdateSEQQs() UpdateSpecificFields ("SEQ Qs") End Sub 的{​​{1}},因此您可以在上面看到它的命名方式。希望这对某人有帮助!