MS Word填充文本之后通过VBA插入

发布于 2025-01-20 10:12:46 字数 2455 浏览 4 评论 0原文

我已经创建了启用Word宏的模板。 打开时会弹出表格,用户可以填写表格。按“确定”后,文档内的书签将被更新并显示。 我需要的是在多个位置的整个文档中填充输入的值。我尝试过交叉引用书签,但它们没有使用表单中输入的值进行更新。

打开表单的图片

Private Sub cancelBut_Click()
    stInfo.Hide
    
End Sub

Private Sub Label11_Click()

End Sub

Private Sub OKbut_Click()
    Dim katcest As Range
    Set katcest = ActiveDocument.Bookmarks("katcest").Range
    katcest.Text = Me.TextBox1.Value
    Dim katopcina As Range
    Set katopcina = ActiveDocument.Bookmarks("katopcina").Range
    katopcina.Text = Me.TextBox2.Value
    Dim zkcest As Range
    Set zkcest = ActiveDocument.Bookmarks("zkcest").Range
    zkcest.Text = Me.TextBox3.Value
    Dim zkopcina As Range
    Set zkopcina = ActiveDocument.Bookmarks("zkopcina").Range
    zkopcina.Text = Me.TextBox4.Value
    Dim zkulozak As Range
    Set zkulozak = ActiveDocument.Bookmarks("zkulozak").Range
    zkulozak.Text = Me.TextBox5.Value
    Dim povrsina As Range
    Set povrsina = ActiveDocument.Bookmarks("povrsina").Range
    povrsina.Text = Me.TextBox6.Value
    Dim vlasnik As Range
    Set vlasnik = ActiveDocument.Bookmarks("vlasnik").Range
    vlasnik.Text = Me.TextBox7.Value
    Dim vladresa As Range
    Set vladresa = ActiveDocument.Bookmarks("vladresa").Range
    vladresa.Text = Me.TextBox8.Value
    Dim datocevida As Range
    Set datocevida = ActiveDocument.Bookmarks("datocevida").Range
    datocevida.Text = Me.TextBox9.Value
    Dim klasa As Range
    Set klasa = ActiveDocument.Bookmarks("klasa").Range
    klasa.Text = Me.TextBox10.Value
    Dim urbroj As Range
    Set urbroj = ActiveDocument.Bookmarks("urbroj").Range
    urbroj.Text = Me.TextBox11.Value
    Me.Repaint
    Dim strDocName As String
    Dim intPos As Integer

    ' Find position of extension in file name
    strDocName = ""
    intPos = InStrRev(strDocName, ".")
 
    If intPos = 0 Then
 
        ' If the document has not yet been saved
        ' Ask the user to provide a file name
        strDocName = InputBox("Upisi naziv " & _
            "vaseg dokumenta.")
    Else
 
        ' Strip off extension and add ".txt" extension
        strDocName = Left(strDocName, intPos - 1)
        strDocName = strDocName & ".docx"
    End If
 
    ' Save file with new extension
    ActiveDocument.SaveAs2 FileName:=strDocName, _
        FileFormat:=wdFormatDocumentDefault
    stInfo.Hide
    infoForm.Show
    
End Sub

I have created word macro enabled template.
At opening form pops-up and user can fill in form. After pressing OK bookmarks inside document are updated and shown.
What I need is to populate entered values trough entire document on multiple locations. I have tried cross-referencing bookmarks but they are not updated with values entered in form.

image of opening form

Private Sub cancelBut_Click()
    stInfo.Hide
    
End Sub

Private Sub Label11_Click()

End Sub

Private Sub OKbut_Click()
    Dim katcest As Range
    Set katcest = ActiveDocument.Bookmarks("katcest").Range
    katcest.Text = Me.TextBox1.Value
    Dim katopcina As Range
    Set katopcina = ActiveDocument.Bookmarks("katopcina").Range
    katopcina.Text = Me.TextBox2.Value
    Dim zkcest As Range
    Set zkcest = ActiveDocument.Bookmarks("zkcest").Range
    zkcest.Text = Me.TextBox3.Value
    Dim zkopcina As Range
    Set zkopcina = ActiveDocument.Bookmarks("zkopcina").Range
    zkopcina.Text = Me.TextBox4.Value
    Dim zkulozak As Range
    Set zkulozak = ActiveDocument.Bookmarks("zkulozak").Range
    zkulozak.Text = Me.TextBox5.Value
    Dim povrsina As Range
    Set povrsina = ActiveDocument.Bookmarks("povrsina").Range
    povrsina.Text = Me.TextBox6.Value
    Dim vlasnik As Range
    Set vlasnik = ActiveDocument.Bookmarks("vlasnik").Range
    vlasnik.Text = Me.TextBox7.Value
    Dim vladresa As Range
    Set vladresa = ActiveDocument.Bookmarks("vladresa").Range
    vladresa.Text = Me.TextBox8.Value
    Dim datocevida As Range
    Set datocevida = ActiveDocument.Bookmarks("datocevida").Range
    datocevida.Text = Me.TextBox9.Value
    Dim klasa As Range
    Set klasa = ActiveDocument.Bookmarks("klasa").Range
    klasa.Text = Me.TextBox10.Value
    Dim urbroj As Range
    Set urbroj = ActiveDocument.Bookmarks("urbroj").Range
    urbroj.Text = Me.TextBox11.Value
    Me.Repaint
    Dim strDocName As String
    Dim intPos As Integer

    ' Find position of extension in file name
    strDocName = ""
    intPos = InStrRev(strDocName, ".")
 
    If intPos = 0 Then
 
        ' If the document has not yet been saved
        ' Ask the user to provide a file name
        strDocName = InputBox("Upisi naziv " & _
            "vaseg dokumenta.")
    Else
 
        ' Strip off extension and add ".txt" extension
        strDocName = Left(strDocName, intPos - 1)
        strDocName = strDocName & ".docx"
    End If
 
    ' Save file with new extension
    ActiveDocument.SaveAs2 FileName:=strDocName, _
        FileFormat:=wdFormatDocumentDefault
    stInfo.Hide
    infoForm.Show
    
End Sub

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文