我尝试在Word文档的标题内插入字段,而不是在它们之前或之后插入字段。这是为了准备导入Madcap Flare的Word文件,Madcap Flare允许在私有的Word字段中指定文件名。下面的代码不起作用,因为字段被附加在标题开头之前,需要嵌入其中。我该怎么做呢。
Sub prepareDocForImport()
Dim headingText As String '
With Selection.Find
.ClearFormatting
.Wrap = wdFindContinue
.Forward = True
.Format = True
.MatchWildcards = False
.Text = ""
.Style = ActiveDocument.Styles("Heading 1 ")
.Execute
While .Found
headingText = Selection.Range.Text
headingText = Replace(headingText , " ", "_")
headingText = LCase(headingText )
Selection.Collapse Direction:=wdCollapseStart
Set myField = ActiveDocument.Fields.Add(Range:=Selection.Range, Type:=wdFieldEmpty, Text:="PRIVATE:MADCAP:FILENAME<" & headingText & ">")
.Execute
Wend
End With
End Sub发布于 2017-07-20 16:40:36
尝试将光标移动到标题单词中的一个字符。
我还添加了一行代码,以确保搜索从文档的开头开始。
Sub prepareDocForImport()
Dim headingText As String
Dim myfield As Field
'Moving to beginning of doc in case a different starting point is selected
Selection.HomeKey wdStory
With Selection.Find
.ClearFormatting
.Wrap = wdFindContinue
.Forward = True
.Format = True
.MatchWildcards = False
.Text = ""
.Style = ActiveDocument.Styles("Heading 1 ")
.Execute
While .Found
headingText = Selection.Range.Text
headingText = Replace(headingText, " ", "_")
headingText = LCase(headingText)
Selection.Collapse Direction:=wdCollapseStart
'Move seleection one character into the header text
Selection.MoveRight Count:=1
Set myfield = ActiveDocument.Fields.Add(Range:=Selection.Range, _
Type:=wdFieldEmpty, _
Text:="PRIVATE:MADCAP:FILENAME<" & headingText & ">", _
PreserveFormatting:=True)
.Execute
Wend
End With
End Subhttps://stackoverflow.com/questions/45194968
复制相似问题