使用 VBA 从 Excel 2010 查找并替换 Powerpoint 2010 中的文本

2023-12-07

我成功地在 powerpoint odule 中使用了这段代码,但是当我将其移动到我的 excel 模块中时,它给我带来了几个问题。我在 Excel 的第 1 页上嵌入了 Powerpoint 应用程序。目标是从 Excel 生成 Powerpoint,并在 Powerpoint 幻灯片上出现时用 Excel 范围中的新公司名称替换​​公司名称。 我收到错误 429 ActiveX 组件无法在“对于 ActivePresentation.Slides 中的每个 osld”创建对象。我的 Powerpoint 演示文稿未激活吗?任何帮助将不胜感激。使用 excel/Powerpoint 2010。

Sub changeme(sFindMe As String, sSwapme As String) 
Dim osld As Slide 
Dim oshp As Shape 
Dim otemp As TextRange 
Dim otext As TextRange 
Dim Inewstart As Integer 



For Each osld In ActivePresentation.Slides 
For Each oshp In osld.Shapes 
    If oshp.HasTextFrame Then 
        If oshp.TextFrame.HasText Then 

            Set otext = oshp.TextFrame.TextRange 
            Set otemp = otext.Replace(sFindMe, sSwapme, , msoFalse, msoFalse) 
            Do While Not otemp Is Nothing 
                Inewstart = otemp.Start + otemp.Length 
                Set otemp = otext.Replace(sFindMe, sSwapme, Inewstart, msoFalse, msoFalse) 
            Loop 

        End If 
    End If 

Next oshp 
Next osld 
End Sub 
 '-------------------------------------------------------------------------
Sub swap() 
Dim sFindMe As String 
Dim sSwapme As String 
Dim ppApp As PowerPoint.Application 
Dim ppPreso As PowerPoint.Presentation 

 'Start Powerpoint

 'Look for existing instance
On Error Resume Next 
Set ppApp = GetObject(, "PowerPoint.Application") 
On Error Goto 0 

 'Create new instance if no instance exists
Set ppApp = CreateObject("Powerpoint.Application") 



 'Open Template in word
With Sheets("Sheet1").Shapes("Object 1").OLEFormat.Verb(Verb:=xlVerbOpen) 
End With 
 'Make it visible
ppApp.Visible = True 



sFindMe = "Name To Find" 
 'change this to suit
sSwapme = "New Name" 
Call changeme(sFindMe, sSwapme) 
 'sFindMe = "<find2>"
 'sSwapme = ActivePresentation.Slides(1).Shapes(2).TextFrame.TextRange
 'Call changeme(sFindMe, sSwapme)
End Sub 

ActivePresentation是一个Powerpoint对象。这对 Excel 来说没有任何意义。当您打开演示文稿时,您必须设置与它的连接,以便 Excel 与其关联。我建议使用下面的代码。此外,我还使用了“后期绑定”,因此您无需从 Excel 添加对 MS Powerpoint 的任何引用。

LOGIC:

  • 将嵌入的 PPT 保存到临时文件夹
  • 在 Excel 中打开文件,然后进行更改

久经考验

Private Declare Function GetTempPath Lib "kernel32" _
Alias "GetTempPathA" (ByVal nBufferLength As Long, _
ByVal lpBuffer As String) As Long

Dim ppApp As Object, ppPreso As Object, ppPresTemp As Object

