返回文本交叉引用的方法是,在VBA中具有标题引用时,如何返回文本?

huangapple go评论47阅读模式
英文:

How can I return the text of a cross-reference when I have a reference to the heading, all inside VBA?

问题

I can help you translate the provided code into Chinese. Here's the translated code:

我想能够在VBA中返回交叉引用的完整段落文本,当我已经有一个对段落标题的交叉引用时。我需要在不修改原始字段代码的情况下完成这个操作。

例如,如果我创建一个对段落标题的交叉引用,字段代码将看起来类似于`{ REF _Ref136248344 \r \h}`,并将返回类似于`1.23`的内容。我可以手动通过移除`\r`将其转换为段落本身的文本,然后会得到`我引用的段落的完整文本`。

**我尝试过的方法**

我尝试使用VBA中的Field对象来获取原始引用(例如`{ REF _Ref136248344 \r \h}`),然后运行`Replace$(strCode, "\r", "")`来去掉`\r`并得到我需要的字段代码(在此示例中为`{ REF _Ref136248344 \h}`),但我找不到任何方法来在VBA中运行这个新的字段代码以获取结果。是否可以创建一个新的字段对象,将此字符串应用到`Field.Code`上,然后获取`Field.Code.Result`?

这是我的当前代码,它不起作用:

```vba
Public Function getTextFromCrossRef()
With Selection
        If .Fields.Count > 0 Then
            Dim myField As Field, crossRefText As String, crossRefResult As String
            Set myField = .Fields(1)
            crossRefText = "交叉引用 " & myField.result & " 是:"
            crossRefResult = getFullTextFromFieldCode(myField)
                 
            modCallUserforms.callDisplayMesageNotModal "交叉引用的结果", crossRefText, crossRefResult
        Else
            MsgBox "选择中未找到交叉引用"
        End If
    End With
End Function

Public Function convertFieldCodeFromHeadingToText(strCode As String) As String
    convertFieldCodeFromHeadingToText = Replace$(strCode, "\r", "")
End Function

'这是我尝试将字段代码应用于新字段的尝试 - 这不起作用
Public Function getFullTextFromFieldCode(inputField As Field) As String
    Dim newField As Field
    Dim inputFieldCode As String
    Dim result As String
    
    inputFieldCode = convertFieldCodeFromHeadingToText(inputField.Code)
    With newField
        .Code = inputFieldCode
    End With
    result = newField.result
End Function

也许另一种选择是使用字段代码返回到原始引用点,然后从那里复制?

我无法将新的字段代码复制并粘贴回原始文档。

我希望避免创建一个新的Word文档,想要的是快速支持新的动态查找。

最终结果

我进行了大量的合同审查,需要来回滚动到多个引用点,这些引用点通常相当复杂。我正在构建一个小型用户窗体,将在屏幕上返回交叉引用的文本,放在引用点旁边,以提高可用性并加快审查速度。

编辑

我尝试创建一个新字段并直接编辑它,但同时也修改了旧字段。我认为Word正在使用_Ref来识别它们。目前看来似乎无法做到我想要的。


请注意,我已经将代码进行了简单的翻译,但在中文环境下运行可能需要进一步的调整和测试。

<details>
<summary>英文:</summary>

I want to be able to return the full paragraph text of a cross-reference in VBA, when I already have a cross-reference to the paragraph heading. I need to do this without modifying the original field code.

For example, if I create a cross-reference to a paragraph heading, the field code will look something like `{ REF _Ref136248344 \r \h}` and will return something like `1.23`. I can manually convert this to the text of the paragraph itself by removing the `\r`, which will then give `the full text of the paragraph I&#39;m cross-referencing`.

**What I&#39;ve tried**

I&#39;ve tried using the Field object in VBA to get the original reference (e.g. `{ REF _Ref136248344 \r \h}` ), then running `Replace$(strCode, &quot;\r&quot;, &quot;&quot;)` to take the `\r` out and give the field code I need (in this example. `{ REF _Ref136248344 \h}` , but I can&#39;t find any way of then running this new field code in VBA to get the result. Is it possible to make a new field object, apply this string to `Field.Code` and then get `Field.Code.Result`?

This is my current code, which doesn&#39;t work:

Public Function getTextFromCrossRef()
With Selection
If .Fields.Count > 0 Then
Dim myField As Field, crossRefText As String, crossRefResult As String
Set myField = .Fields(1)
crossRefText = "Cross-reference " & myField.result & " is:"
crossRefResult = getFullTextFromFieldCode(myField)

        modCallUserforms.callDisplayMesageNotModal &quot;Result of cross-reference&quot;, crossRefText, crossRefResult
    Else
        MsgBox &quot;No cross-references found in selection&quot;
    End If
