VBA Pasting from excel into Powerpoint - excel

Sub jede_Grafik_nach_PowerPoint()
'Extras - Verweise: Microsoft PowerPoint x.x Object Library
Dim Grafik As Shape
Dim PP As Object
Set PP = CreateObject("Powerpoint.Application")
Dim PP_Datei As PowerPoint.Presentation
Dim PP_Folie As PowerPoint.Slide
Dim ws As Worksheet
Dim wb As Workbook
Set wb = ThisWorkbook
Set PP_Datei = PP.Presentations.Open("C:\Users\akaygun\Desktop\test.pptm")
PP.Visible = msoTrue
'Set PP_Datei = PP.ActivePresentation wenn akt. Präsi sein soll
For Each ws In wb.Sheets
If Left(ws.Name, 3) = "MLK" Then
'neue Folie einfügen
PP_Datei.Slides(3).Copy
PP_Datei.Slides.Paste
Set PP_Folie = PP_Datei.Slides(PP_Datei.Slides.Count)
'copypaste
ws.Shapes("Stunden").Copy
PP_Folie.Shapes.Paste
PP_Folie.Shapes("Stunden").Top = 315.1991
PP_Folie.Shapes("Stunden").Left = 22.17449
ws.Shapes("Tage").Copy
PP_Folie.Shapes.Paste
PP_Folie.Shapes("Tage").Top = 10.16945
PP_Folie.Shapes("Tage").Left = -2.806772
End If
Next ws
End Sub
Dear Community,
I am trying to paste diagramms from excel to Powerpoint via VBA automatically.
When running this Sub it always says : "Remote server Computer doesnt exist"
'462'
I already tried to set a New Presentation instead of an Object but it did not help.

Related

Creating PPT from two different excel file in VBA

I am trying to open a excel file, take the ranges as picture and create a ppt our of it. I am not sure what is going wrong in the code below. Can someone help please? The code works well for all other slides coming from other excel file. The moment I opena excel file and switch , it is throwing "subscript out of range error"
Sub GlobalBankPPT()
Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim ppSlide As PowerPoint.Slide
Dim ppShape As PowerPoint.Shape
Dim ppPic As Variant
Dim j As Integer
Dim wkb As Workbook
Dim sh As Worksheet
Dim WS_Count As Integer, i As Integer
Dim mypp As New ExcelToPPt.cPPt
mypp.InitFromTemplate MyTemplateppt:=mytemp, MyOutputppt:=""
Set ppApp = GetObject(, "PowerPoint.Application")
ppApp.Visible = msoTrue
Set ppPres = ppApp.ActivePresentation
Set wkb = Workbooks.Open(Filename:=ThisWorkbook.Worksheets("Index").Range ("FilePath"))
wkb.Activate
WS_Count = wkb.Worksheets.Count
For i = 1 To WS_Count
If Worksheets(i).Name = "Industry" Then
wkb.Activate
Set sh = ActiveSheet
Exit For
End If
Next I
'--------------------------------------------------------------------Slide7
Range(Sheet15.Range("A150"), Sheet15.Range("Q191")).Select
Selection.CopyPicture Appearance:=xlScreen, Format:=xlPicture
ppPres.Slides(7).Select
Set ppPic = ppPres.Slides(7).Shapes.Paste
ppPic.Select
ppPic.Top = 70.24
ppPic.Width = 10.5 * 100
ppPic.Height = ppPic.Width / 3.4
ppPic.Left = 50
end sub

Copy Word Paragraph to Excel Cells

