【问题标题】:VBA Word macro goes to breakmodeVBA Word 宏进入中断模式
【发布时间】:2023-04-06 12:59:01
【问题描述】:

我正在尝试使用 vba 从 excel 打开两个文档,并从这个特定的 excel 文件中调用一个 word 宏。

宏在 Word 中运行良好,我还可以打开文档并启动 word 宏。但是,当从一个文档切换到另一个文档时,单词宏会进入中断模式(当我从 Word 而不是 Excel 运行它时不会发生这种情况)。

我使用 excel 中的以下代码:

Set wordApp = CreateObject("Word.Application")
worddoc = "H:\Word Dummy's\Dummy.docm"
wordApp.Documents.Open worddoc
wordApp.Visible = True

wordApp.Run macroname:="update_dummy", varg1:=client, varg2:=m_ultimo, varg3:=y

在 word 中,我有一个 sub,其中包含在断点和以下代码之间定义的参数:

worddoc2 = "H:\Word Dummy's\texts.docx"

Word.Application.Activate
Documents.Open worddoc2, ReadOnly:=True
ThisDocument.Activate
Set bmks = ThisDocument.Bookmarks

谁能告诉我为什么它不能从 excel 运行以及我该如何解决这个问题?

提前致谢。

【问题讨论】:

  • 中断发生在哪里?从excel打开后用excel还是word?
  • 在 word 中打开 worddoc2 并转到 thisdocument.activate 后
  • 请展示您的完整代码,以便更轻松地帮助您...
  • 从您的代码看来,您不需要ThisDocument.Activate。如果您注释掉该行会发生什么?
  • 还是同样的错误。我还尝试添加 EnableEvents=true 和 documents(worddoc).activate 而不是 thisdocument.activate 但我不断收到错误消息:无法在中断模式下执行。认为这是因为 excelmacro 在启动 wordmacro 时进入了中断模式,但我不知道如何解决这个问题。

标签: vba excel ms-word


【解决方案1】:

在 Google 上搜索了很多之后,我终于自己找到了答案。
我需要补充:

application.EnableEvents=false

到 excel 宏。

仅此而已。现在可以了。

【讨论】:

    【解决方案2】:

    我的完整代码很大(excel 中的宏还打开另外两个工作簿并在其中运行宏)。这部分代码现在正在工作(所以我把它省略了),但我只想添加它打开 worddoc 并根据在 excel 用户表单中选择的客户端添加特定文本的部分。但是为了让您更好地了解我的代码的外观,这是在 excel 中(客户端由另一个模块中的用户窗体定义):

    Sub open_models (client as string)
    
    Application.DisplayStatusBar = True
    
    ‘determine datatypes
    Dim m_integer As Integer
    Dim m_ultimo As String
    Dim m_primo As String
    
    Dim y As String
    Dim y_integer As Integer
    Dim y_old As String
    Dim y_last As String
    
    Dim wordApp As Object
    Dim worddoc As String
    
    'Determine current month and year and previous
    m_integer = Format(Now, "mm")
    y_integer = Format(Now, "yyyy")
    
    If m_integer <= 9 Then
        m_ultimo = "0" & m_integer - 1
        m_primo = "0" & m_integer - 2
     Else
        m_ultimo = m_integer - 1
        m_primo = m_integer - 2
    End If
    
    If m_integer = 1 Then
        y = y_integer - 1
      Else
        y = y_integer
    End If
    
    On Error Resume Next
    
    'open word dummy
    Set wordApp = CreateObject("Word.Application")
    worddoc = "H:\RAPORTAG\" & y & "\" & y & m_ultimo & "\Dummy.docm"
    
    wordApp.Documents.Open worddoc
    wordApp.Visible = True
    
    wordApp.Run macroname:="update_dummy", varg1:=client, varg2:=m_ultimo, varg3:=y, varg4:= worddoc)
    
    On Error GoTo 0
    
    ThisWorkbook.Activate
    
    'reset statusbar and close this workbook
    Application.DisplayStatusBar = False
    Application.Calculation = xlCalculationAutomatic
    Application.DisplayAlerts = True
    
    ThisWorkbook.Close False
    
    End Sub
    

    这是我正在使用的word中的代码:

    Sub update_dummy(client As String, m_ultimo As String, y As String, worddoc as string)
    
    Dim wordapp As Object
    Dim rngStart As Range
    Dim rngEnd As Range
    Dim worddoc As String
    Dim worddoc2 As String
    Dim dekkingsgraad As String
    Dim bmks As Bookmarks
    Dim bmRange As Range
    Dim rng As Range
    Dim i As Boolean
    
    On Error Resume Next
    
    worddoc2 = "H:\RAPORTAG\" & y & "\" & y & m_ultimo & "\dummytexts.docx"
    
    'open other word
    Documents.Open worddoc2, ReadOnly:=True
    Documents(worddoc).Activate
    Set bmks = Documents(worddoc).Bookmarks
    
    'management summary
    If client <> "PMT" Then
    i = True
    Set rngStart = Documents(worddoc2).Bookmarks("bn0_1_start").Range
    Set rngEnd = Documents(worddoc2).Bookmarks("bn0_1_end").Range
    End If
    
    If i = True Then
        Set rng = Documents(worddoc2).Range(rngStart.Start, rngEnd.End)
        rng.Copy
    
        Set bmRange = Documents(worddoc).Bookmarks("bmManagementsummary").Range
        bmRange.PasteAndFormat (wdPasteDefault)
    End If
    
    i = False
    
    On Error GoTo 0
    
    End Sub
    

    我还定义了 20 个书签,但它们的代码都是一样的。

    【讨论】:

      【解决方案3】:

      这个问题我已经看到并解决过几次了,我发现的解决方案很奇怪。

      1. 将所有代码复制粘贴到文本中 编辑器,word 1,excel 1

      2. 删除 word 或 excel 或更好的所有宏,只需创建 新文件。

      3. 将所有代码从文本编辑器粘贴到 word/excel 中。

      我在 Excel 和 Access 中肯定遇到过 3 或 4 次。特别是如果您之前在该位置有一个断点。

      这听起来很愚蠢,但试试看是否有效,这让我好几次免于精神错乱。

      【讨论】:

        猜你喜欢
        • 2020-10-15
        • 1970-01-01
        • 2016-02-05
        • 2021-03-22
        • 1970-01-01
        • 2013-05-06
        • 2018-04-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多