我正在尝试根据简单文本文件的内容插入一系列 Word 字段,但是当我运行此代码时,它会将所有字段插入在一起,并将所有换行符聚集在所有字段之后,而不是插入换行符在每个字段之间。祈祷告诉我,我做错了什么?
Option Explicit
Sub AddFields()
Dim fileName As String
fileName = InputBox("Filename containing field list")
Dim fso As New Scripting.FileSystemObject
Dim fileStream As Scripting.TextStream
Set fileStream = fso.OpenTextFile(fileName, ForReading, False)
Dim line As String
While Not fileStream.AtEndOfStream
line = fileStream.ReadLine
Selection.Range.InsertBreak WdBreakType.wdLineBreak
AddField line
Wend
End Sub
Sub AddField(mergeFieldName As String)
Dim fieldText As String
fieldText = "MERGEFIELD " & mergeFieldName & " "
ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:=fieldText, PreserveFormatting:=False
End Sub