Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Code to browse for a file

Status
Not open for further replies.

Spurs

Mechanical
Nov 7, 2002
297
Does anyone have VB code that can be used (modified) to browse the path and select a file.

In this case I am looking for an excel file with a .xls extension which has input data for my macro,

Thanks
 
Replies continue below

Recommended for you

Let me clarify a bit more:

After I identify the excel file, I am then looking for a way to read in cells from that excel file into variables in the solidworks macro.

What type of statment is needed to be able to assign a variable in the VB code with the value in a specific Excel cell?

 
You can not do this directly in VBA. The "Common Dialog" object that browses for files in not licensed.

There is a workaround, requiring use of Windows API ("Declare" statements calling functins from Windows dll's). There is an example at this website: <
Below is code from a module I use to open SW files. You can modify it to filer for Excel files.
Code:
' Windows API for the Open Filebox
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" _
    (pOpenfilename As OPENFILENAME) As Long

' structure needed by Windows API
Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type


' for more information on Open- or SaveAs dialogs look at
' [URL unfurl="true"]http://www.mvps.org/vbnet/index.html?code/comdlg/filedlgsoverview.htm[/URL]
Public Function OpenFileDialog() As String
    ' common dialog for browse for desired filename

'set variables for OPENFILENAME type
    Dim OFName As OPENFILENAME
    'Set the filter
    OFName.lpstrFilter = "SW files (*.sld*)" + Chr$(0) + "*.sld*" + Chr$(0) + "SW part files (*.sldprt)" + Chr$(0) + "*.sldprt" + Chr$(0) + "SW assembly files (*.sldasm)" + Chr$(0) + "*.sldasm" + Chr$(0) + "SW drawing files (*.slddrw)" + Chr$(0) + "*.slddrw" + Chr$(0) + "All files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
    'default extension
    OFName.lpstrDefExt = "" + Chr$(0)
    'Set the initial directory
    'OFName.lpstrInitialDir = ""
    OFName.lpstrInitialDir = TargetPathOnly
   'Set the dialog title
    OFName.lpstrTitle = "Select file"

    'Set the structure size
    OFName.lStructSize = Len(OFName)
    'Create a buffer
    OFName.lpstrFile = Space$(254)
    'Set the maximum number of chars
    OFName.nMaxFile = 255
    'Create a buffer
    OFName.lpstrFileTitle = Space$(254)
    'Set the maximum number of chars
    OFName.nMaxFileTitle = 255
    'no extra flags
    OFName.flags = 0

    'Show the 'Open File'-dialog
    If GetOpenFileName(OFName) Then
        OpenFileDialog = Trim$(OFName.lpstrFile)
    Else
        OpenFileDialog = ""
    End If

End Function

[bat]Honesty may be the best policy, but insanity is a better defense.[bat]
-SolidWorks API VB programming help
 
Tic

Thanks - it found the file I needed.

Any idea how i can get a cell value like A10 from that file and assign a variable name to it withing the solidworks vba?

 
Spurs ;

I'm not sure that I understand what you want to do. But as I understand your needs, I advise you to use Excel API.

In oder to do that, you can add as references of your project the "Microsoft Excel X Object Library".

Then you'll able to do same actions than Excel VBA permits you.

SW2007 SP5
Worksatation HP wx4300 2GB
NVidia Quadro FX 3450
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor