VBA Word - 使用弹出窗口填写多个表单 [英] VBA Word - filling out multiple forms with pop-up window

查看:247
本文介绍了VBA Word - 使用弹出窗口填写多个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务:我在一个 Word 文档中有多个表格,并且必须填写相同的信息,例如公司名称、地址、税号等.

My task: I have multiple forms in one Word document and it has to be filled out with the same information such as company name, address, tax number, etc.

我使用 VBA 的经验非常有限,所以我使用书签并编写了一些代码:

My experience with VBA is very limited so I used bookmarks and wrote some code:

Private Sub OKbutton_Click()
Dim FirmaName As Range
Set FirmaName = ActiveDocument.Bookmarks("FirmaName").Range
FirmaName.Text = Me.TextBox1.Value
Dim FirmaNameRio As Range
Set FirmaNameRio = ActiveDocument.Bookmarks("FirmaNameRio").Range
FirmaNameRio.Text = Me.TextBox1.Value
Me.Repaint
stinfo.Hide

对我来说的问题或挑战是,我希望能够动态"更改弹出表单中的数据.现在每次我按 OK 按钮时它都会以这种方式工作,它会在前一个后面添加新数据,这是不可取的.如果我犯了错误,我只想在弹出窗口中更改它,而不是在文档中.

The issue or challenge for me is, that I want to be able to "dynamically" change the data in the pop-up form. Now it works the way every time I press OK-button, it adds new data behind the previous, which is undesirable. If I make a mistake I want to change it only in the pop-up window, not in the document.

那么有没有什么办法让弹窗中输入的信息可以改写之前的信息呢?

So is there any way to program it that the information typed in the pop-up window can rewrite the previous information?

没有必要使用书签或其他任何东西.它只需要工作,其他员工可以轻松填写​​这些表格,并通过跳过机械复制粘贴来节省一些时间.

It´s not necessary to use bookmarks or whatsoever. It just has to work, that it´s easy for other employees to fill out these forms and save some time by skipping mechanical copy-pasting.

推荐答案

使用如下代码:

Sub UpdateBookmark(StrBkMk As String, StrTxt As String)
Dim BkMkRng As Range
With ActiveDocument
  If .Bookmarks.Exists(StrBkMk) Then
    Set BkMkRng = .Bookmarks(StrBkMk).Range
    BkMkRng.Text = StrTxt
    .Bookmarks.Add StrBkMk, BkMkRng
  End If
End With
Set BkMkRng = Nothing
End Sub

您可以使用如下代码调用:

which you would call with code like:

Call UpdateBookmark "FirmaName" Me.TextBox1.Value

而且,由于您正在使用书签,而不是多次将相同的值写入不同的书签,只需插入对第一个要重复其值的书签的交叉引用,并且当您更新所有书签时, 使用单个代码行更新所有交叉引用:

And, since you're using bookmarks, instead of writing the same value multiple times to different bookmarks, simply insert a cross-reference to the first bookmark whose value is to be repeated and, when you've updated all the bookmarks, use a single code line to update all the cross-references:

ActiveDocument.Fields.Update

这篇关于VBA Word - 使用弹出窗口填写多个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