以下代码为ThisWorkbook中的每个Sheet(不包括任何Worksheet)生成一个pdf文件:
Sub Charts_Export()
Const kPath As String = "D:\@D_Trash\SO Questions\Output\#Name.pdf" 'Update as required
Dim oSht As Object, sPath As String
With ThisWorkbook
For Each oSht In .Sheets
With oSht
If oSht.Type <> xlWorksheet Then
sPath = Replace(kPath, "#Name", .Name) 'Update as required
.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=sPath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End If: End With: Next: End With
End Sub
打开 pdf 文件后,同时按 Shift + Ctrl + Y 以激活 pdf 中的 Read Out Loud 选项.然后同时按 Shift + Ctrl + V 读取AlternativeText。
之前的代码使用了 OP 发布的同一篇文章,将图表导出为 pdf 文件,每个文件中都包含 Alternative text。
这似乎表明问题可能是由于用于将AlternativeText 添加到Chart 的方法。一旦将AlternativeText 作为Sheet 移动,我找不到将AlternativeText 添加到Chart 的方法,因此必须在将Chart 移动到Sheet 之前添加AlternativeText,当Chart 仍然是工作表中的对象 (Shape)。
使用此方法将AlternativeText 添加到每个Chart,然后再将其移动到Sheet`。
Private Sub Charts_Add_AlternativeText()
Const kAltTxt As String = "This is a test of the Alt Text in graph [#Name]" 'Update as required
Dim ws As Worksheet
Dim co As ChartObject
Set ws = ThisWorkbook.Worksheets("DATA") 'Update as required
For Each co In ws.ChartObjects
co.ShapeRange.AlternativeText = Replace(kAltTxt, "#Name", co.Name) 'Update as required
Next
End Sub
或使用此方法将AlternativeText 添加到每个Chart 工作表中。
Private Sub Charts_Add_AlternativeText()
Const kWsName As String = "!Temp"
Const kAltTxt As String = "This is a test of the Alt Text in graph [#Name]" 'Update as required
Dim wb As Workbook, ws As Worksheet
Dim oSht As Object, sp As Shape
Dim sChName As String, bIdx As Byte
With Application
.EnableEvents = False
.DisplayAlerts = False
.ScreenUpdating = False
.Application.Calculation = xlCalculationManual
End With
Set wb = ThisWorkbook
With wb
Rem Add Temp Worksheet
On Error Resume Next
.Worksheets(kWsName).Delete
On Error GoTo 0
Set ws = .Worksheets.Add(After:=.Sheets(.Sheets.Count))
ws.Name = kWsName
Rem Work with Chart Sheets
For Each oSht In .Sheets
With oSht
If oSht.Type <> xlWorksheet Then
Rem Move Chart to Temp Worksheet
bIdx = .Index
sChName = .Name
.Location Where:=xlLocationAsObject, Name:=kWsName
Set sp = ws.Shapes(1)
With sp
Rem Add AlternativeText to Shape (Chart)
.AlternativeText = Replace(kAltTxt, "#Name", sChName) 'Update as required
Rem Move Chart to Chart Sheet
.Chart.Location Where:=xlLocationAsNewSheet, Name:=sChName
wb.Sheets(sChName).Move Before:=wb.Sheets(bIdx)
End With: End If: End With: Next: End With
With Application
.EnableEvents = True
.DisplayAlerts = True
.ScreenUpdating = True
.Application.Calculation = xlCalculationAutomatic
End With
End Sub