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
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
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
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:/
Below is code from a module I use to open SW files. You can modify it to filer for Excel files.
CODE
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
http://www.EsoxRepublic.com-SolidWorks API VB programming help
RE: VBA Code to browse for a file
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
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