简体   繁体   English

VBA Word宏进入中断模式

[英]VBA Word macro goes to breakmode

I'm trying to open two documents from excel with vba and call a word macro from this particular excel file. 我正在尝试使用vba从excel打开两个文档,并从此特定excel文件调用word宏。

The macro is working fine in Word and I also get the documents to open and the word macro to start. 宏在Word中工作正常,我也可以打开文档并启动word宏。 However when there is a switch from one document to the other the word macro goes to break-mode (which does not happen when I run it from Word instead of Excel). 但是,当从一个文档切换到另一个文档时,word宏将进入中断模式(当我从Word而不是Excel运行它时,不会发生这种情况)。

I use the following code from 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

In word I have a sub with the parameters defined between breakets and the following code: 换句话说,我有一个带有在断点和以下代码之间定义的参数的子:

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

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

Can anyone tell me why it does not run from excel and how I can fix this? 谁能告诉我为什么它不能从excel中运行以及如何解决?

Thanks in advance. 提前致谢。

I finally found the answer myself after a lot of searching on Google. 经过在Google上的大量搜索,我终于找到了答案。
I needed to add : 我需要添加:

application.EnableEvents=false

To the excel macro. 到excel宏。

That was all. 就这些。 Now it works. 现在可以了。

My complete code is huge (the macro in excel also opens two other workbooks and runs a macro in them). 我完整的代码非常庞大(excel中的宏还打开了另外两个工作簿并在其中运行一个宏)。 This part of the code is working for now (so I left it out), but I just want to add the part that it opens a worddoc and adds specific texts in it depending on what client has been chosen in the excel userform. 这部分代码目前正在工作(因此我省略了),但是我只想添加部分,它可以打开worddoc并在其中添加特定文本,具体取决于在excel用户窗体中选择的客户端。 But to show you a better idea how my code looks like, this is in excel (where the client is defined by a userform in another module): 但是为了向您展示我的代码看起来更好的主意,这是在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

And this is the code in word I am using: 这是我正在使用的文字代码:

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

I have 20 more bookmarks that are defined but the code for them is all the same. 我还定义了20个书签,但是它们的代码都相同。

I have seen and solved this problem a few times before, the solution I found was odd. 我已经看过几次并且解决了这个问题,发现的解决方案很奇怪。

  1. Copy paste all your code into a text editor, 1 for word, 1 for excel 复制将您的所有代码粘贴到文本编辑器中,一个用于单词,一个用于Excel 1

  2. Delete all the macros in word or excel or better yet, just create new files. 删除word或excel或更好的所有宏,只需创建新文件即可。

  3. Paste all the code into word/excel from your text editor. 从您的文本编辑器将所有代码粘贴到word / excel中。

I've definitely had this 3 or 4 times in Excel and Access. 我肯定在Excel和Access中有3或4次了。 Especially if you previously had a breakpoint at that location. 特别是如果您以前在该位置有断点。

It sounds stupid but try it and see if that works, this has saved me from insanity a few times. 听起来很愚蠢,但请尝试一下是否可行,这使我免于几次疯狂。

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

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