A family of Microsoft word processing software products for creating web, email, and print documents.
Hi @Julian Melissa,
Thank you for providing the updated details. We appreciate the extra context you’ve shared regarding your workflow.
Based on your description, you are encountering difficulties running your script that reads PNG file names from Excel column A and uses the descriptions in column B to populate Alt Text in a Word document, with an additional error about not finding the spreadsheet despite the correct path and naming.
Below are some workarounds that suit your situation:
I created a sample Word document with two sample PNG pictures (warning.png and house/png), which were downloaded from the internet.
For the subscription in the Excel sheet, I use the same two columns as you're doing in your file: file name in column A and alt_text in column B.
Now in the Word document, press Alt + F11 to open the VBA editor or open it via the Developer tab > Visual Basic. Then press Insert > Module and paste this VBA script into it:
Sub ApplyAltTextFromExcel()
Dim xlApp As Object
Dim xlWB As Object
Dim xlSheet As Object
Dim dict As Object
Dim lastRow As Long
Dim i As Long
Set dict = CreateObject("Scripting.Dictionary")
'OPEN EXCEL FILE
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
'ADD YOUR EXCEL FILE PATH HERE
Set xlWB = xlApp.Workbooks.Open("C:\Users\Windows\Desktop\pngname.xlsx")
Set xlSheet = xlWB.Sheets(1)
'READ COLUMN A + B
lastRow = xlSheet.Cells(xlSheet.Rows.Count, 1).End(-4162).Row
For i = 2 To lastRow 'need to skip header row so start from i=2
If xlSheet.Cells(i, 1).Value <> "" Then
dict(Trim(CStr(xlSheet.Cells(i, 1).Value))) = Trim(CStr(xlSheet.Cells(i, 2).Value))
End If
Next i
'CLOSE EXCEL FILE
xlWB.Close False
xlApp.Quit
Set xlApp = Nothing
'LOOP THROUGH INLINE SHAPES
Dim ils As InlineShape
Dim fileName As String
Dim rID As String
Dim part As Object
For Each ils In ActiveDocument.InlineShapes
If ils.Type = wdInlineShapePicture Or ils.Type = wdInlineShapeLinkedPicture Then
On Error Resume Next
rID = ils.Range.InlineShapes(1).LinkFormat.SourceFullName
If rID = "" Then
rID = ils.LinkFormat.SourceFullName
End If
If rID <> "" Then
fileName = Mid(rID, InStrRev(rID, "") + 1)
If dict.Exists(fileName) Then
ils.AlternativeText = dict(fileName)
End If
End If
On Error GoTo 0
End If
Next ils
Dim shp As Shape
For Each shp In ActiveDocument.Shapes
If shp.Type = msoPicture Then
On Error Resume Next
fileName = Mid(shp.LinkFormat.SourceFullName, InStrRev(shp.LinkFormat.SourceFullName, "") + 1
If dict.Exists(fileName) Then
shp.AlternativeText = dict(fileName)
End If
On Error GoTo 0
End If
Next shp
MsgBox "Import successfully"
End Sub
Remember to add the correct path of the Excel file into the script. You can copy the right path in File Explorer > right-click the Excel file > Copy as path.
Then close the VBA editor. To run the VBA script, press Alt+F8 then select the ApplyAltTextFromExcel > Run.
Now all the two PNGs will be added with their corresponding descriptions in alt text.
I hope the information provided proves useful. Please proceed with the outlined steps and let me know whether they resolve the issue. If not, I’ll be glad to continue working with you to find a solution.
Thank you for your kindness and cooperation throughout this process. Should you have any questions or need further assistance, feel free to reach out at any time.
I look forward to your thoughts on this.