解析Word Doc以插入交叉引用

时间:2013-12-16 23:26:39

标签: vba ms-word word-vba

我在带有书签的word文档中有某些文字。我想使用Word VBA解析相同单词的文档并插入交叉引用。出于某种原因,当我插入交叉引用时,代码不会移动到下一个单词。

Sub ReplaceTextwithCrossRef()

Dim BMtext As String
Dim BMname As String
Dim Sel As Selection
Set Sel = Application.Selection

BMname = Sel.Bookmarks(1).Name
BMtext = Sel.Text
MsgBox BMname
MsgBox BMtext

For Each oWd In ActiveDocument.Words

oWd.Select

If oWd.Text = BMtext Then

If Selection.Bookmarks.Exists(BMname) Then

Else

Selection.InsertCrossReference ReferenceType:=wdRefTypeBookmark, _
        ReferenceKind:=wdContentText, ReferenceItem:=BMname

Selection.MoveDown Unit:=wdLine, Count:=1

End If

Else

End If

Next oWd

End Sub

用户选择加书签的单词,代码移动到单词的下一个实例,并交叉引用它。即。

BOOKMARKEDITEM

WORDS1

WORDS2

BOOKMARKEDITEM

WORDS3

它将在BOOKMARKEDITEM的第二个实例上插入交叉引用,但它不会移动到WORDS3。它会卡住并不断回到交叉引用,即使我告诉它向下移动下一行代码。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我解决了自己的问题。使用'Do','With'和'If-Else'语句而不是循环遍历每个单词。我认为交叉参考插件由于某种原因搞砸了'For'循环。这是解决方案:

Sub ReplaceTextwithCrossRef()

    Dim BMtext As String
    Dim BMname As String
    Dim Counter As Long
    Dim Counter2 As Long

    Dim Sel As Selection
    Set Sel = Application.Selection

    'Select the bookmark
    BMname = Sel.Bookmarks(1).Name
    BMtext = Sel.Text
    MsgBox "This is the bookmark: " & BMname
   ' MsgBox BMtext

    'Select all of the document and search
    ActiveDocument.Range.Select
    Do
        With Selection.Find
            .ClearFormatting
            .Text = BMtext
            .Replacement.Text = ""
            .Format = False
            .MatchWildcards = False
            .Wrap = wdFindStop
            .Execute
        End With

        If Selection.Find.Found Then
        'Overall counter
            Counter = Counter + 1
                'Check if the select is bookmarked
                If Selection.Bookmarks.Exists(BMname) Then
                    'Do nothing and move on
                Else
                    'Insert the cross referebce
                    Selection.InsertCrossReference ReferenceType:=wdRefTypeBookmark, _
                    ReferenceKind:=wdContentText, ReferenceItem:=BMname
                    Counter2 = Counter2 + 1
                End If
        End If
    Loop Until Not Selection.Find.Found

    'Tell how many we found
    MsgBox "We found " & Counter & " instances of " & BMtext & " and " & Counter2 & " cross references were made."

End Sub

编辑:添加了添加Charformat的代码

如果要在插入交叉引用之前保留原始格式,请在“Counter2”和End If语句之间使用以下代码来编辑字段代码。我在网上搜索得很长,找到了可行的东西,这就是我想出的:

    Dim oField As Field
    Dim sCode As String
    'Move left and select the reference
                    Selection.MoveLeft Unit:=wdWord, Count:=1
                    Selection.Expand Unit:=wdWord
    'Check reference and add Charformat
                    For Each oField In Selection.Fields
                        If oField.Type = wdFieldRef Then
                            sCode = oField.Code.Text
                            If InStr(sCode, "Charformat") = 0 Then oField.Code.Text = sCode & "\*Charformat"
                        End If
                    Next
    'Move the cursor past the cross reference
                    Selection.Fields.Update
                    Selection.MoveRight Unit:=wdWord, Count:=1