I am trying to copy Word paragraphs to Excel cells, but I am hung up on
Runtime error 9: Subscript out of range.
I have searched. Everything I read says it cannot find the file, but the file is in the same folder, and the name is not mis-spelled, and the extension is correct. So, I am stumped. The original code comes from here: How to copy a formatted paragraph from Word 2013 to Excel?.
Private Sub Load_Schedule()
Dim ParaCount As Integer
Dim wDoc As Word.Document
Dim wb As Workbook
Dim ws As Worksheet
Set wDoc = ActiveDocument
Set wb = Workbooks("new.xlsm")
Set ws = wb.Sheets("Sheet1")
ws.Activate
ws.Columns(1).AutoFit
For ParaCount = 1 To wDoc.Paragraphs.Count
wDoc.Paragraphs(ParaCount).Range.FormattedText.Copy
Sheets(ws).Cells(ParaCount, 1).PasteSpecial
Paste:=xlPasteFormats
Next ParaCount
End Sub
The error comes on this line: Set wb = Workbooks("new.xlsm")
As you work with both applications, you should use full declarations like Word.Document and Excel.Workbook (if you already referenced the appropriate libraries).
An already opened Excel file can be referenced without path.
The Paste:= ... parameter belongs to the previous code line, so you have to add a blank + undersore at the end of the previous line or put them together into one line.
Please reference your worksheet's cell by ws.Cells ... and not by Sheets(ws), as your "ws" already is a worksheet object and not a string.
The further answer depends, if you run your code from Word-VBA or from Excel-VBA.
Word VBA
If you want to reference an Excel file from Word-VBA, you need the Excel.Application object additionally.
If Excel is already started, you can use the existing application object - otherwise you create one and make it visible.
Same with your Excel file: If it's already open, you use it - if not, you open it.
Private Sub LoadSchedule()
Dim ParaCount As Integer
Dim wDoc As Word.Document
Dim objExcel As Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
On Error Resume Next
Set objExcel = GetObject(, "Excel.Application")
On Error GoTo 0
If objExcel Is Nothing Then
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
End If
On Error Resume Next
Set wb = objExcel.Workbooks("new.xlsm")
On Error GoTo 0
If wb Is Nothing Then
Set wb = objExcel.Workbooks.Open(objExcel.DefaultFilePath & "\new.xlsm")
' or ThisDocument.Path or whatever path
End If
Set wDoc = ActiveDocument
Set ws = wb.Sheets("Sheet1")
For ParaCount = 1 To wDoc.Paragraphs.Count
wDoc.Paragraphs(ParaCount).Range.FormattedText.Copy
ws.Cells(ParaCount, 1).PasteSpecial Paste:=xlPasteFormats
Next ParaCount
ws.Columns(1).AutoFit
'ws.Activate
End Sub
Excel VBA
In Excel you can try to reference an already opened Word file directly as ActiveDocument without getting the Word.Application additionally.
Private Sub LoadSchedule()
Dim ParaCount As Integer
Dim wDoc As Word.Document
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
On Error Resume Next
Set wb = Workbooks("new.xlsm")
On Error GoTo 0
If wb Is Nothing Then
Set wb = Workbooks.Open(Application.DefaultFilePath & "\new.xlsm")
End If
Set wDoc = ActiveDocument
Set ws = wb.Sheets("Sheet1")
For ParaCount = 1 To wDoc.Paragraphs.Count
wDoc.Paragraphs(ParaCount).Range.FormattedText.Copy
ws.Cells(ParaCount, 1).PasteSpecial Paste:=xlPasteFormats
Next ParaCount
ws.Columns(1).AutoFit
'ws.Activate
End Sub
You need to specify the full path to the excel file - you say it's the same as the word document so this will work:
Sub GetXLFileInWord()
Dim xl As Excel.Application
Set xl = New Excel.Application
Dim wb As Excel.Workbook
Set wb = xl.Documents.Open(ThisDocument.Path & "\new.xlsm")

Open a ppt from Sharepoint on command button click - VBA

