Ir ao conteúdo
  • Cadastre-se

Macro Excel para PPT - Quase lá! ( Código Incluso )


ffialho

Posts recomendados

Olá pessoal,

Estou trabalhando numa macro que copia gráficos do Excel para o PPT. Peguei o codigo na web, fiz pouquissimas alteracoes. A macro abaixo faz tudo isso, so estou tendo um problema na configuração dos graficos no PPT propriamente dito. Os graficos estão sendo copiados sobrepostos e fora da ordem.

Tenho dois questionamentos:

1-como eu poderia configurar a macro para salvar os graficos no tamanho e posicao dentro do PPT que gostaria?

2-Seria possivel eu especificar na macro o endereco/pasta para salvar o PPT e o nome do arquivo PPT? Algo que quando a pessoa rode a macro ela gera o PPT com os graficos e salva na pasta especificada.

Estou utilizando Excel 2010...

grato

FF

Código:

Sub Export_Excel_Charts_to_PowerPoint()

'Requires a reference to the Microsoft PowerPoint Library via the Tools - Reference menu in the VBE

Dim ppApp As PowerPoint.Application

Dim ppSlide As PowerPoint.Slide

Dim myChart As Excel.ChartObject

Dim cNum As Integer

'Look for existing instance

On Error Resume Next

Set ppApp = GetObject(, "PowerPoint.Application")

On Error GoTo 0

'Create new instance if no instance exists

If ppApp Is Nothing Then Set ppApp = New PowerPoint.Application

'Add a presentation if none exists

If ppApp.Presentations.Count = 0 Then ppApp.Presentations.Add

'Make the instance visible

ppApp.Visible = True

'Check that a slide exits, if it doesn't add 1 slide. Else use the last slide for the paste operation

If ppApp.ActivePresentation.Slides.Count = 0 Then

Set ppSlide = ppApp.ActivePresentation.Slides.Add(1, ppLayoutBlank)

Else

If AddSlidesToEnd Then

'Appends slides to end of presentation and makes last slide active

ppApp.ActivePresentation.Slides.Add ppApp.ActivePresentation.Slides.Count + 1, ppLayoutBlank

ppApp.ActiveWindow.View.GotoSlide ppApp.ActivePresentation.Slides.Count

Set ppSlide = ppApp.ActivePresentation.Slides(ppApp.ActivePresentation.Slides.Count)

Else

'Sets current slide to active slide

Set ppSlide = ppApp.ActiveWindow.View.Slide

End If

End If

'Options for Copy & Paste Ranges and Charts

cNum = 11

ActiveSheet.Activate

For Each myChart In ActiveSheet.ChartObjects

myChart.Select

ActiveChart.ChartArea.Copy

ppSlide.Shapes.PasteSpecial(DataType:=ppPasteMetafilePicture).Select

'Adjust the Chart on Powerpoint Slide

Select Case (cNum + 10) Mod 4

Case 1

ppApp.ActiveWindow.Selection.ShapeRange.Align msoAlignLefts, msoTrue

ppApp.ActiveWindow.Selection.ShapeRange.Align msoAlignTops, msoTrue

Case 2

ppApp.ActiveWindow.Selection.ShapeRange.Align msoAlignRights, msoTrue

ppApp.ActiveWindow.Selection.ShapeRange.Align msoAlignTops, msoTrue

Case 3

ppApp.ActiveWindow.Selection.ShapeRange.Align msoAlignLefts, msoTrue

ppApp.ActiveWindow.Selection.ShapeRange.Align msoAlignBottoms, msoTrue

Case 0

ppApp.ActiveWindow.Selection.ShapeRange.Align msoAlignRights, msoTrue

ppApp.ActiveWindow.Selection.ShapeRange.Align msoAlignBottoms, msoTrue

End Select

If (cNum + 10) Mod 4 = 0 Then

Set ppSlide = ppApp.ActivePresentation.Slides.Add(ppApp.ActivePresentation.Slides.Count + 1, ppLayoutBlank)

ppSlide.Select

End If

cNum = cNum + 1

Next

AppActivate ("Microsoft PowerPoint")

Set ppSlide = Nothing

Set ppApp = Nothing

End Sub

