Excel文件不可见但Word文件是

时间:2017-11-17 05:23:59

标签: vba excel-vba excel

Form Workbook1我正在使用以下代码启动Opening_File Userform以保持应用程序隐藏并显示表单:

Sub ShowingForm()
    Opening_File.Show (vbModeless)
    Application.Visible = False
    ThisWorkbook.Windows(1).Visible = False
End Sub

我使用以下代码从表单中打开Excel和Word文件:

Sub OpeningExcelFile()
    Dim Finfo As String
    Dim FilterIndex As Integer
    Dim Title As String
    Dim Filename As Variant
    Dim wb As Workbook
    Dim objWdApp As Object
    Dim objWdDoc As Object



    'Setup the list of file filters
    Finfo = "Excel Files (*.xlsx),*xlsx," & _
            "Macro-Enable Worksheet (*.xlsm),*xlsm," & _
            "Word Files (*.docx),*.docx," & _
            "All Files (*.*),*.*"
             MultiSelect = True


    'Display *.* by default
    FilterIndex = 4

    'Set the dialog box caption
    Title = "Select a File to Open"

    'Get the Filename
    Filename = Application.GetOpenFilename(Finfo, _
        FilterIndex, Title)

    'Handle return info from dialog box

    If Filename = False Then
            MsgBox "No file was selected."
            Exit Sub 

    Else
        MsgBox "You selected " & Filename
    End If


    If InStr(1, Filename, ".docx", vbTextCompare) > 0 Then
        Set objWdApp = CreateObject("Word.Application")
        objWdApp.Visible = True
        Set objWdDoc = objWdApp.Documents.Open(Filename) '\\ Open Word Document

    Else
        Set wb = Workbooks.Open(Filename) '\\ Open Excel Spreadsheet



    End If


End Sub

打开Word文件不是问题,但每当我打开Application.Visible = False以来的Excel文件时,我都必须隐藏表单:

Private Sub CommandButton2_Click()
     Opening_File.Hide
     Application.Visible = True
     ThisWorkbook.Windows(1).Visible = True
End Sub

所以我能够看到打开excel文件。反正是否保持表单与工作簿可见,我正在启动表单不可见,但我从表单中打开的每个工作簿都可见?谢谢

1 个答案:

答案 0 :(得分:0)

我意识到在这种情况下最好的方法是让ThisWorkbook.Windows(1).Visible = False女巫离开应用程序,并允许我查看从表单中打开的每个工作簿文件。

Sub ShowingForm()
    Opening_File.Show (vbModeless)
    Application.Visible = True
    ThisWorkbook.Windows(1).Visible = False
End Sub