VBA Word宏进入中断模式
我试图用vba从excel打开两个文档,并从这个特定的excel文件调用word宏 宏在Word中运行良好,我还打开了文档并启动了Word宏。但是,当从一个文档切换到另一个文档时,word宏将进入中断模式(当我从word而不是Excel运行它时,不会发生这种情况) 我使用excel中的以下代码:VBA Word宏进入中断模式,vba,excel,ms-word,Vba,Excel,Ms Word,我试图用vba从excel打开两个文档,并从这个特定的excel文件调用word宏 宏在Word中运行良好,我还打开了文档并启动了Word宏。但是,当从一个文档切换到另一个文档时,word宏将进入中断模式(当我从word而不是Excel运行它时,不会发生这种情况) 我使用excel中的以下代码: Set wordApp = CreateObject("Word.Application") worddoc = "H:\Word Dummy's\Dummy.docm" wordApp.Documen
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,其参数在Breaket和以下代码之间定义:
worddoc2 = "H:\Word Dummy's\texts.docx"
Word.Application.Activate
Documents.Open worddoc2, ReadOnly:=True
ThisDocument.Activate
Set bmks = ThisDocument.Bookmarks
有人能告诉我为什么它不能从excel运行,以及我如何解决这个问题吗
提前感谢。我的完整代码非常庞大(excel中的宏还会打开另外两个工作簿并在其中运行宏)。代码的这一部分目前正在运行(所以我省略了它),但我只想添加它打开worddoc并在其中添加特定文本的部分,这取决于在excel用户表单中选择的客户端。但为了让您更好地了解我的代码,这是在excel中(在excel中,客户端由另一个模块中的用户表单定义):
Sub-open_模型(客户端为字符串)
Application.DisplayStatusBar=True
'确定数据类型
Dim m_整数作为整数
作为字符串的Dim m_ultimo
将m_primo变为字符串
朦胧如弦
Dim y_整数作为整数
朦胧如弦
模糊的y_最后作为字符串
Dim wordApp作为对象
Dim worddoc作为字符串
'确定当前月份、年份和上一个
m_整数=格式(现在为“mm”)
y_integer=格式(现在为“yyy”)
如果m_integer我以前见过并解决过这个问题好几次,那么我找到的解决方案是奇怪的
将所有代码复制粘贴到文本中
编辑器,1个用于word,1个用于excel
删除word或excel中的所有宏,或者更好,只需创建
新文件
从文本编辑器将所有代码粘贴到word/excel中
我在Excel和Access中肯定有过三四次这样的经历。特别是如果您以前在该位置有断点
这听起来很愚蠢,但试试看是否有效,这让我好几次免于精神错乱。在谷歌上搜索了很多次后,我终于自己找到了答案。
我需要补充:
application.EnableEvents=false
到excel宏
就这些。现在它可以工作了。休息时间在哪里?从excel打开后在excel或word中?在word中打开worddoc2并转到此文档。激活请显示您的完整代码,以便更容易地帮助您…从您的代码中,您似乎不需要此文档。激活
。如果你把那行注释掉会发生什么?还是同样的错误。我还尝试添加EnableEvents=true和documents(worddoc).activate而不是thisdocument.activate,但我一直收到错误:无法在中断模式下执行。我想这是因为启动wordmacro时excelmacro会进入中断模式,但我不知道如何解决这个问题。
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
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