2012-03-21 11 views
5

Utilicé con éxito este código dentro de un módulo de PowerPoint, pero cuando lo muevo dentro de mi módulo Excel me da varios problemas. Integré la aplicación Powerpoint en la hoja 1 de Excel. El objetivo es generar el punto de poder de excel y reemplazar el nombre de la compañía cada vez que aparece en una diapositiva de PowerPoint con el nuevo nombre de la empresa de un rango de Excel. consigo el error 429 no puede crear el componente ActiveX objeto en "Para Cada osld En ActivePresentation.Slides. Es mi presentación en Powerpoint no está activo? Cualquier ayuda sería muy apreciada. El uso de Excel/Powerpoint 2010.Buscar y reemplazar texto en Powerpoint 2010 de Excel 2010 con VBA

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 
+0

Su ppt no está activo ** en Excel **: debe incluir esa referencia 'ppApp' cuando se refiera a ActivePresentation: es decir. 'ppApp.ActivePresentation.Slides' –

Respuesta

8

ActivePresentation es un PowerPoint Objeto. No significa nada para Excel. Cuando abre una presentación, debe establecer una conexión con ella para que Excel se relacione con ella. Le sugiero que use el siguiente código. También he usado Enlace tardío para que no lo haga. es necesario agregar cualquier referencia a MS Powerpoint desde Excel.

LOGIC:

  • Guardar el PPT incrustado a una carpeta temporal
  • Abrir el archivo en Excel y luego hacer los cambios

probado y comprobado

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 

Espero que esto ayude :)

Sid

+0

Sid muchas gracias, ¡este código no solo funcionó, sino que también fue increíblemente rápido! ¡Tu eres el hombre! – user1284325

+0

¡Aceptado! Gracias de nuevo – user1284325

+0

Lo siento, todavía no lo intenté, user1284325 no soy yo ... – LaBracca

Cuestiones relacionadas