word把脚注文本转换为普通文本——为机器翻译
事先声明:以下内容仅作参考,操作前请备份原文档。由于操作可能引起的后果,作者不承担责任。
有一份英文文档,含有大量脚注,上传某网站进行机器翻译,会发现脚注内容丢失,只翻译了普通文本的内容。如果脚注也需要翻译,那么在上传机翻之前,需要把脚注都转换为普通文本。
文档是按章分节的。现要求让脚注每节重新编号,编号样式为阿拉伯数字,转换后,编号样改为“[1]”的形式,所有脚注文本放在每章的最后,脚注文本的编号与正文中脚注引用的编号是一致的,且正文中脚注引用位置不变。
实现代码如下。代码含有详细注释,在本机测试,效果良好。
Sub 脚注转普通文本()
'按章分节。如果没有分节,请先分节。
'按节重新编号脚注,按节获得脚注
'在原文档内修改,可以保留原文档的格式不变。请注意备份原文档
'保持编号不变,编号的位置不变
'用于文档上传机器翻译
Dim FootN As Footnote, i As Long, j As Long
Dim FootRng As Range, FootNo As Long, FootTx As String
Dim Sec As Section, Rng As Range
'把脚注设置为按节重新编号,编号样式为阿拉伯数字
With ActiveDocument.Content.Footnotes
.NumberingRule = wdRestartSection
.NumberStyle = wdNoteNumberStyleArabic
.Location = wdBottomOfPage
End With
For Each Sec In ActiveDocument.Sections
i = 1
FootTx = ""
FootNo = Sec.Range.Footnotes.Count
If FootNo > 0 Then
'为本节内的脚注加上普通文本编号
For j = 1 To FootNo '用for each footn in Sec.Range.Footnotes 会出错,取得了全文本的脚注,奇怪!
Set FootN = Sec.Range.Footnotes(j)
FootN.Reference.InsertAfter "[" & i & "]" '在正文区脚注引用标记的后面加形如“[1]”的编号
Set FootRng = FootN.Range '脚注文本不包含其前的脚注引用标记
FootRng.InsertBefore "[" & i & "] " '插入的文本添加到了FootTx中
FootTx = FootTx & FootRng.Text & vbCrLf
i = i + 1
Next j
Set Rng = ActiveDocument.Range(Sec.Range.End - 1, Sec.Range.End - 1) '定义节的末尾一个位置,节的末尾是分节符,所以要-1
Rng.InsertAfter vbCrLf & vbCrLf '插入两个段落标志
Set Rng = ActiveDocument.Range(Sec.Range.End - 1, Sec.Range.End - 1)
Rng.Paragraphs(1).Style = wdStyleFootnoteText '设置为内置样式“脚本文本”
Rng.InsertAfter FootTx '插入脚注文本
End If
Next Sec
'删除全文档脚注
For Each FootN In ActiveDocument.Footnotes
FootN.Delete
Next
End Sub
很少有专门关于WordVBA的书。但WordVBA和excelVBA原理相通,要学习更多关于VBA的实用知识可以看。