I wanted to open ppt form SharePoint using the VBA, using the command button
if any one knows please help me.
Thanks...
I have similar code...below.. but its giving me the error
Dim PPT As PowerPoint.Application
Dim fileName As Variant
Dim WS1 As Worksheet
Dim rng As Range
Set WS1 = ThisWorkbook.Worksheets("Sheet4")
Set rng = WS1.Range("AA4")
Set PPT = New PowerPoint.Application
fileName = rng
PPT.Visible = True
'PPT.Presentations.Open fileName:="F:\Reports\" & Business_Plan.Value & ".ppt"
PPT.Presentations.Open fileName("AA4")
Assuming the value of "A2" in Sheet 1 is the entire Path+File Name (F:\Reports\FileName.ppt)
Sub openPowerP()
Dim PPT As PowerPoint.Application
Dim fileName As String
Dim WS1 As Worksheet
Set WS1 = ThisWorkbook.Sheets("Sheet1")
fileName = WS1.Range("A2").Value
Set PPT = New PowerPoint.Application
PPT.Presentations.Open fileName
End Sub
The reference to powerpoint is needed of course.

How to update powerpoint table from excel workbook

I'm trying to write a code to run from excel, and then open an existing excel and powerpoint file from the c drive, and use the data in the worksheet named "Oct18" to update the FIRST table in Slide 2 of the powerpoint.
The following is the code I wrote, but somehow it doesn't work.
May I know if anyone knows how to go about this please ?
Sub WriteText_toPPT_Table()
'Add a reference to Microsoft Powerpoint 12.0 object library
Dim ppApp As PowerPoint.Application
Dim ppPres As PowerPoint.Presentation
Dim ppShp As PowerPoint.Shape
Dim ppSld As PowerPoint.slide
Dim xlworkbook As Workbook
Dim firstsheet As String
Dim fileDir As String
Dim excelFile As String
firstsheet = "Oct18"
'fileDir = "c:\masterpresentation.pptx"
'excelFile = "c:\masterexcel.pptx
'1) Open powerpoint application
Set ppApp = CreateObject("PowerPoint.Application")
ppApp.Visible = msoTrue
Set xlworkbook = Excel.Application.Workbooks.Open(Filename:=excelFile)
'2) opening an existing presentation
Set ppPres = ppApp.Presentations.Open(Filename:=fileDir)
Set exceldir = Excel.Application.Workbooks.Open(Filename:=excelFile)
ppPres.Slides(2).Shapes(1).Table.Cell(1, 1).Shape.TextFrame.TextRange.Text = xlworkbook.Sheets(firstsheet).Cells(4, 12)
End Sub

OLEFormat (unknown member): Invalid Request in PowerPoint

I am using this particular code by Belisarius:
Sub a()
Dim oSl As PowerPoint.Slide
Dim oSh As PowerPoint.Shape
Set oSl = ActivePresentation.Slides(1)
Set oSh = oSl.Shapes(1)
With oSh.OLEFormat.Object.WorkSheets(1)
.Range("A1").Value = .Range("A1").Value + 1
.Range("A2").Value = .Range("A2").Value - 1
End With
Set oSl = Nothing
Set oSh = Nothing
End Sub
I've embedded a line chart (with the ability to change values in excel) using insert menu in PowerPoint 2010. I'm getting an error that says OLEFormat (unknown member): Invalid Request. I know this has worked for someone out there but apparently what I've inserted is not an object. Why am I getting this error?
Accessing the underlying Excel worksheet is a little tricky - try this approach instead
Sub Test()
Dim myChart As Chart
Dim myChartData As ChartData
Dim myWorkBook As Object
Dim myWorkSheet As Object
Set myChart = ActivePresentation.Slides(1).Shapes(1).Chart
Set myChartData = myChart.ChartData
myChartData.Activate
Set myWorkBook = myChartData.Workbook
Set myWorkSheet = myWorkBook.Worksheets(1)
With myWorkSheet
.Range("A1").Value = .Range("A1").Value + 1
.Range("A2").Value = .Range("A2").Value - 1
End With
myWorkBook.Close
Set myWorkBook = Nothing
End Sub

Resources