问题描述
我有一个Word文档,其中包含大约4000个表单域,之后我必须将它们导出到数据库中.问题在于4000个字段中的书签"字段中都没有信息,因此我无法获取存储在其中的信息.
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.
我正在尝试创建一个宏来帮助编写书签(FormField.Name)的过程,但是无法正确完成.问题是我想更改用户选择中包含的FormFields的名称,并且仅更改它们.我设法解决了这个问题:
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
但是它不起作用,因为Selection对象在For Each循环中进行了更改,之后它仅包含所选内容的第一个FormField.
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.
我尝试过:
Dim mySelection as Selection
Set mySelection = Selection
但是,如果我更改选择,变量mySelection也会发生变化(这是很正常的……),而且我找不到任何克隆对象的方法.
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?
谢谢
推荐答案
为您的副本"使用其他引用:
Use a different reference for your "copy":
Dim selBkUp As Range
Set selBkUp = ActiveDocument.Range(Selection.Range.Start, Selection.Range.End)
或使用重复项:
Dim selBkUp As Range
selBkUp = Selection.Range.Duplicate
这篇关于Word宏,存储当前选择(VBA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!