Sub swap()
    Dim sFindMe As String, sSwapme As String, FlName As String
    Dim objOLE As OLEObject
    Dim sh As Shape

    '~~> Decide on a temporary file name which will be saved in the
    '~~> users temporary folder. You might want to change the extention 
    '~~> from pptx to ppt if you are using earlier versions of MS Office
    FlName = GetTempDirectory & "\Temp.pptx"

    Set sh = Sheets("Sheet1").Shapes("Object 1")

    sh.OLEFormat.Activate

    Set objOLE = sh.OLEFormat.Object

    Set ppPresTemp = objOLE.Object

    '~~> Save the file to the relevant temp folder
    ppPresTemp.SaveAs Filename:=FlName

    '~~> Close the temp presentation that opened
    ppPresTemp.Close

    '~~> Establish an Powerpoint application object
    On Error Resume Next
    Set ppApp = GetObject(, "PowerPoint.Application")

    If Err.Number <> 0 Then
        Set ppApp = CreateObject("PowerPoint.Application")
    End If
    Err.Clear
    On Error GoTo 0

    ppApp.Visible = True

    Set ppPreso = ppApp.Presentations.Open(Filename:=FlName)

    sFindMe = "Name To Find"
    sSwapme = "New Name"

    changeme sFindMe, sSwapme


    '~~> In the end Clean Up (Delete the temp file saved in the temp directory)
    'Kill FlName
End Sub

Sub changeme(sFindMe As String, sSwapme As String)
    Dim osld As Object, oshp As Object
    Dim otemp As TextRange, otext As TextRange
    Dim Inewstart As Integer

    For Each osld In ppPreso.Slides
        For Each oshp In osld.Shapes
            If oshp.HasTextFrame Then
                If oshp.TextFrame.HasText Then
                    Set otext = oshp.TextFrame.TextRange

                    Set otemp = otext.Replace(sFindMe, sSwapme, , _
                    msoFalse, msoFalse)

                    Do While Not otemp Is Nothing
                        Inewstart = otemp.Start + otemp.Length
                        Set otemp = otext.Replace(sFindMe, sSwapme, _
                        Inewstart, msoFalse, msoFalse)
                    Loop
                End If
            End If
        Next oshp
    Next osld
End Sub

'~~> Function to get the user's temp directory
Function GetTempDirectory() As String
   Dim buffer As String
   Dim bufferLen As Long
   buffer = Space$(256)
   bufferLen = GetTempPath(Len(buffer), buffer)
   If bufferLen > 0 And bufferLen < 256 Then
      buffer = Left$(buffer, bufferLen)
   End If
   If InStr(buffer, Chr$(0)) <> 0 Then
      GetTempDirectory = Left$(buffer, InStr(buffer, Chr$(0)) - 1)
   Else
      GetTempDirectory = buffer
   End If
End Function

希望这可以帮助 :)

Sid

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 VBA 从 Excel 2010 查找并替换 Powerpoint 2010 中的文本 的相关文章

