在 Word VBA 中打开 Excel 文件后如何使 Microsoft Word 可见 [英] How to make Microsoft Word visible after opening an Excel file in Word VBA

查看:66
本文介绍了在 Word VBA 中打开 Excel 文件后如何使 Microsoft Word 可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 VBA Word 宏,它打开一个 Word 文档,然后打开一个 Excel 文件,选择一个单元格引用,最后使用 Msgbox 显示一条消息.打开 Excel 文件后,我无法找到使 Word 可见的代码,以便用户可以查看 Msgbox 消息,而无需使用任务栏从 Excel 切换到 Word.我试过 oWord.Visible = True 但 VBA 给了我一个错误.任何提示表示赞赏.

I have a VBA Word macro that opens a Word doc, then opens an Excel file, selects a cell reference and finally displays a message using Msgbox. After the Excel file has been opened I am having trouble finding the code to make Word visible so that the user can view the Msgbox message without having to using the task bar to switch from Excel to Word. I tried oWord.Visible = True but VBA gives me an error. Any hints are appreciated.

查看下面的代码:

Sub Module_Test()
Dim oExcel As Object
Dim oWord_Doc as object
Dim wb_open as workbook
Set oExcel = New Excel.Application
str_Excel_Filename = "C:\Test\Excel_Template.xlsx"
Documents.Open ("C:\Test\Doc_to_process.docx")
Set oWord_Doc = activedocument
oExcel.Visible = True
oExcel.ScreenUpdating = True
oExcel.Workbooks.Open str_Excel_Filename
Set wb_open = activeworkbook
wb_open.ActiveSheet.range("a6").Select
' At this point Excel is visible.  But the Msgbox statement below is not visible except when one switches to Word using the task bar.  What statement do I put here to make Word visible?
Msgbox "Here is a message that should be visible when viewing the window containing the Doc_to_process.docx"
End Sub

推荐答案

Visible 处于应用程序级别.您的 oExcel 变量为您提供了线索.您没有名为 oWord 的变量.

Visible is at the Application level. Your oExcel variable gives you the clue. You don't have a variable called oWord.

编辑添加以下代码

Option Explicit

Sub Module_Test()

Const MY_WB_PATH                As String = "C:\Test\Excel_Template.xlsx"
Const MY_DOC_PATH               As String = "C:\Test\Doc_to_process.docx"

Dim my_xl_app                   As Excel.Application
Dim my_doc                      As Word.Document
Dim my_wb                       As Excel.Workbook

    Set my_xl_app = New Excel.Application

    With my_xl_app
        .Visible = True
        .ScreenUpdating = True
        Set my_wb = .Workbooks.Open(MY_WB_PATH)

    End With

    my_wb.Activate
    my_wb.activeworksheet.Range("a6").Select

    ' At this point Excel is visible.  But the Msgbox statement
    ' below is not visible except when one switches to Word using
    ' the task bar.  What statement do I put here to make Word visible?
    Set my_doc = Documents.Open(MY_DOC_PATH)
    my_doc.Activate
    ' If required
    my_doc.Application.Visible = True

    MsgBox "Here is a message that should be visible when viewing the window containing the Doc_to_process.docx"
End Sub

如果您不熟悉 VBA,则应始终使用以下内容.

If you are new to VBA then the following should always be used.

  1. 在 VBA IDE 中确保每个模块都以选项显式"开头

  1. In the VBA IDE ensure each module starts with 'Option explicit'

在 VBA IDE 中,确保勾选 Tools.Option.Code Settings 中的所有复选框

In the VBA IDE ensure that all checkboxes in Tools.Option.Code Settings are ticked

这篇关于在 Word VBA 中打开 Excel 文件后如何使 Microsoft Word 可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