Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

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

SW Custom Property Linking in Excel's VB 11

Status
Not open for further replies.

puds

Mechanical
Joined
Jun 25, 2007
Messages
40
Location
CA
I am not a programmer at all, however I have managed to create myself and my company a quick and easy way of tracking our drawings. I simply created a spreadsheet in Excel that automatically updates on open & lists all files in our drawing vault folder by their filename and adds a hyperlink to each of these files. I now want to add the corresponding description, customer name & project # from the custom properties of each of the solidworks files so that they will display in the spreadsheet also, therefore giving us a means of finding our documents without knowing the exact part number. Right now, I have the spreadsheet working so that column A lists the files and hyperlinks them, column B lists the Last Modified Date & column C shows the file path. Column D heading shows Description, Column E heading shows Customer & Column F shows Project, just like they should. However I do not understand how I can retrieve the custom property values and have them added into my spreadsheet. If anybody could help, that would be awesome. Here is my code so far:

Private Sub WORKBOOK_OPEN()
' Searches the selected folders and sub folders for files with the specified
'extension. .xls, .doc, .ppt, etc.
'A new worksheet is produced called "File Search Results". You can click on the link and go directly
'to the file you need.
Dim i As Long, z As Long, Rw As Long
Dim ws As Worksheet
Dim y As Variant
Dim fLdr As String, Fil As String, FPath As String

y = "*.*"
If y = False And Not TypeName(y) = "STRING" Then Exit Sub
Application.ScreenUpdating = False

'**********************************************************************
'fLdr = BrowseForFolderShell
fLdr = "P:\DRAWING_VAULT"

