简体   繁体   English

Excel VBA修改带书签的Word文档

[英]Excel VBA to modify word documents with bookmarks

I have a problem with my code below. 我的下面的代码有问题。 I created an userform in order to generate Word documents automatically which I prepared (I created a bunch of bookmarks). 我创建了一个用户窗体,以便自动生成我准备的Word文档(我创建了一堆书签)。

It works really well on my computer but not on another computer and I really don't understand why. 它在我的计算机上确实运行良好,但在另一台计算机上却无法运行,我真的不明白为什么。 Both computers have the same Office version (1902) and I have activated the Microsoft Word 16.0 Object Library reference. 两台计算机具有相同的Office版本(1902),并且我已经激活了Microsoft Word 16.0对象库参考。

What I mean by "it's not working" it is that the Word document will open but no action will be Performed... And also I have not a single error message. 我的意思是“它不起作用”是指Word文档将打开,但不会执行任何操作……而且我也没有一条错误消息。

    Private Sub BCO_Click()

        Dim objWord As New Word.Application, wordDoc As Word.Document

'FCO is the userform and the subobjects are combobox entries.
        If FCO.SOCIETENAME <> "" And FCO.NUMCO <> "" And FCO.ComboBox1 <> "" Then

            Dim pathcovierge As String
            Dim pathconew As String

'Path of the files needed there, copy from an existing (pathcovierge) to a new one (pathconex)
            pathcovierge = path & "\Documents_Vierges\" & "CO.docx"
            pathconew = path & "\CO\CO_" & UCase(FCO.SOCIETENAME.Value) & "_" & UCase(FCO.NUMCO.Value) & ".docx"

            If Dir(path & "\CO\", vbDirectory) = "" Then MkDir path & "\CO\"

'If file already open, msgbox
            On Error Resume Next

            FileCopy pathcovierge, pathconew

            If Err > 0 Then
                MsgBox "Veuillez fermer CO.docx afin de générer un CO."
            End If



'opening of the new word document

            objWord.Visible = True
            objWord.Documents.Open pathconew

            Dim DocDest As Word.Document
            Set DocDest = GetObject(pathconew)


'THIS IS NOT WORKING.

            DocDest.Bookmarks("WNUMCO").Range.Text = FCO.NUMCO.Value
            DocDest.Bookmarks("WDATECO").Range.Text = FCO.DATECO.Value
            DocDest.Bookmarks("WNOMCLIENT").Range.Text = FCO.SOCIETENAME.Value



'Saving (working)
            DocDest.SaveAs pathconew
            AppActivate ("CO_" & UCase(FCO.SOCIETENAME.Value) & "_" & UCase(FCO.NUMCO.Value) & ".docx")

            On Error GoTo 0
        Else
            MsgBox "Veuillez rentrer tous les champs obligatoires (*)"
        End If


    End Sub

I took a look in your code and made some changes (also see my comments in the code): 我查看了您的代码并进行了一些更改(另请参见代码中的注释):

  • I enhanced the readability by early exiting the procedure instead of using 'arrow code'. 我通过尽早退出该过程而不是使用“箭头代码”来增强了可读性。
  • Now the opened Word document will be set to the variable immediately. 现在,打开的Word文档将立即设置为变量。
  • Your error handling suppressed all errors. 您的错误处理抑制了所有错误。 I changed it, but you should add proper error handling though. 我更改了它,但是您应该添加适当的错误处理。 Think about splitting your procedure in several separate procedures. 考虑将您的过程分为几个单独的过程。

This should lead you to your result: 这应该导致您得到结果:

Private Sub BCO_Click()
    If FCO.SOCIETENAME = "" Or FCO.NUMCO = "" Or FCO.ComboBox1 = "" Then
        MsgBox "Veuillez rentrer tous les champs obligatoires (*)"
        Exit Sub
    End If

    Dim pathcovierge As String
    pathcovierge = path & "\Documents_Vierges\" & "CO.docx"

    Dim pathconew As String
    pathconew = path & "\CO\CO_" & UCase(FCO.SOCIETENAME.Value) & "_" & UCase(FCO.NUMCO.Value) & ".docx"

    If Dir(path & "\CO\", vbDirectory) = "" Then MkDir path & "\CO\"

    'This seems to be the reason why you get no error:
    On Error Resume Next

    FileCopy pathcovierge, pathconew

    If Err > 0 Then
        MsgBox "Veuillez fermer CO.docx afin de générer un CO."
    End If

    'This will let you see a possible error, but you should think about implement a proper error handling though:
    On Error Goto 0

    Dim objWord As Word.Application
    Set objWord = New Word.Application
    objWord.Visible = True

    Dim docDest As Word.Document
    'If the problem was to get the handle to the opened document, this should work better:
    Set docDest = objWord.Documents.Open(pathconew)

    docDest.Bookmarks("WNUMCO").Range.Text = FCO.NUMCO.Value
    docDest.Bookmarks("WDATECO").Range.Text = FCO.DATECO.Value
    docDest.Bookmarks("WNOMCLIENT").Range.Text = FCO.SOCIETENAME.Value

    docDest.SaveAs pathconew

    AppActivate ("CO_" & UCase(FCO.SOCIETENAME.Value) & "_" & UCase(FCO.NUMCO.Value) & ".docx")
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM