[英]VBA Word macro goes to breakmode
我正在嘗試使用vba從excel打開兩個文檔,並從此特定excel文件調用word宏。
宏在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
換句話說,我有一個帶有在斷點和以下代碼之間定義的參數的子:
worddoc2 = "H:\Word Dummy's\texts.docx"
Word.Application.Activate
Documents.Open worddoc2, ReadOnly:=True
ThisDocument.Activate
Set bmks = ThisDocument.Bookmarks
誰能告訴我為什么它不能從excel中運行以及如何解決?
提前致謝。
經過在Google上的大量搜索,我終於找到了答案。
我需要添加:
application.EnableEvents=false
到excel宏。
就這些。 現在可以了。
我完整的代碼非常龐大(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
這是我正在使用的文字代碼:
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個書簽,但是它們的代碼都相同。
我已經看過幾次並且解決了這個問題,發現的解決方案很奇怪。
復制將您的所有代碼粘貼到文本編輯器中,一個用於單詞,一個用於Excel 1
刪除word或excel或更好的所有宏,只需創建新文件即可。
從您的文本編輯器將所有代碼粘貼到word / excel中。
我肯定在Excel和Access中有3或4次了。 特別是如果您以前在該位置有斷點。
聽起來很愚蠢,但請嘗試一下是否可行,這使我免於幾次瘋狂。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.