Solved

Tweaking the 'Open' Dialog Box using VBA

Posted on 2013-12-09
6
3,029 Views
Last Modified: 2013-12-10
Dear Experts:

Below code snippet activates the open dialog box prompting the user to select an excel file.

I would like to tweak this snippet as follows:

(1) the filter should include xls, xlsx as well as xlsm - files
(2) The inital open dialog is to point to the following path as a constant: 'C:\MyFile\Data'

Help is much appreciated.

Thank you very much in advance.

Regards, Andreas

Sub Import_worksheet()
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook

' active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook

' get the customer workbook
filter = "Excel files (*.xlsx),*.xlsx"
'The open dialog is to point to the following path as a constant: C:\MyFile\Data
caption = "Please Select an input file "
customerFilename = Application.GetOpenFilename(filter, , caption)

If customerFilename = False Then
    MsgBox "No File specified.", vbExclamation, "ERROR"
    Exit Sub
    
Else
'Filter should be only xls and xlsx files
' If Cancel is pressed then abort the whole macro

Set customerWorkbook = Application.Workbooks.Open(customerFilename)
.......

Open in new window

0
Comment
Question by:AndreasHermle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 51

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 150 total points
ID: 39706173
Hi,

pls try

Declare Function SetCurrentDirectory Lib "kernel32" Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Long


Sub Import_worksheet()
Dim customerBook As Workbook
Dim filter As String
Dim caption As String
Dim customerFilename As String
Dim customerWorkbook As Workbook
Dim targetWorkbook As Workbook

' active workbook is the target
Set targetWorkbook = Application.ActiveWorkbook

' get the customer workbook
SetCurrentDirectory "C:\MyFile\Data"
filter = "Excel files 97-2003(*.xls),*.xls,Excel files (*.xlsx), *.xlsx,Excel files with Macro (*.xlsm), *.xlsm"
'The open dialog is to point to the following path as a constant: C:\MyFile\Data
caption = "Please Select an input file "
customerFilename = Application.GetOpenFilename(filter, , caption)

Open in new window

regards
0
 
LVL 35

Accepted Solution

by:
[ fanpages ] earned 200 total points
ID: 39706176
...or without the need for the "SetCurrentDirectory" routine:

filter = "Excel files (*.xls; *.xlsm; *.xlsx),*.xls;*.xlsm;*.xlsx"

ChDrive "C:"

ChDir "C:\MyFile\Data"

caption = "Please Select an input file "

customerFilename = Application.GetOpenFilename(filter, , caption)

Open in new window



Alternatively, using a different approach:

  Application.FileDialog(msoFileDialogOpen).ButtonName = "&Open"
  Application.FileDialog(msoFileDialogOpen).initialFilename = "C:\MyFile\Data"
  Application.FileDialog(msoFileDialogOpen).Filters.Clear
  Application.FileDialog(msoFileDialogOpen).Filters.Add "Excel files (*.xls; *.xlsm; *.xlsx)", "*.xls;*.xlsm;*.xlsx", 1
  Application.FileDialog(msoFileDialogOpen).Title = "Please Select an input file"
  Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
  
  If Not (Application.FileDialog(msoFileDialogOpen).Show) Then
     MsgBox "No File specified.", vbExclamation, "ERROR"
     Exit Sub
  Else
     customerFilename = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
'    ...
  End If

Open in new window


BFN,

fp.
0
 
LVL 15

Assisted Solution

by:unknown_routine
unknown_routine earned 150 total points
ID: 39706200
Dim fileDialog As Office.fileDialog
   Dim varFile As Variant


   ' Set up the File Dialog. '
   Set fileDialog = Application.fileDialog(msoFileDialogFilePicker)

   With fileDialog

      ' Allow user to make multiple selections in dialog box '
      .AllowMultiSelect = True

      ' Set the title of the dialog box. '
      .Title = "Please select one or more files"

      ' Clear out the current filters, and add our own.'
      .Filters.Clear
      .Filters.Add "Excel Files", "*.xls"
      .Filters.Add "Excel 2010 Files", "*.xlsx"
      .Filters.Add "Excel xlsm Files", "*.xlsm"
      .InitialFileName = "C:\MyFile\Data\"
      ' Show the dialog box. If the .Show method returns True, the '
      ' user picked at least one file. If the .Show method returns '
      ' False, the user clicked Cancel. '
      If .Show = True Then
       'more cod here
      Else
         MsgBox "You clicked Cancel in the file dialog box."
      End If
   End With

Open in new window

0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

Author Comment

by:AndreasHermle
ID: 39706424
Dear all,

thank you very much for the overwhelming support. I will give it a try soon and then let you know.

Regards,  Andreas
0
 

Author Closing Comment

by:AndreasHermle
ID: 39708152
Dear all,

all of the approaches work just fine and I can learn from all of them.

Thank you very much for your great and professional support, I really appreciate it.

Regards, Andreas
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 39708767
You're very welcome, Andreas.

Thanks for closing the question so promptly.
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question