简体   繁体   English

从 word 打开时绕过 workbook_open

[英]Bypass workbook_open when opening from word

I am working on a project that requires both an excel document and a word document.我正在做一个需要excel文档和word文档的项目。 I have fully programmed the excel document to work using UserForms, one of which opens automatically when opening the document.我已经对 Excel 文档进行了完整的编程以使用 UserForms 工作,其中之一在打开文档时会自动打开。 (see code below) (见下面的代码)

Private Sub Workbook_Open()
    frmOpen.Show
End Sub

The word document has been programmed to read data from this excel document and write it into a report. word文档已被编程为从此excel文档中读取数据并将其写入报告。 (see code below) (见下面的代码)

Private Sub cmdAutomatic_Click()
    Dim objExcel As New Excel.Application
    Dim exWb As Excel.Workbook
    Dim selectID As String


    Set exWb =objExcel.Workbooks.Open("C:\ Path")
    exWb.Close

    ''Data is written into the document here

    Set exWb = Nothing
    Unload Me
End Sub

The issue is that every time this button is pressed it opens the user form from the excel document and halts the other code until the user form is closed.问题是每次按下此按钮时,它都会从 excel 文档中打开用户表单并暂停其他代码,直到用户表单关闭。 Is there a way to only have the User Form open when it.有没有办法只打开用户表单。

I have tried我试过了

Application.EnableEvents = False

However this just returns a method or data member not found (so I assume that this has to be run excel to excel?)然而,这只是返回一个未找到的方法或数据成员(所以我认为这必须运行 excel 到 excel?)

Sorry if this question has already been answered, I could not find anything that addressed this issue.对不起,如果这个问题已经得到回答,我找不到任何解决这个问题的东西。 Also sorry if this has a really simple solution, this is for a school project and this is my first time using VBA也很抱歉,如果这有一个非常简单的解决方案,这是一个学校项目,这是我第一次使用 VBA

Edit: I realized that doing the following might work编辑:我意识到执行以下操作可能会奏效

exWb.Application.EnableEvents = False

However because I need to put this before the "Set" for it to stop the form from opening, it doesnt work (as the object is not set at the point the line is run).但是,因为我需要将它放在“设置”之前以阻止表单打开,所以它不起作用(因为对象没有设置在线运行点)。

You can disable Excel events with objExcel.EnableEvents = False before opening the workbook and reactivate them afterwards with objExcel.EnableEvents = True .您可以在打开工作簿之前使用objExcel.EnableEvents = False禁用 Excel 事件,然后使用objExcel.EnableEvents = True重新激活它们。

And as @Siddarth Rout told in comments, you can show your UserForm in Modeless mode with frmOpen.Show vbmodeless to avoid blocking other code execution.正如@Siddarth Rout 在评论中所说,您可以使用frmOpen.Show vbmodeless在无模式模式下显示您的用户窗体,以避免阻塞其他代码执行。 See MSDN remarks about this : https://msdn.microsoft.com/en-us/library/office/gg251819.aspx请参阅 MSDN 对此的评论: https : //msdn.microsoft.com/en-us/library/office/gg251819.aspx

So your code will look like this :所以你的代码看起来像这样:

Private Sub cmdAutomatic_Click()
    Dim objExcel As New Excel.Application
    Dim exWb As Excel.Workbook
    Dim selectID As String

    objExcel.EnableEvents = False
    Set exWb =objExcel.Workbooks.Open("C:\ Path")

exWb.Close
    objExcel.EnableEvents = True
    ''Data is written into the document here

    Set exWb = Nothing
    Unload Me
End Sub

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

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