Soy bastante nuevo en VBA y estoy tratando de hacer una presentación powepoint de un libro de trabajo. Tengo una plantilla, y la idea es llenarla con charts y tablas.
Este es mi código:
Sub ChartToPresentation() ' Set a VBE reference to Microsoft PowerPoint Object Library Dim PPApp As PowerPoint.Application Dim PPPres As PowerPoint.Presentation Dim PPSlide As PowerPoint.Slide ' Reference existing instance of PowerPoint Set PPApp = GetObject(, "Powerpoint.Application") ' Reference active presentation Set PPPres = PPApp.ActivePresentation PPApp.ActiveWindow.ViewType = ppViewSlide ' 6 - Convocatoria - Presentismo Set PPSlide = PPPres.Slides(6) ThisWorkbook.Worksheets("FyV").ChartObjects(15).Select 'Hoja8.ChartObjects(15).Select ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture PPSlide.Shapes.Paste PPApp.ActiveWindow.Selection.ShapeRange.Left = 10 PPApp.ActiveWindow.Selection.ShapeRange.Top = 20 'PPSlide.ShapeRange.Width = 80 'PPSlide.ShapeRange.Height = 80 End Sub
Tengo un bloque por gráfico, a veces más de 1 gráfico por diapositiva. Pero estoy teniendo varios problemas.
Cuando pregunto
ThisWorkbook.Worksheets("FyV").ChartObjects(15).Select
Obtengo el cuadro 24 de esa hoja de trabajo. Cuando solicito los cuadros 3, 12 y 13 obtengo el cuadro 5.
Cuando comente
'PPSlide.ShapeRange.Width = 80 'PPSlide.ShapeRange.Height = 80
Obtuve el siguiente error:
Error de compilation: método o miembro de datos no encontrado
A veces la línea:
ThisWorkbook.Worksheets("FyV").ChartObjects(XX).Select
Obtiene el siguiente error:
Error "1004" en time de ejecución: error definido por la aplicación o definido por el object
Pero XX existe, y está en "FyV"
Yo he tratado
ThisWorkbook.Worksheets("FyV").ChartObjects(15).Select
Y
'Hoja8.ChartObjects(15).Select
Para resolver 1 y 3, pero no cambió nada.
Gracias de antemano, Bauti.
Encontré una solución (guiada por las respuestas, ¡Gracias!) No es tan elegante, pero funciona.
Sub ChartToPresentation() ' Set a VBE reference to Microsoft PowerPoint Object Library Dim PPApp As PowerPoint.Application Dim PPPres As PowerPoint.Presentation Dim PPSlide As PowerPoint.Slide ' Reference existing instance of PowerPoint Set PPApp = GetObject(, "Powerpoint.Application") ' Reference active presentation Set PPPres = PPApp.ActivePresentation PPApp.ActiveWindow.ViewType = ppViewSlide Worksheets("FyV").Select ' 6 - Convocatoria - Presentismo Set PPSlide = PPPres.Slides(6) ThisWorkbook.Worksheets("FyV").ChartObjects("Chart 15").Select 'Hoja8.ChartObjects(15).Select ActiveChart.CopyPicture Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture PPSlide.Shapes.Paste PPApp.ActiveWindow.Selection.ShapeRange.Left = 40 PPApp.ActiveWindow.Selection.ShapeRange.Top = 200 PPApp.ActiveWindow.Selection.ShapeRange.Width = 160 PPApp.ActiveWindow.Selection.ShapeRange.Height = 160 End Sub
Como hubo pocos cambios en la hoja de trabajo, no fue tan difícil agregar la línea de la hoja de trabajo cada vez que hubo un cambio.
Además, al preguntar en el foro de excel supremo recibí esta respuesta, que parece funcionar:
Sub ChartToPresentation() ' Set a VBE reference to Microsoft PowerPoint Object Library Dim PPApp As PowerPoint.Application Dim PPPres As PowerPoint.Presentation Dim PPSlide As PowerPoint.Slide Dim oShape As PowerPoint.Shape ' Reference existing instance of PowerPoint Set PPApp = GetObject(, "Powerpoint.Application") ' Reference active presentation Set PPPres = PPApp.ActivePresentation PPApp.ActiveWindow.ViewType = ppViewSlide ' 6 - Convocatoria - Presentismo Set PPSlide = PPPres.Slides(6) ThisWorkbook.Worksheets("FyV").ChartObjects("Chart 1").CopyPicture Appearance:=xlScreen, Format:=xlPicture PPSlide.Shapes.Paste With PPSlide Set oShape = .Shapes(.Shapes.Count) End With 'oShape.LockAspectRatio = msoFalse oShape.Left = 10 oShape.Top = 20 oShape.Width = 80 oShape.Height = 80 End Sub
Gracias por las respuestas, Bauti.
ChartObjects(15)
representa el " decimoquinto " gráfico en la hoja, 15 no se corresponde necesariamente con el nombre del gráfico ni con su position en la hoja, pero está relacionado con el order en que se crearon los charts.
Cuando comente
'PPSlide.ShapeRange.Width = 80' PPSlide.ShapeRange.Height = 80 Recibo el siguiente error:
Error de compilation: método o miembro de datos no encontrado
Sí, porque no puede establecer el ancho y la altura para un shaperange.
Si solo hay una forma en el range, como sería el caso de un gráfico pegado en PPT desde Excel, puede usar PPSlide.ShapeRange (1) .Height, etc.
Si necesita establecer el tamaño de más de una forma en un range, deberá iterar a través de la colección ShapeRange:
For x = 1 to PPSlide.ShapeRange.Count With PPSlide.ShapeRange(x) ' Do stuff here End With Next
Por cierto, generalmente quiere evitar seleccionar cualquier cosa, ya sea en PPT o Excel. Obtenga una reference de object al gráfico en lugar de seleccionarlo. De hecho, si la hoja en la que se encuentra el gráfico no está actualmente a la vista, intentarlo. Selecciónelo puede ser una de las razones de los errores que está viendo.