VBA

VBA

ebarlevi
Advocate Advocate
550 Views
9 Replies
Message 1 of 10

VBA

ebarlevi
Advocate
Advocate
I need to serarch for a file using a browser in VBA.
Once I find the file, I want to use it's name later in the program.
Also, I need to pass the file name to LISP.
Thanks.
0 Likes
551 Views
9 Replies
Replies (9)
Message 2 of 10

Anonymous
Not applicable
Hello ebarlevi Happy New Year here is a VBA Code for Excel is for extract file name into spreadsheet, maybe this code it will be helpfully for you. Copy and paste on Excel Module, then review if help you. I don't know what is exactly do you want to do but i think this help you.With this code I get DXF files and then open one by one and apply some changes to the drawings.

Public h As Long
Public objShell, objFolder, objFolderItem
Public FSO, oFolder, Fil
Public NewSht As Worksheet
Public FolderSelection As String
Public Col As Integer
Public ColConfig As Integer
Public zz As Integer
Public TextToSearch As String
Public Sub MainExtractData()
Dim x As Integer
Col = 13
zz = 5
ColConfig = 12
FolderSelection = "Selecciona el Directorio Principal de los DXF"
Dim MainFolderName As String
MainFolderName = BrowseForFolder()
h = 5
Set FSO = CreateObject("scripting.FileSystemObject")
Set oFolder = FSO.GetFolder(MainFolderName)
On Error Resume Next
For Each Fil In oFolder.Files
Set objFolder = objShell.Namespace(oFolder.Path)
Set objFolderItem = objFolder.ParseName(Fil.Name)
'Here you can select or change the kind of files do you want for example here is a DXF file from autocad.
If UCase(Fil.Name) Like "*.DXF" Then
' On Error Resume Next
ActiveWorkbook.Worksheets(1).Cells(h, ColConfig) = UCase(Fil.Name)
' On Error Resume Next
ActiveWorkbook.Worksheets(1).Cells(h, Col) = oFolder.Path
h = h + 1
End If
Next
Call RecursiveFolder(oFolder)
zz = zz + 1
If IsEmpty(ActiveWorkbook.Worksheets(1).Cells(5, 1)) = True Then
MsgBox "No hay archivos DXF 😞 de la configuración en este folder: " & oFolder.Path
End If
Set FSO = Nothing
Set objShell = Nothing
Set oFolder = Nothing
Set objFolder = Nothing
Set objFolderItem = Nothing
Set Fil = Nothing
Application.ScreenUpdating = True
End Sub
Sub RecursiveFolder(xFolder)
Dim SubFld
For Each SubFld In xFolder.SubFolders
Set oFolder = FSO.GetFolder(SubFld)
'Set objFolder = objShell.Namespace(SubFld.Path)
For Each Fil In SubFld.Files
Set objFolder = FSO.GetFolder(oFolder.Path)
'Problem with objFolder at times
If Not objFolder Is Nothing Then
'Set objFolderItem = objFolder.ParseName(Fil.Name)
If UCase(Fil.Name) Like "*.DXF" Then
On Error Resume Next
ActiveWorkbook.Worksheets(1).Cells(h, ColConfig) = UCase(Fil.Name)
On Error Resume Next
ActiveWorkbook.Worksheets(1).Cells(h, Col) = oFolder.Path
h = h + 1
End If
Else
Debug.Print Fil.Path & " " & Fil.Name
End If
Next
Call RecursiveFolder(SubFld)
Next
End Sub
Function BrowseForFolder(Optional OpenAt As Variant) As Variant
'Function purpose: To Browser for a user selected folder.
'If the "OpenAt" path is provided, open the browser at that directory
'NOTE: If invalid, it will open at the Desktop level
Dim ShellApp As Object
'Create a file browser window at the default folder
Set ShellApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, FolderSelection, 0, OpenAt)
'Set the folder to that selected. (On error in case cancelled)
On Error Resume Next
BrowseForFolder = ShellApp.self.Path
On Error GoTo 0
'Destroy the Shell Application
Set ShellApp = Nothing
'Check for invalid or non-entries and send to the Invalid error
'handler if found
'Valid selections can begin L: (where L is a letter) or
'\\ (as in \\servername\sharename. All others are invalid
Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
Case Is = "\"
If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
Case Else
GoTo Invalid
End Select
Exit Function
Invalid:
'If it was determined that the selection was invalid, set to False
BrowseForFolder = False
End Function
0 Likes
Message 3 of 10

Anonymous
Not applicable
Hi ebarlevi,

The attached File dialog class will do it for you.

Look at the notes in the class to see how to use it.



Regards


Laurie Comerford

ebarleviwrote:
> I need to serarch for a file using a browser in VBA. Once I find the
> file, I want to use it's name later in the program. Also, I need to pass
> the file name to LISP. Thanks.
0 Likes
Message 4 of 10

ebarlevi
Advocate
Advocate
Thank you for your help.
Please let me be more specific:
Bellow you will find a sub taken from an AutoCAD sample DVB project.
Regardless of the main purpose of the application, please look at the at the line that reads
sXmlFileName = Application.Path & "\Sample\Civil 3D API\Vba\SurfacePoints\EG.xml"
All I want, is to search for the xml file myself, instead of using the above file.
I would be thankful if you could direct me how to use tour program to search for the file.
Again, thanks