'**********************************************************************
With Application.FileSearch
.NewSearch
.LookIn = fLdr
.SearchSubFolders = False
.Filename = y
Set ws = ThisWorkbook.Worksheets.Add(Sheets(1))
On Error GoTo 1
2: ws.Name = "TMC_DRAWING_LIST"
On Error GoTo 0
If .Execute() > 0 Then
For i = 1 To .FoundFiles.Count
Fil = .FoundFiles(i)
'Get file path from file name
FPath = Left(Fil, Len(Fil) - Len(Split(Fil, "\")(UBound(Split(Fil, "\")))) - 1)
If Left$(Fil, 1) = Left$(fLdr, 1) Then
If CBool(Len(Dir(Fil))) Then
z = z + 1
ws.Cells(z + 1, 1).Resize(, 6) = _
Array(Dir(Fil), _
FileDateTime(Fil), _
FPath, _
'***this is where I would assume I need to add the callouts for description, customer & project**
)



ws.Hyperlinks.Add Anchor:=Cells(z + 1, 1), _
Address:=.FoundFiles(i)
End If
End If
Next i
End If
End With

ActiveWindow.DisplayHeadings = False

With ws
Rw = .Cells.Rows.Count
With .[A1:F1]
.Value = [{"FILE NAME (CLICK TO OPEN)","LAST MODIFIED", "PATH"," FILE DESCRIPTION","CUSTOMER","PROJECT/WORKORDER NO."}]
.Font.ColorIndex = vbBlack
.Font.Bold = True
.Font.Size = 11
.Cells.Interior.Color = vbGreen
.EntireColumn.AutoFit
.HorizontalAlignment = xlCenter
End With
.[G1:IV1 ].EntireColumn.Hidden = True
On Error Resume Next
Range(Cells(Rw, "A").End(3)(2), Cells(Rw, "A")).EntireRow.Hidden = True
Range(.[A2 ], Cells(Rw, "C")).Sort [A2 ], xlAscending, Header:=xlNo
End With

Application.ScreenUpdating = True
Exit Sub
1: Application.DisplayAlerts = False
Worksheets("TMC_DRAWING_LIST").Delete
Application.DisplayAlerts = True
GoTo 2
End Sub












 
tried it but it still gives me an error when one of the properties hasn't been filled in in a file (such as description). It won't return a blank value. I think this is the last issue too....if I can get this beat, my sheet will be up and running finally! Please any help would be great!
 
Sub WORKBOOK_OPEN()


Dim i As Long, z As Long, Rw As Long
Dim ws As Worksheet
Dim y As Variant
Dim fLdr As String
Dim fil As String
Dim DSO As DSOFile.OleDocumentProperties
Dim FPath As String

Dim Object As String
Set OBJFILE = CreateObject("DSOFile.OleDocumentProperties")
y = "*.*"
If y = False And Not TypeName(y) = "STRING" Then Exit Sub
Application.ScreenUpdating = False

'**********************************************************************
'fLdr = BrowseForFolderShell
fLdr = "P:\DRAWING_VAULT"

'**********************************************************************

With Application.FileSearch
.NewSearch
.LookIn = fLdr
.SearchSubFolders = False
.FileName = y

Set ws = ThisWorkbook.Worksheets.Add(Sheets(1))
On Error GoTo 1
2: ws.Name = "TMC DRAWING LIST"

On Error GoTo 0
If .Execute() > 0 Then
For i = 1 To .FoundFiles.Count
fil = .FoundFiles(i)
Set DSO = New DSOFile.OleDocumentProperties
DSO.Open SFileName:=fil
'Get file path from file name

FPath = Left(fil, Len(fil) - Len(Split(fil, "\")(UBound(Split(fil, "\")))) - 1)
If Left$(fil, 1) = Left$(fLdr, 1) Then
If CBool(Len(Dir(fil))) Then
z = z + 1
ws.Cells(z + 1, 1).Resize(, 8) = _
Array(Dir(fil), _
DSO.SummaryProperties.DateLastSaved, _
IIf(Not DSO.CustomProperties.Item("description") Is Nothing, DSO.CustomProperties.Item("description").Value, ""), _
IIf(Not DSO.CustomProperties.Item("customer") Is Nothing, DSO.CustomProperties.Item("customer").Value, ""), _
IIf(Not DSO.CustomProperties.Item("project") Is Nothing, DSO.CustomProperties.Item("project").Value, ""), _
IIf(Not DSO.CustomProperties.Item("userdefined1") Is Nothing, DSO.CustomProperties.Item("userdefined1").Value, ""), _
IIf(Not DSO.CustomProperties.Item("userdefined2") Is Nothing, DSO.CustomProperties.Item("userdefined2").Value, ""), _
IIf(Not DSO.CustomProperties.Item("drawnby") Is Nothing, DSO.CustomProperties.Item("drawnby").Value, ""))

ws.Hyperlinks.Add Anchor:=Cells(z + 1, 1), _
Address:=.FoundFiles(i)
End If
End If
Next i
End If
End With

ActiveWindow.DisplayHeadings = False

With ws
Rw = .Cells.Rows.Count
With .[A1:H1]
.Value = [{"FILE NAME (CLICK TO OPEN)","LAST MODIFIED"," FILE DESCRIPTION","CUSTOMER","WORKORDER NO.","COMPUTER ID NO.","CUSTOMER DWG NO.","AUTHOR"}]
.Font.ColorIndex = vbBlack
.Font.Bold = True
.Font.Size = 11
.Cells.Interior.Color = vbGreen
.EntireColumn.AutoFit
.HorizontalAlignment = xlCenter
End With
.[I1:IV1 ].EntireColumn.Hidden = True
On Error Resume Next
Range(Cells(Rw, "A").End(3)(2), Cells(Rw, "A")).EntireRow.Hidden = True
Range(.[A2 ], Cells(Rw, "C")).Sort [A2 ], xlAscending, Header:=xlNo
End With

Application.ScreenUpdating = True
Exit Sub
1: Application.DisplayAlerts = False
Worksheets("TMC DRAWING LIST").Delete
Application.DisplayAlerts = True
GoTo 2
End Sub












 
Custom property names are case-sensitive. Therefore "Description" doesn't equal "description"
 
checked/changed all of the property names to match, still having the same problem. Maybe the file doesn't actually have the "description" property unless it has a value applied to it. I am using another macro to add these custom properties before I run this spreadsheet list (the files do not automatically have these properties when the files are saved).
 
OK, this should fix you. I forgot a couple of things. The first is that any attempt to access a non-existent collection member results in an error. The second is that each statement of an IIF is evaluated regardless of the condition. Anyway, what you need to do is replace each

IIf(Not DSO.CustomProperties.Item("[name]") Is Nothing, DSO.CustomProperties.Item("[name]").Value, "")

with

IsProp(DSO.CustomProperties, "[name]")

IsProp is a function I wrote that will check for existence of a property by name and return either the value or the words "Property Not Present". You need to copy this function and paste it at the bottom of your code, outside the last "End Sub" statement.

Code:
Function IsProp(myCProps As Object, myMemName As String) As String
    Dim testProp As Object
    On Error Resume Next
    Set testProp = myCProps(myMemName)
    If testProp Is Nothing Then
        IsProp = "Property Not Present"
    Else
        IsProp = testProp.Value
    End If
    On Error GoTo 0
End Function
 
Awesome, thank you so much Handleman! By any change do you know how to automatically turn on autofilter in excel? It's about the only thing that I would like to add. Other than that, this is great. If anyone may be interested in the code, feel free to ask and I can post the latest and now working code!
 
I have tried
ws.EnableAutoFilter = True

but it doesn't seem to do anything. I am just trying to get the autofilter arrows to show up on worksheet open. Just so I dont have to go to Data > Filter > AutoFilter manually each time I open the excel file. Any suggestions?
 
puds, I am interested in your code, please post it.
 
I turned the AutoFilter on while recording a macro in Excel (a good way to find the VB code for menu choices). The resulting code was:
Code:
Selection.AutoFilter

Eric
 
Here you go:

Sub WORKBOOK_OPEN()

Dim i As Long, z As Long, Rw As Long
Dim ws As Worksheet
Dim y As Variant
Dim fLdr As String
Dim fil As String
Dim DSO As DSOFile.OleDocumentProperties
Dim FPath As String

Dim Object As String
Set OBJFILE = CreateObject("DSOFile.OleDocumentProperties")
y = "*.*"
If y = False And Not TypeName(y) = "STRING" Then Exit Sub
Application.ScreenUpdating = False

'**********************************************************************
'fLdr = BrowseForFolderShell
fLdr = "P:\DRAWING_VAULT" 'this is the folder where your drawings/files are contained

'**********************************************************************

With Application.FileSearch
.NewSearch
.LookIn = fLdr
.SearchSubFolders = False 'you can search subfolders if you want
.FileName = y

Set ws = ThisWorkbook.Worksheets.Add(Sheets(1))
On Error GoTo 1
2: ws.Name = "TMC DRAWING LIST" 'call your sheet anything you like
ws.EnableAutoFilter = True
On Error GoTo 0
If .Execute() > 0 Then
For i = 1 To .FoundFiles.Count
fil = .FoundFiles(i)
Set DSO = New DSOFile.OleDocumentProperties
DSO.Open SFileName:=fil
'Get file path from file name

FPath = Left(fil, Len(fil) - Len(Split(fil, "\")(UBound(Split(fil, "\")))) - 1)
If Left$(fil, 1) = Left$(fLdr, 1) Then
If CBool(Len(Dir(fil))) Then
z = z + 1
ws.Cells(z + 1, 1).Resize(, 8) = _
Array(Dir(fil), _
DSO.SummaryProperties.DateLastSaved, _
IsProp(DSO.CustomProperties, "Description"), _
IsProp(DSO.CustomProperties, "Customer"), _
IsProp(DSO.CustomProperties, "Project"), _
IsProp(DSO.CustomProperties, "UserDefined1"), _
IsProp(DSO.CustomProperties, "UserDefined2"), _
IsProp(DSO.CustomProperties, "DrawnBy"))



ws.Hyperlinks.Add Anchor:=Cells(z + 1, 1), _
Address:=.FoundFiles(i)
End If
End If
Next i
End If
End With

ActiveWindow.DisplayHeadings = False

With ws
Rw = .Cells.Rows.Count
With .[A1:H1]
.Value = [{"FILE NAME (CLICK TO OPEN)","LAST MODIFIED"," FILE DESCRIPTION","CUSTOMER","WORKORDER NO.","COMPUTER ID NO.","CUSTOMER DWG NO.","AUTHOR"}]
.Font.ColorIndex = vbBlack
.Font.Bold = True
.Font.Size = 11
.Cells.Interior.Color = vbGreen
.EntireColumn.AutoFit
.HorizontalAlignment = xlCenter
End With
.[I1:IV1 ].EntireColumn.Hidden = True
On Error Resume Next
Range(Cells(Rw, "A").End(3)(2), Cells(Rw, "A")).EntireRow.Hidden = True
Range(.[A2 ], Cells(Rw, "C")).Sort [A2 ], xlAscending, Header:=xlNo
End With

Application.ScreenUpdating = True
Exit Sub
1: Application.DisplayAlerts = False
Worksheets("TMC DRAWING LIST").Delete
Application.DisplayAlerts = True
GoTo 2
End Sub

Function IsProp(myCProps As Object, myMemName As String) As String
Dim testProp As Object
On Error Resume Next
Set testProp = myCProps(myMemName)
If testProp Is Nothing Then
IsProp = "N/A"
Else
IsProp = testProp.Value
End If
On Error GoTo 0
End Function










 
Thanks puds. A star for you for your effort and a star for handleman for his help. We would be lost without him.
 
Handleman,
I have one more glich I need to get rid of, hopefully you or someone else can help. When I am opening my spreadsheet, if someone else on the network has one of the files open that would be included in my list of files in my spreadsheet, I get a path/file access error and my spreadsheet doesn't update or display properly. From what I understand, I need to use the dso read only option or something along those lines, I am just unsure of the exact code and placement in my current code for this to work effectively. Any help would be great!! Thanks again!
 
You should just be able to change the line

DSO.Open SFileName:=fil

to

DSO.Open SFileName:=fil, True

and that should do it.

 
It didn't work - I am getting an error:

compile error - expected named parameter

(it highlights "true")
 
Sorry, I don't use the named parameters very much, but I'm guessing that if one parameter is named they all have to be named. So you would need to use

DSO.Open SFileName:=fil, ReadOnly:=True

Basically, the lines "DSO.Open fil, True" and "DSO.Open SFileName:=fil, ReadOnly:=True" are exactly the same. The second one just specifies the name of the argument along with its value. The first relies on the order of arguments to determine which is which.
 
I am still getting a path/file access error - how can I add the dsooptionopenreadonlyifnowriteaccess line? Maybe that will help?
 
I tried
DSO.Open SFileName:=fil, ReadOnly:=True, dsofileoptions:=dsoOptionOpenReadOnlyIfNoWriteAccess

and received error 'named argument not found'
 
Try:
DSO.Open SFileName:=fil, ReadOnly:=True, dsoFileOpenOptions:=dsoOptionOpenReadOnlyIfNoWriteAccess
 
tried it.....got named argument not found and it highlights the dsofileopenoptions code
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top