20161226xlVBA演示文稿替换文字另存pdf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
Const ModelText As String = "机构名称"
Const ModelName As String = "测试文件.pptx"
 
Sub NextSeven_CodeFrame()
    '应用程序设置
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.Calculation = xlCalculationManual
    '错误处理
    On Error GoTo ErrHandler
    '计时器
    Dim StartTime, UsedTime As Variant
    StartTime = VBA.Timer
 
    '变量声明
    Dim Wb As Workbook
    Dim Sht As Worksheet
    Dim Rng As Range
    Dim Arr As Variant
    Dim EndRow As Long
 
    Dim pApp As Object
    Dim Pre As Object
    'Dim pApp As PowerPoint.Application
    'Dim pre As PowerPoint.Presentation
 
    Dim FindStr As String
    Dim ReplaceStr As String
    Dim FilePath As String
    Dim FolderPath As String
    Dim tmp As String
 
    Dim FileName As String
    FileName = Left(ModelName, InStrRev(ModelName, ".") - 1)
    '实例化对象
    Set Wb = Application.ThisWorkbook
    Set Sht = Wb.Worksheets(1)
    FolderPath = Wb.Path & "\"
    'Set pApp = New PowerPoint.Application
    Set pApp = CreateObject("PowerPoint.Application")
    Debug.Print FolderPath & ModelName
    Set Pre = pApp.Presentations.Open(FolderPath & ModelName)
    With Sht
        EndRow = .Cells(.Cells.Rows.Count, 1).End(xlUp).Row
        Set Rng = .Range("A1:Z" & EndRow)
        Arr = Rng.Value
        For i = LBound(Arr) To UBound(Arr)
            If i = 1 Then
                FindStr = ModelText
                ReplaceStr = Arr(i, 1)
                FilePath = FolderPath & FileName & "_予" & Arr(i, 1) & ".pdf"
                ReplaceAndPublish Pre, FilePath, FindStr, ReplaceStr
            Else
                FindStr = Arr(i - 1, 1)
                ReplaceStr = Arr(i, 1)
                FilePath = FolderPath & FileName & "_予" & Arr(i, 1) & ".pdf"
                ReplaceAndPublish Pre, FilePath, FindStr, ReplaceStr
            End If
        Next i
    End With
    '运行耗时
    UsedTime = VBA.Timer - StartTime
    'MsgBox "本次运行耗时:" & Format(UsedTime, "0.0000000秒")
ErrorExit:        '错误处理结束,开始环境清理
    Pre.Close
    Set Pre = Nothing
    pApp.Quit
    Set pApp = Nothing
    Set Wb = Nothing
    Set Sht = Nothing
    Set Rng = Nothing
 
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.Calculation = xlCalculationAutomatic
    Exit Sub
ErrHandler:
    If Err.Number <> 0 Then
        MsgBox Err.Description & "!", vbCritical, "错误提示!"
        'Debug.Print Err.Description
        Err.Clear
        Resume ErrorExit
    End If
End Sub
Private Sub ReplaceAndPublish(ByVal Pre As Object, ByVal FilePath As String, ByVal FindText As String, ByVal ReplaceText As String)
    Dim sld As PowerPoint.Slide
    Dim shp As PowerPoint.Shape
    Dim Txt As String
    For Each sld In Pre.Slides
        For Each shp In sld.Shapes
            If shp.HasTextFrame = msoTrue Then
                If shp.TextFrame.HasText Then
                    Txt = shp.TextFrame.TextRange.Text
                    If InStr(1, Txt, FindText) > 0 Then
                        shp.TextFrame.TextRange.Text = Replace(Txt, FindText, ReplaceText)
                        Exit For
                    End If
                End If
            End If
        Next
    Next
    Pre.SaveAs FilePath, ppSaveAsPDF
End Sub

  

posted @   wangway  阅读(153)  评论(0编辑  收藏  举报
编辑推荐:
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
阅读排行:
· 开发的设计和重构,为开发效率服务
· 从零开始开发一个 MCP Server!
· Ai满嘴顺口溜,想考研?浪费我几个小时
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
点击右上角即可分享
微信分享提示