Public Function CreateSurfaceByImportXML() As Boolean
Dim oSurfaces As AeccSurfaces
Set oSurfaces = g_oDocument.Surfaces

' Get a reference to the existing surface ("EG"). If
' it does not exist, try loading surface data from a
' XML file.
Dim oSurface As AeccSurface
Dim sXmlFileName As String
On Error Resume Next
Set oSurface = oSurfaces.Item("EG")
On Error GoTo 0

If (oSurface Is Nothing) Then
sXmlFileName = Application.Path & "\Sample\Civil 3D API\Vba\SurfacePoints\EG.xml"
Set oSurface = oSurfaces.ImportXML(sXmlFileName)
If (oSurface Is Nothing) Then
Debug.Print "Error loading XML file: " & Err.Description
CreateSurfaceByImportXML = False
Err.Clear
End If
End If

' Fill the screen with the surface, and show the triangles
' that make up the surface.
ThisDrawing.Application.ZoomExtents
Call DisplayBorderTrianglesOnly

CreateSurfaceByImportXML = True
End Function
0 Likes
Message 5 of 10

ebarlevi
Advocate
Advocate
Thank you for your help.
Please let me be more specific:
Bellow you will find a sub taken from an AutoCAD sample DVB project.
Regardless of the main purpose of the application, please look at the at the line that reads
sXmlFileName = Application.Path & "\Sample\Civil 3D API\Vba\SurfacePoints\EG.xml"
All I want, is to search for the xml file myself, instead of using the above file.
I would be thankful if you could direct me how to use tour program to search for the file.
Again, thanks

Public Function CreateSurfaceByImportXML() As Boolean
Dim oSurfaces As AeccSurfaces
Set oSurfaces = g_oDocument.Surfaces

' Get a reference to the existing surface ("EG"). If
' it does not exist, try loading surface data from a
' XML file.
Dim oSurface As AeccSurface
Dim sXmlFileName As String
On Error Resume Next
Set oSurface = oSurfaces.Item("EG")
On Error GoTo 0

If (oSurface Is Nothing) Then
sXmlFileName = Application.Path & "\Sample\Civil 3D API\Vba\SurfacePoints\EG.xml"
Set oSurface = oSurfaces.ImportXML(sXmlFileName)
If (oSurface Is Nothing) Then
Debug.Print "Error loading XML file: " & Err.Description
CreateSurfaceByImportXML = False
Err.Clear
End If
End If

' Fill the screen with the surface, and show the triangles
' that make up the surface.
ThisDrawing.Application.ZoomExtents
Call DisplayBorderTrianglesOnly

CreateSurfaceByImportXML = True
End Function
0 Likes
Message 6 of 10

Anonymous
Not applicable
ebarlevi see for pretty windows API file browser, just paste it into module

http://discussion.autodesk.com/forums/thread.jspa?threadID=708311&tstart=30

{code}
If (oSurface Is Nothing) Then
sXmlFileName = ShowOpen("Choose a file", "*.XML", , "C:\Windows")
Set oSurface = oSurfaces.ImportXML(sXmlFileName)
If (oSurface Is Nothing) Then
Debug.Print "Error loading XML file: " & Err.Description
CreateSurfaceByImportXML = False
Err.Clear
End If
End If
{code}
0 Likes
Message 7 of 10

ebarlevi
Advocate
Advocate
Thank you so much.
Works like a charm.
0 Likes
Message 8 of 10

Anonymous
Not applicable
Hi,

This is just for curiosity.

Enclosing the code with {code} does not work to ensure formatting
correctly for Thunderbird.

Enclosing with ... should work


If (oSurface Is Nothing) Then
sXmlFileName = ShowOpen("Choose a file", "*.XML", , "C:\Windows")
Set oSurface = oSurfaces.ImportXML(sXmlFileName)
If (oSurface Is Nothing) Then
Debug.Print "Error loading XML file: " & Err.Description;
CreateSurfaceByImportXML = False
Err.Clear
End If
End If




Regards


Laurie Comerford

cadger wrote:
> ebarlevi see for pretty windows API file browser, just paste it into
> module
> http://discussion.autodesk.com/forums/thread.jspa?threadID=708311&tstart=30
> {code} If (oSurface Is Nothing) Then sXmlFileName = ShowOpen("Choose a
> file", "*.XML", , "C:\Windows") Set oSurface =
> oSurfaces.ImportXML(sXmlFileName) If (oSurface Is Nothing) Then
> Debug.Print "Error loading XML file: " & Err.Description
> CreateSurfaceByImportXML = False Err.Clear End If End If {code}
0 Likes
Message 9 of 10

Anonymous
Not applicable
what did you find out laurie? { code } seems to work well for chrome
0 Likes
Message 10 of 10

Anonymous
Not applicable
Hi cadger,

I simply confirmed that {code} {code} doesn't work in Thunderbird (and
Outlook express) and does. I don't use the Web site, but
have seen no complaints about code posted with

It seems the squiggly braces are a characteristic of the RTF format,
whereas the <> braces are used in HTML. It certainly makes you wonder
what sort of incompetent at Autodesk decided to mix RTF into a web/email
environment when it was painfully obvious that it wouldn't/doesn't work
in Outlook Express and Thunderbird which between them are probably used
by well over 50% of email newsgroup readers.

Regards


Laurie Comerford

cadger wrote:
> what did you find out laurie? { code } seems to work well for chrome
0 Likes