Word宏,存储当前选择(VBA)

发布于 2024-07-24 21:20:47 字数 1090 浏览 3 评论 0原文

我有一个 Word 文档,其中包含大约 4000 个表单字段,之后我必须将其导出到数据库。 问题是,4000 个字段中没有一个在“书签”字段中包含信息,因此我无法获取其中存储的信息。

我正在尝试创建一个宏来帮助编写书签(FormField.Name)的过程,但无法正确执行。 问题是我想更改用户选择中包含的 FormField 的名称,并且仅更改它们。 我设法找到了这个解决方案:

Sub Macro2()
    Dim myFile As String
    Dim fnum As Integer
    Dim sFileText As String
    Dim currentField As FormField

    myFile = "c:\testMacro.txt"
    fnum = FreeFile()
    Open myFile For Input As fnum

    For Each currentField In Selection.FormFields
        Input #fnum, sFileText

        With currentField
            .StatusText = sFileText
            .OwnStatus = True
        End With

        currentField.Select
        Application.WordBasic.FormFieldOptions Name:=sFileText
    Next currentField
End Sub

但它不起作用,因为 Selection 对象在 For Each 循环中发生了更改,之后它只包含选择的第一个 FormField 。

所以这是我的问题,有没有办法保存当前选择并在更改后将其加载回来。

我已经尝试过:

Dim mySelection as Selection
Set mySelection = Selection

但是如果我更改选择,变量 mySelection 也会更改(这很正常......)并且我没有找到任何方法来克隆该对象。

有人知道如何做到这一点吗?

谢谢

I have a Word document that contains about 4000 form fields which I have to export afterwards to a database. The matter is that none of the 4000 fields have an information in the "Bookmark" field, thus I cannot get the information stored in them.

I'm trying to create a macro to help the process of writing the bookmark (FormField.Name) but cannot manage to do it right. The matter is that I want to change the names of the FormFields contained in the user's selection, and only them. I've managed to come to this solution:

Sub Macro2()
    Dim myFile As String
    Dim fnum As Integer
    Dim sFileText As String
    Dim currentField As FormField

    myFile = "c:\testMacro.txt"
    fnum = FreeFile()
    Open myFile For Input As fnum

    For Each currentField In Selection.FormFields
        Input #fnum, sFileText

        With currentField
            .StatusText = sFileText
            .OwnStatus = True
        End With

        currentField.Select
        Application.WordBasic.FormFieldOptions Name:=sFileText
    Next currentField
End Sub

But it doesn't work, because the Selection object is changed in the For Each loop and afterwards it only contains the first FormField of the selection.

So here is my question, is there a way to save the current selection and load it back after having changed it.

I've tried :

Dim mySelection as Selection
Set mySelection = Selection

But if I change the Selection, the variable mySelection changes as well (which is quite normal...) and I didn't find any way to clone the object.

Has someone an idea on how to do this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晚雾 2024-07-31 21:20:47

对您的“副本”使用不同的参考:

Dim selBkUp As Range
Set selBkUp = ActiveDocument.Range(Selection.Range.Start, Selection.Range.End)

或使用副本:

Dim selBkUp As Range
selBkUp = Selection.Range.Duplicate

Use a different reference for your "copy":

Dim selBkUp As Range
Set selBkUp = ActiveDocument.Range(Selection.Range.Start, Selection.Range.End)

Or use a duplicate:

Dim selBkUp As Range
selBkUp = Selection.Range.Duplicate
话少情深 2024-07-31 21:20:47

Selection.Range 自动是重复的,所以你可以这样做:

Dim selBkUp As Range
Set selBkUp = Selection.Range

Selection.Range is automatically a duplicate, so you can just do:

Dim selBkUp As Range
Set selBkUp = Selection.Range
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文