【问题标题】:compile error: end sub expected on ActiveDocument.Close after .VBComponents("thisDocument").CodeModule.AddFromFile编译错误:在 .VBComponents("thisDocument").CodeModule.AddFromFile 之后 ActiveDocument.Close 预期结束子
【发布时间】:2021-07-03 01:06:10
【问题描述】:

即使查看了所有类似的短语问题和几个搜索引擎结果,我也没有找到任何答案。

我复制当前的 word 文档并通过删除以前的模块来更改代码库,并通过从文件中添加来重写 ThisDocument 组件。对于上下文,但很可能是可跳过的:

Public Sub DOCMPublish()
   '...msoFileDialogSaveAs...and then...'

    Application.Documents.Add ThisDocument.FullName
    On Error Resume Next
    
    ' unlink fields and finalize content to avoid updates within the archived documents
    Dim oFld As field
    For Each oFld In ActiveDocument.Fields
        oFld.Unlink
    Next
        
    ' rewrite macros and unload modules
    On Error Resume Next
    Dim Element As Object
    For Each Element In ActiveDocument.VBProject.VBComponents
        ActiveDocument.VBProject.VBComponents.Remove Element
    Next
    rewriteMain ActiveDocument, "ThisDocument", ThisDocument.path & "\Document_Public_DOCM.vba"
    ' protect content
    ActiveDocument.Protect wdAllowOnlyFormFields, Password:="LoremIpsum"
    
    ' msoFileDialogSaveAs does not support filetypes, hence forcing extension
    DOCMFile = fileSaveName.SelectedItems(1)
    DOCMFile = Replace(DOCMFile, ".doc", ".docm")
    DOCMFile = Replace(DOCMFile, ".docmx", ".docm")
    
    ' the next line saves the copy to your location and name
    ActiveDocument.SaveAs2 filename:=DOCMFile, FileFormat:=wdFormatXMLDocumentMacroEnabled
    ' next line closes the copy leaving you with the original document
    ActiveDocument.Close
End Sub

这个子在过去几年中正常工作:

Sub rewriteMain(ByRef Workument, ByVal Module, ByVal Source)
    'delete code from ThisDocument/ThisWorkbook
    Workument.VBProject.VBComponents.Item(1).CodeModule.DeleteLines 1, Workument.VBProject.VBComponents.Item(1).CodeModule.CountOfLines
    'rewrite from file
    With Workument.VBProject
            .VBComponents(Module).CodeModule.AddFromFile Source
    End With
    'delete module
    Workument.VBProject.VBComponents.Remove Workument.VBProject.VBComponents("Rewrite")
End Sub

要导入的Document_Public_DOCM.vba的内容是

Option Explicit

Private Sub Document_Close()
    ThisDocument.Saved = True
End Sub

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
    Dim cc As ContentControl
    For Each cc In ThisDocument.ContentControls
        'checkboxes have no type attribute to check against, therefore the need of _
        error handling on checked-property that is checkbox-only in this usecase
        On Error Resume Next
        ThisDocument.Bookmarks("text" & cc.Tag).Range.Font.Hidden = Not cc.Checked
        ThisDocument.Bookmarks("notext" & cc.Tag).Range.Font.Hidden = cc.Checked
    Next
End Sub

我看这里没有问题,修改保存后的文件也不会报错。但与此同时,在导入和 ActiveDocument.SaveAs2 后关闭 ActiveDocument 时出现编译错误。虽然不关闭文件我没有收到任何错误,但这对工作环境不利,会弄乱屏幕。

单词经常崩溃,有时它只会导致状态丢失。我还尝试编码为 utf-8 和 iso 8859-1,禁用屏幕更新,但这似乎也不是解决方案。我错过了什么?

编辑: 我进一步尝试但没有成功:

  • 在编辑器中禁用语法检查
  • 出错时继续下一步
  • Err.Clear
  • newDoc.EnableEvents = False(在实施@Алексей-Р 建议后)
  • 不包括删除 .VBProject.VBComponents 名称“ThisDocument”

另外,显式编译修改后的文件代码预计不会引发任何错误。是否有任何我不知道的编辑器设置?

【问题讨论】:

  • 我在 Windows 10 上的 MS Word 2019 上尝试了您的代码,但无法重现您所写的问题。我看到的一个潜在问题是ActiveDocument 的使用。由于您没有发布所有代码,因此某些操作可能会更改ActiveDocument 并在错误的书中做一些事情。尝试将书绑定到变量Dim newDOC As Document: Set newDOC = Documents.Add(ThisDocument.FullName),然后使用newDOC. 而不是ActiveDocument.
  • 谢谢!不幸的是,这并没有解决问题。我在win10上的office 2016上运行它。由于这显然看起来像是很好的做法,我还是实施了它。尝试了编辑中所述的其他一些事情...
  • 用ofgice 2019对其进行了测试,结果相同。使用的库是应用程序的visual basic、ms word 16.0 对象库、ole 自动化、ms office 16.0 office 库和ms forms 2.0 对象库。您是否启用了其他库?
  • 这里是所有参考文献:(VBA)Visual Basic For Applications[Type Library], (Word)Microsoft Word 16.0 Object Library[Type Library], (stdole)OLE Automation[Type Library], (Normal)[Project], (Office)Microsoft Office 16.0 Object Library[Type Library].

标签: vba ms-word


【解决方案1】:

我尝试自己回答,至少在这种情况下这解决了问题:

我打开文件

Set newDOC = Documents.Add(ThisDocument.FullName, True, wdNewBlankDocument, False)

我只能假设在新的空白文档中打开文件而不显示它可能会阻止代码执行,因此会在运行时替换问题。

编辑: 一开始它有效,然后它没有。还是不知道为什么。以下现在似乎是万无一失的:

Set newDOC = Documents.Add("", True, wdNewBlankDocument, False)

ThisDocument.Content.Copy
dim rng
Set rng = newDoc.Content
rng.Collapse Direction:=wdCollapseEnd
rng.Paste
'clear clipboard, otherwise an annoying msg popy up everytime because huge content is left there from copying
Dim clscb As New DataObject 'object to use the clipboard
clscb.SetText text:=Empty
clscb.PutInClipboard 'put void into clipboard

此解决方案首先会打开一个新的空白文档并复制内容,而无需使用宏。之后我继续重写模块,就像问题中最初的 sn-p 一样

不知道为什么它对我提供的代码适用于@АлексейР。还是谢谢你的关心!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    相关资源
    最近更新 更多