随机推荐

  • Javascript Array.sort 实现?

    JavaScript 使用哪种算法Array sort 功能使用 我知道它可以采用各种方式的参数和函数来执行不同类型的排序 我只是对普通排序使用哪种算法感兴趣 我刚刚浏览了 WebKit Chrome Safari source 根据数组的
  • Java:空间对编译有影响吗?

    我正在制作一个程序 有点像 Piglatin 其中我无意中错过了语句中的一个变量 String a R a 其实应该是String a R text a 编译器产生了一个错误 但是 当我做到了 String a R a 程序编译完成 我想知
  • 需要在导航抽屉内显示可扩展列表视图

    I am an Android Application Developer I have started working on React Native I am unable to find a way to show expandabl
  • ASP.NET MVC 5 身份 userManager.IsInRole

    以下代码不起作用 我无法解释为什么 我的用户管理器造成了很大的困扰 因为它创建用户和角色很好 但是当我运行此代码时 userManager IsInRole 总是返回 false 所以第二个当我运行我的种子时 我遇到了错误 因为它试图创建记
  • Zend Framework 2 库路径

    当我试图尝试 ZF2 时 我偶然发现了我的第一个问题 在模块上说我想使用 Shanty Mongo 连接到 MongoDb 的外部库 因此 我复制了库上的整个 Shanty 目录并创建了一个新的 Model 类 namespace Dumm
  • AsyncTask不能在android线程中工作

    我使用 AsyncTask 来更改 TextView 的文本 如下所示 private class LongOperation extends AsyncTask
  • Twisted:重新连接ClientFactory连接到不同的服务器

    我有一个扭曲的 ReconnectingClientFactory 我可以通过该工厂成功连接到给定的 ip 和端口 而且效果很好 reactor connectTCP ip 端口 myHandsomeReconnectingClientFa
  • 如何找到与我的代码兼容的所有以前版本的 python

    我在 python 2 7 3 中创建了一个中型项目 包含大约 100 个模块 我希望找出我的代码与哪些以前版本的 python 例如 2 6 x 2 7 x 兼容 在公共领域发布我的项目之前 找到它的最简单方法是什么 我知道的解决方案 安
  • 使用索引作为键初始化对象数组[重复]

    这个问题在这里已经有答案了 我试图找出如何初始化一个对象数组 其中每个对象都以索引 i 作为其键 以 0 作为其值 下面的代码没有按预期工作 但我不明白为什么 我还是 Javascript 的初学者 在其他地方找不到答案 var n 10
  • 带有 Dagger Hilt 的 Android 动态功能模块

    我已经构建了一个动态功能模块示例 其中包含基于格子应用程序的片段 子组件和依赖组件 如果您想查看here是链接 现在 我正在尝试使用将其转换为 Dagger Hilt安卓官方文档 在核心模块中 即库模块 应用程序模块和动态功能模块依赖于 S
  • Kotlin 无法在 Android Studio 上运行

    所有 kotlin 文件都无法在我的 Android Studio 上显示 即使直接将java文件转换为koltin 也可以对其进行编辑 但它不会出现在项目文件树上 IDE 还表明它是反编译的 class 文件 我无法创建 Kotlin 文
  • 如何过滤除特定白名单之外的所有 HTML 标签?

    这是针对 NET 的 设置了 IgnoreCase 但未设置 MultiLine 通常我在正则表达式方面表现不错 也许我的咖啡因不足 用户可以输入 HTML 编码的实体 u i b h3 h4 br a img 允许自动关闭 和 无论有或没
  • 无法使用点布局(graphviz 作为库)

    我使用 graphviz v2 28 0 作为 C 应用程序中的库 并且我想使用点布局渲染图形 一切正常 直到我打电话给gvLayout context graph 点 输出以下错误的函数 Error Layout type dot not
  • Pygame 三角函数:跟随斜边?

    我的方法里有一个方法Enemy类称为huntPlayer 它需要一个玩家对象p 这里是 def huntPlayer self p if self dist2p lt 200 self hunting True if p x gt self
  • 将“排名”列添加到数据框中

    我有一个数据框 其中包含不同年份的不同项目的数量 df lt data frame item rep c a b c 3 year rep c 2010 2011 2012 each 3 count c 1 4 6 3 8 3 5 7 9
  • 使用递归二分算法检查字符是否在字符串中

    我目前正在 edx 上学习编程课程 我的说明如下 使用二分搜索的思想 编写一个递归算法 检查字符串中是否包含字符 只要字符串按字母顺序排列即可 我的代码 python 2 7 在这里 def isitIn char aStr m aStr
  • 从其他程序集实例化 ResourceDictionary xaml

    我在包含颜色和画笔的 WPF 类库中定义了一个资源字典 称为 Brush Resources xaml
  • 事务标记为仅回滚:如何查找原因

    我在 Transactional 方法中提交事务时遇到问题 methodA methodB Transactional methodB em persist em flush log OK 当我从 method 调用 method 时 该方
  • POST 请求上的 GAE Soft 私有内存限制错误

    我正在开发一个使用 Google 应用程序引擎付费服务的应用程序 在应用程序中 我正在解析一个大型 xml 文件并尝试将数据提取到数据存储区 但是在执行此任务时 GAE 向我抛出如下错误 我还尝试通过将前端实例类从 F1 增加到 F2 来更
  • 使用 VBA 从 Excel 2010 查找并替换 Powerpoint 2010 中的文本

    我成功地在 powerpoint odule 中使用了这段代码 但是当我将其移动到我的 excel 模块中时 它给我带来了几个问题 我在 Excel 的第 1 页上嵌入了 Powerpoint 应用程序 目标是从 Excel 生成 Powe