Consulting

Results 1 to 7 of 7

Thread: Getopenfilename - File dialog hidden

  1. #1
    VBAX Newbie
    Joined
    Feb 2022
    Posts
    3
    Location

    Getopenfilename - File dialog hidden

    I have a code to import data to Word from Excel. The user has an open document in Word, and wants to import data from the selected Excel workbook. This is part of the code in Word:

        Dim xlApp As Excel.Application
        Dim xlWbk As Excel.Workbook
        Dim xlWs As Excel.Worksheet
        Dim strFile As String
            
        
        strFile = Excel.Application.GetOpenFilename(FileFilter:="Excel Workbooks Only (*.xlsx),*.xlsx", FilterIndex:=1, Title:="Select Excel Workbook")
        Workbooks.Open (strFile)
        
        Set xlApp = Excel.Application
        xlApp.Visible = False
        Set xlWbk = Excel.ActiveWorkbook
        
        Set xlWs = xlWbk.Sheets(1)
        
        xlWs.UsedRange.Copy
            
        Selection.Paste
        xlApp.Application.DisplayAlerts = wdAlertsNone
    When the user activates the macro, the file dialog is hidden behind the Word window, and has to use Alt-tab to navigate to it. Any ways to avoid this?

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    I usually do something like that this way

    Option Explicit
    
    
    Sub test()
    
    
        Dim xlApp As Object
        Dim xlWbk As Object
        Dim xlWs As Object
        Dim strFile As String
            
            
        Set xlApp = CreateObject("Excel.Application")
        
        strFile = xlApp.GetOpenFilename(FileFilter:="Excel Workbooks Only (*.xlsx),*.xlsx", FilterIndex:=1, Title:="Select Excel Workbook")
        xlApp.Workbooks.Open (strFile)
        Set xlWbk = xlApp.activeworkbook
        xlApp.Visible = False
        
        Set xlWs = xlWbk.Sheets(1)
        
        xlWs.UsedRange.Copy
            
        Selection.Paste
    
    
        xlApp.Application.DisplayAlerts = wdAlertsNone
        xlApp.Quit
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Newbie
    Joined
    Feb 2022
    Posts
    3
    Location
    Quote Originally Posted by Paul_Hossler View Post
    I usually do something like that this way
    Thanks, but it didn't help. The dialogue is still hidden behind the main window.

    Maybe I should had included this: in this situation, the macro is called from a template, not the current document. If the template is open (or you are working on a document with the macro) and the VBA window is open, it works. If you start from a new file and just call the macro from a template, it's hidden.

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Avoid redundant object variables:

    Sub M_snb()
      With Application.FileDialog(3)
        .InitialFileName = "G:\OF\*.xlsx"
        If .Show Then
          With GetObject(.SelectedItems(1))
            .Application.DisplayAlerts = False
            .sheets(1).usedrange.Copy
            ActiveDocument.Content.Paste
            .Close 0
          End With
        End If
      End With
    End Sub

  5. #5
    VBAX Newbie
    Joined
    Feb 2022
    Posts
    3
    Location
    Thanks, this works, but only in the document with the macro. It doesn't work when the macro is called from a template.

    Correction: it worked the first time, but now it doesn't work at all. "Run-time error. Call was rejected by callee".

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    What is 'this' ?

    The Code I posted is independent of where it has been stored.
    Last edited by snb; 02-08-2022 at 05:15 AM.

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Quote Originally Posted by fenilsen View Post
    Maybe I should had included this: in this situation, the macro is called from a template, not the current document.
    Yes, that would make a difference

    Try this in the DOTM and see if it works

    I saved the DOTM and then FIle, New to create a DOCX

    There's no error checking or handling canceling and the code could be cleaned up if you want


    Option Explicit
    
    
    Sub test()
    
    
        Dim xlApp As Object
        Dim xlWbk As Object
        Dim xlWs As Object
        Dim strFile As String
            
        ActiveWindow.Activate
            
        Set xlApp = CreateObject("Excel.Application")
        
        strFile = xlApp.GetOpenFilename(FileFilter:="Excel Workbooks Only (*.xlsx),*.xlsx", FilterIndex:=1, Title:="Select Excel Workbook")
        xlApp.Workbooks.Open (strFile)
        Set xlWbk = xlApp.activeworkbook
        xlApp.Visible = False
        
        Set xlWs = xlWbk.Sheets(1)
        
        xlWs.UsedRange.Copy
            
        Selection.Paste
        
        xlApp.Application.DisplayAlerts = wdAlertsNone
        xlApp.Quit
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •