×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

VBA Code to browse for a file

VBA Code to browse for a file

VBA Code to browse for a file

(OP)
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

RE: VBA Code to browse for a file

(OP)
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?

RE: VBA Code to browse for a file

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: <http://allapi.mentalis.org/apilist/GetOpenFileName.shtml>

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
' http://www.mvps.org/vbnet/index.html?code/comdlg/filedlgsoverview.htm
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

batHonesty may be the best policy, but insanity is a better defense.bat
http://www.EsoxRepublic.com-SolidWorks API VB programming help

RE: VBA Code to browse for a file

(OP)
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?

RE: VBA Code to browse for a file

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources