如何将VBA中的单词对象引用还原到较早的项目库?

时间:2019-05-31 17:42:35

标签: excel vba excel-vba ms-word

我写了一段代码,其中通过excel vba执行标签合并。我的客户端(2010)的Excel版本比我(2013)的版本早,并且由于具有Word对象引用v 14和me 15而无法执行该部分。

我认为问题在于早期约束与晚期约束?我一直试图在不检查VBA中15个引用的情况下使代码正常工作,但是无论我知道可能的哪种代码变化,当我尝试保存以前声明为对象的内容时,它仍然会调试。我很确定这也与此有关。在代码中,使用3个单独的标签模板来生成3个标签文档。调用和关闭模板没有问题,直接将模板声明为其工作簿路径。当试图保存已设置为对象的合并输出“ wd”时,就会出现问题。

                            Sub RunMerge()
                            ' Word constants

                            Dim wd As Object
                            Dim wdocSource As Object
                            Dim wks As Worksheet
                            Dim Worksheets
                            Dim strWorkbookName As String

                            On Error Resume Next
                            Set wd = GetObject(, "Word.Application")
                            If wd Is Nothing Then
                                Set wd = CreateObject("Word.Application")
                            End If
                            On Error GoTo 0

                            Set Worksheets = ActiveWorkbook.Sheets
                            Set wks = ActiveSheet
                                With ActiveWorkbook
                                    For Each wks In Worksheets



                            Set wdocSource = wd.Documents.Open(ThisWorkbook.Path & "\Label Templates\PRODUCT Label Template_" & wks.Name & ".docx")

                            strWorkbookName = ThisWorkbook.Path & "\Order Output\Order_Output_ " & Format(Date, "dd.mm.yyyy") & ".xlsx"

                            wdocSource.MailMerge.MainDocumentType = wdFormLabels

                            wdocSource.MailMerge.OpenDataSource _
                                    Name:=strWorkbookName, _
                                    AddToRecentFiles:=False, _
                                    Revert:=False, _
                                    Format:=wdOpenFormatAuto, _
                                    Connection:="Data Source=" & strWorkbookName, _
                                    SQLStatement:="SELECT * FROM [" & wks.Name & "$]"

                            With wdocSource.MailMerge
                                .Destination = wdSendToNewDocument
                                .SuppressBlankLines = True
                                With .DataSource
                                    .FirstRecord = wdDefaultFirstRecord
                                    .LastRecord = wdDefaultLastRecord
                                End With
                                .Execute Pause:=False
                            End With

                            wd.SaveAs2 (ThisWorkbook.Path & "\Label Output\Label Output_" & wks.Name & "_" & Format(Date, "dd.mm.yyyy") & ".docx")
                            wdocSource.Close SaveChanges:=False

                                    Next wks
                                End With

                            wd.Visible = True
                            Set wdocSource = Nothing
                            Set wd = Nothing
                            Set wks = Nothing
                            Set Worksheets = Nothing
                            End Sub

一旦它到达“ wd.SaveAs2”,就会抛出一个对象引用错误,如果我将其更改为activedocment,文档或单词。东西或将其调暗,设置,放在一端/作为对象,无用!!

1 个答案:

答案 0 :(得分:1)

您的代码基本上是为与后期绑定一起使用而设置的,但是您通过引用内置的Word常量违反了后期绑定原理-这就需要Word对象库引用。删除Word对象库引用,并通过后期绑定将代码修改为可以完全使用(例如,将内置Word常量替换为其数值)。