Link para o comentário
Compartilhar em outros sites

:D

Pode ajudar a quem tenha que passar por isso :)

Option Explicit

Sub Graficos_Resultados_para_PPT()

'

'Excel Application objects declaration

Dim ws As Worksheet

Dim objChartObject As ChartObject

Dim objChart As Chart

Dim objCht As Chart

Dim lngSlideKount As Long

Dim sTitle As String

'

'Powerpoint Application objects declaration

' You need to add a reference (Tools | References) to the Microsoft PowerPoint nn.nn Object Library

Dim pptApp As PowerPoint.Application

Dim pptPres As PowerPoint.Presentation

Dim pptSld As PowerPoint.Slide

'

On Error GoTo Error_Para

'

'Reference Powerpoint session

Set pptApp = CreateObject("PowerPoint.Application")

'

pptApp.Visible = msoTrue

'Open existing presentation

pptApp.Visible = True

pptApp.Presentations.Open "\\psf\Home\Desktop\Apresentacao.pptx"

Set pptPres = pptApp.ActivePresentation

pptApp.ActiveWindow.ViewType = ppViewSlide

'

lngSlideKount = 0

'ppLayoutBlank = 12

Set pptSld = pptPres.Slides.Add(lngSlideKount + 1, 12)

pptApp.ActiveWindow.View.GotoSlide pptSld.SlideIndex

' ======================================================================

' select sheet containing data to be copied to PowerPoint:

Sheets("Avaliacao Mecanica").Select

' select cells to be copied to PowerPoint:

range("A4:Q32").Select

' copy cells:

Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture

' past to PowerPoint:

pptSld.Shapes.Paste.Select

lngSlideKount = lngSlideKount + 1

' END OF CHANGED CODE

' ======================================================================

pptApp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True

pptApp.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, True

pptApp.ActiveWindow.Selection.ShapeRange.Item(1).ScaleHeight 0.5, msoCTrue, msoScaleFromMiddle

' CODE TO ENTER A NEW SLIDE

' ======================================================================

pptApp.ActivePresentation.Slides.Add pptApp.ActivePresentation.Slides.Count + 1, ppLayoutBlank

pptApp.ActiveWindow.View.GotoSlide pptApp.ActivePresentation.Slides.Count

Set pptSld = pptApp.ActiveWindow.View.Slide

' ======================================================================

' select sheet containing data to be copied to PowerPoint:

Sheets("Avaliacao").Select

' select cells to be copied to PowerPoint:

range("A39:O85").Select

' copy cells:

Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture

' past to PowerPoint:

pptSld.Shapes.Paste.Select

lngSlideKount = lngSlideKount + 1

' END OF CHANGED CODE

' ======================================================================

'

pptApp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True

pptApp.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, True

pptApp.ActiveWindow.Selection.ShapeRange.Item(1).ScaleHeight 0.5, msoCTrue, msoScaleFromMiddle

pptApp.ActiveWindow.Selection.ShapeRange.Left = 100

pptApp.ActiveWindow.Selection.ShapeRange.Top = 100

With pptPres

.SaveAs "\\psf\Home\Desktop\Resultados_Sxx.pptx"

End With

'

'Activate PowerPoint application

pptApp.ActiveWindow.ViewType = ppViewNormal

pptApp.Visible = True

pptApp.Activate

If lngSlideKount > 0 Then

If lngSlideKount = 1 Then

MsgBox "1 chart was copied to PowerPoint", vbOKOnly + vbInformation, "Information"

Else

MsgBox lngSlideKount & " charts were copied to PowerPoint", vbOKOnly + vbInformation, "Information"

End If

End If

GoTo Exit_Para

Error_Para:

MsgBox "Error " & Err.Number & vbCrLf & vbCrLf & _

Err.Description & vbCrLf & vbCrLf & _

"copying charts to PowerPoint", vbOKOnly + vbCritical, "Error"

Exit_Para:

On Error Resume Next

Set ws = Nothing

Set objChart = Nothing

Set objChartObject = Nothing

Set pptSld = Nothing

Set pptPres = Nothing

Set pptApp = Nothing

End Sub

Link para o comentário
Compartilhar em outros sites

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...