End With

End Function

Public Function convertFieldCodeFromHeadingToText(strCode As String) As String
convertFieldCodeFromHeadingToText = Replace$(strCode, "\r", "")
End Function

'This was my attempt to apply a field code to a new field - this doesn't work
Public Function getFullTextFromFieldCode(inputField As Field) As String
Dim newField As Field
Dim inputFieldCode As String
Dim result As String

inputFieldCode = convertFieldCodeFromHeadingToText(inputField.Code)
With newField
    .Code = inputFieldCode
End With
result = newField.result

End Function


Possibly another option is to use the field code to get back to the original reference point, then copy from there? 

I can&#39;t copy and paste the new field code back into the original document.

I&#39;d prefer to avoid having to create a new Word document, the idea is for this to be quick and support new lookups dynamically.

**End result**

I do a lot of contract reviews, which necessitate scrolling backwards and forwards to multiple references, which are often quite complex. I&#39;m building a small userform that will return the text of a cross-reference on screen, next to the place it is referenced, to aid usability and speed up review.

**Edit**

I tried creating a new field and editing it directly, but this also modifies the old field at the same time. I assume Word is using the _Ref to identify them. At the moment, it doesn&#39;t look like it&#39;s possible to do what I want.

Public Function getFullTextFromFieldCode(inputField As Field) As String
Dim newField As Field
Dim result As String
Dim newFieldCodeText As String

Set newField = inputField
newFieldCodeText = inputField.Code.text
newFieldCodeText = Replace$(newFieldCodeText, &quot;\r&quot;, &quot;&quot;)
newField.Code.text = newFieldCodeText
newField.Update &#39;This line also changes inputField to match newField
result = newField.result.text

getFullTextFromFieldCode = result

End Function


</details>


# 答案1
**得分**: 2

你的代码无法正常工作,因为`newField`只是一个带有Field数据类型的对象变量,它并不是一个字段,所以它无法具有结果。

实现你想要的最简单方法是临时更改字段代码,更新字段以获取结果,然后再将其改回来。

```VBA
Public Function getTextFromCrossRef()
    With Selection
        If .Fields.Count > 0 Then
            Dim myField As Field, crossRefText As String, crossRefResult As String, fldCode As String
            Set myField = .Fields(1)
            crossRefText = "Cross-reference " & myField.result & " is:"
            crossRefResult = getFullTextFromFieldCode(myField)
            modCallUserforms.callDisplayMesageNotModal "Result of cross-reference", crossRefText, crossRefResult
        Else
            MsgBox "No cross-references found in selection"
        End If
    End With
End Function

Private Function getFullTextFromFieldCode(inputField As Field) As String
    Dim origFieldCode As String, editedFieldCode As String
    
    origFieldCode = inputField.Code
    editedFieldCode = Replace$(inputField.Code, "\r", "")
    With inputField
        .Code = editedFieldCode
        .Update
        getFullTextFromFieldCode = .result
        .Code = origFieldCode
        .Update
    End With
End Function

以上是你要的翻译内容。

英文:

Your code isn't working because newField is simply an object variable with the datatype of Field. It does not exist as a field, so it cannot have a result.

The simplest way of achieving what you want is to temporarily change the field code, update the field to get the result, and change it back again.

Public Function getTextFromCrossRef()
    With Selection
        If .Fields.Count &gt; 0 Then
            Dim myField As Field, crossRefText As String, crossRefResult As String, fldCode As String
            Set myField = .Fields(1)
            crossRefText = &quot;Cross-reference &quot; &amp; myField.result &amp; &quot; is:&quot;
            crossRefResult = getFullTextFromFieldCode(myField)
            modCallUserforms.callDisplayMesageNotModal &quot;Result of cross-reference&quot;, crossRefText, crossRefResult
        Else
            MsgBox &quot;No cross-references found in selection&quot;
        End If
    End With
End Function

Private Function getFullTextFromFieldCode(inputField As Field) As String
    Dim origFieldCode As String, editedFieldCode As String
    
    origFieldCode = inputField.Code
    editedFieldCode = Replace$(inputField.Code, &quot;\r&quot;, &quot;&quot;)
    With inputField
        .Code = editedFieldCode
        .Update
        getFullTextFromFieldCode = .result
        .Code = origFieldCode
        .Update
    End With
End Function

huangapple
  • 本文由 发表于 2023年5月29日 20:59:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定