The code below is not retrieving any data: Sub RetrieveLinkedDocsInfo() Dim folderPath…
- User
The code below is not retrieving any data: Sub RetrieveLinkedDocsInfo() Dim folderPath As String Dim folderPathLength As Integer Dim fileName As String Dim fileExtension As String Dim sheetName As String Dim linkedDocsSheet As Worksheet Dim i As Integer Dim vbComp As Object Dim vbModule As Object Dim codeLines As Variant Dim codeLine As Variant
' Update the folder path to the location of your "zzzz ServProvDocs" folder folderPath = "G:\Shared drives\Swan School Site Premises\PREMISES MANAGEMENT\SERVICE PROVIDERS\zzzz ServProvDocs" ' Set the worksheet object to the "Linked Docs" sheet Set linkedDocsSheet = ThisWorkbook.Sheets("Linked Docs") ' Clear the existing contents in the "Linked Docs" sheet linkedDocsSheet.Cells.ClearContents ' Write the headers in the "Linked Docs" sheet linkedDocsSheet.Range("A1").Value = "Name of the document" linkedDocsSheet.Range("B1").Value = "Document type" linkedDocsSheet.Range("D1:G1").Value = "Sheet Name" ' Get the length of the folder path folderPathLength = Len(folderPath) ' Loop through each file in the folder i = 2 ' Starting row in the "Linked Docs" sheet fileName = Dir(folderPath) Do While fileName <> "" ' Get the file name without the folder path fileName = Mid(fileName, folderPathLength + 1) ' Get the file extension fileExtension = Mid(fileName, InStrRev(fileName, ".") + 1) ' Get the sheet name(s) where the document is linked sheetName = "N/A" ' Default value if not found ' Search for the document link in VBA code modules For Each vbComp In ThisWorkbook.VBProject.VBComponents If vbComp.Type = 1 Then ' Module Set vbModule = vbComp.CodeModule codeLines = vbModule.Lines For Each codeLine In codeLines If InStr(1, codeLine, fileName) > 0 Then sheetName = vbComp.Name Exit For End If Next codeLine If sheetName <> "N/A" Then Exit For End If Next vbComp ' Write the document information in the "Linked Docs" sheet linkedDocsSheet.Cells(i, 1).Value = fileName linkedDocsSheet.Cells(i, 2).Value = fileExtension linkedDocsSheet.Cells(i, 4).Value = sheetName ' Move to the next row in the "Linked Docs" sheet i = i + 1 ' Move to the next file in the folder fileName = Dir LoopEnd Sub
- Assistant
The code appears to be retrieving data by searching for document links in VBA code modules within a specified folder and writing the information to a worksheet called "Linked Docs". However, there are a few things you can check to troubleshoot why it may not be retrieving any data:
-
Make sure the folder path specified in the code is correct and accessible. Double-check the path and ensure it points to the correct folder location where the documents are stored.
-
Ensure that the worksheet "Linked Docs" exists in the workbook that contains the VBA code. If the worksheet doesn't exist, you can create it manually or modify the code to create it dynamically.
-
Check if there are any documents with supported extensions (e.g., .docx, .xlsx, .pdf, etc.) in the specified folder. If there are no matching files, the code will not retrieve any data.
-
Confirm that the VBA project is enabled and not locked. Sometimes, security settings or macro settings may prevent the code from accessing and retrieving data from VBA code modules.
By checking these points and ensuring everything is set up correctly, you should be able to troubleshoot and fix the issue.
-
- User
All the checks are good, but no data is retrieved
- Assistant
If you have checked all the possible issues mentioned above and the code still does not retrieve any data, there may be no VBA code modules in the workbook that contain references to the documents in the specified folder.
In this case, you can try the following troubleshooting steps:
-
Ensure that the VBA code modules in the workbook actually contain references to the documents in the folder. You can manually check the code modules and verify if there are any references to the documents that should be retrieved.
-
Check if the documents are referenced in a different way in the VBA code modules, such as using relative paths or different file name formats. The current code only checks for an exact match of the full file name (including extension) in the code modules. Modify the code accordingly to handle different variations if necessary.
-
- User
code does not seem to work. Can you kindly re-examine it again: Sub RetrieveLinkedDocsInfo() Dim folderPath As String Dim folderPathLength As Integer Dim fileName As String Dim fileExtension As String Dim sheetName As String Dim linkedDocsSheet As Worksheet Dim i As Integer Dim vbComp As Object Dim vbModule As Object Dim codeLines As Variant Dim codeLine As Variant
- Assistant