Run PowerPoint sub with R - vba

I need to run a PowerPoint sub from R via:
shell(shQuote(normalizePath("C:/.../VBA_Script.vbs"))).
The script VBA_Script should trigger a sub called request_bank, which should open amsgboxwith the value of the variablebank(=J. P. Morgan`).
I get the error:
Application.Run: Invalid request. Sub or function not defined, Code: 80048240, MS PowerPoint 2013.
I just tried all the different Run.-Paths mentioned in this thread Run PowerPoint Sub from Excel. I still get the error. I wonder why the same code is working if I run the same Sub in Excel or if I add the rows:
Dim PSlide
Set PSlide = PPres.Slides(1).Duplicate
But that's no clean solution for me. There must be a better way.
VBS-Script:
Option Explicit
CallPMacro
Sub CallPMacro()
Dim PApp
Dim PPres
'Dim PSlide
Set PApp = CreateObject("PowerPoint.Application")
Set PPres = PApp.Presentations.Open("C:\...\test.pptm", 0, True)
'Set PSlide = PPres.Slides(1).Duplicate
PApp.Visible = True
PApp.Run "request_bank"
PApp.Quit
Set PPres = Nothing
Set PApp = Nothing
End Sub
VBA-Code from the Sub request_bank in the test.pptm:
Sub request_bank()
Dim bank As String
bank = "J.P. Morgan"
MsgBox ("bank: " & bank)
End Sub
Any idea how to fix it?

Related

Unable to suppress Excel Display Alerts

I am trying to suppress the message which pops up on deletion of sheet in the embedded excel of a PPT chart, using this code snippet:
POP-UP MESSAGE
CODE-SNIPPET:
Sub MainSub()
' Some code here
' reference to excel has been added
If SelShape.HasChart = True Then
Dim SelChart As PowerPoint.Chart
Set SelChart = SelShape.Chart
Dim Selchtdat As PowerPoint.ChartData
Set Selchtdat = SelChart.ChartData
On Error Resume Next
Selchtdat.Activate
On Error GoTo 0
Dim EmbdExcel As Excel.Workbook
Set EmbdExcel = Selchtdat.Workbook
Call FilterRequiredData (EmbdExcel, "Data")
End If
' Some code here
End Sub
'-----------------------------------------------------------------------------------------------------
Private Sub FilterRequiredData(ChrtWbk As Excel.Workbook, stname As String)
ChrtWbk .Activate
On Error Resume Next
Excel.Application.DisplayAlerts = False
ChrtWbk.Sheets("TempSheet").Delete
Excel.Application.DisplayAlerts = True
On Error GoTo 0
ChrtWbk.Sheets.Add(After:=ChrtWbk.Sheets(ChrtWbk.Sheets.count)).Name = "TempSheet"
'Some other code
End Sub
This code works perfectly in the native Excel VBA environment, but when invoked in PPT VBA Environment, it doesn't suppress the message. Can anybody tell me why this doesn't work ? Are there any other ways to tackle this ?

Unlock with macro when read-only in PowerPoint

I want to remove read-only using a PowerPoint macro.
I'm writing a macro that fires when a file is opened.
In that macro, there is a process to delete a specific shape.
The PowerPoint file has a write lock.
Given the above assumptions, when you open the file read-only
The macro will be executed, but an error will occur because it is read-only and the shape cannot be deleted.
So I unlock the read-only lock when the macro is executed
When I'm done deleting a particular shape, I want to lock it again for read-only.
Is there such a way?
I know the write lock password.
Public Sub Ribbon_onLoad(ribbon As IRibbonUI)
Const EXP_DATE As Date = "2021/09/30"
Dim NOW_DATE As Date: NOW_DATE = Format(Date, "yyyy/mm/dd")
Dim pp As PowerPoint.Presentation: Set pp = ActivePresentation
If NOW_DATE <= EXP_DATE Then
MsgBox "OK!"
Call DeleteShapesWithName("expShape")
Else
MsgBox "No!Exp!:" + Format(EXP_DATE, "yyyy/mm/dd")
'ActivePresentation.Close
End If
End Sub
Sub DeleteShapesWithName(ByVal targetName As String)
Dim sld As Slide
Dim shp As Shape
Dim i As Long
i = 1
For Each sld In Application.ActivePresentation.Slides
Do Until i > sld.Shapes.Count
If sld.Shapes(i).Name = targetName Then
sld.Shapes(i).Delete
Else
i = i + 1
End If
Loop
Next
End Sub
------add
I added the modified source after receiving the reply.
Public Sub Ribbon_onLoad(ribbon As IRibbonUI)
Call UnlockPresentation
End Sub
Sub UnlockPresentation()
Dim oPVW As ProtectedViewWindow
Dim oPres As Presentation
Set oPVW = ProtectedViewWindows.Open("C:\test\example_exp.pptm")
oPVW.Edit ModifyPassword:="test"
Call DeleteShapesWithName("expShape")
'Do stuff here
End Sub
Sub DeleteShapesWithName(ByVal targetName As String)
Dim sld As Slide
Dim shp As Shape
Dim i As Long
i = 1
For Each sld In Application.ActivePresentation.Slides
Do Until i > sld.Shapes.Count
If sld.Shapes(i).Name = targetName Then
sld.Shapes(i).Delete
Else
i = i + 1
End If
Loop
Next
End Sub
Here's code to open a read-only presentation so you can modify it. No need to reset the password, just replace "Test" with the actual password. Where the Do stuff here comment is, you can use the ActivePresentation keyword to modify the file:
Sub UnlockPresentation()
Dim oPVW As ProtectedViewWindow
Dim oPres As Presentation
Set oPVW = ProtectedViewWindows.Open("C:\HasModPW.pptx")
oPVW.Edit ModifyPassword:="ExistingModificationPassword"
'Do stuff here
End Sub
Please note: Microsoft's help page on ProtectViewWindow.Edit is wrong. The page currently states this method changes the password, but it actually provides the PW to make editing possible,

Word Window Type mismatch

I am trying to capture pages in word as image and paste in Excel via VBA, below is the complete code. but got a Type Mismatch error as the comment in below. How to fix the error?
Function openFile() As String
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Filters.Add "Word Files", "*.doc*", 1
.Show
openFile = .SelectedItems.Item(1)
End With
End Function
Function readWord(ByVal path As String)
Debug.Print "Read word", path
Set objWordApp = CreateObject("Word.Application")
Set objWordDoc = objWordApp.Documents.Open(path)
objWordApp.Visible = False
Dim objPage As Page
Dim objPane As Pane
Dim objWindow As Window
Debug.Print objWordDoc.Windows.Count
Debug.Print TypeName(objWordDoc.Windows.Item(1))
For Each objWindow In objWordDoc.Windows 'Got Type mismatch Here
For Each objPane In objWindow.Panes
For Each objPage In objPane.Pages
Debug.Print "Page"
Next objPage
Next objPane
Next objWindow
End Function
Sub processWord()
Dim p As String
p = openFile()
readWord (p)
End Sub
The error is caused because your code contains a confused mess of objects.
You are attempting to use late binding for Word and yet you declare:
Dim objPage As Page
Dim objPane As Pane
Dim objWindow As Window
As you appear to be writing your code in Excel this results in these objects being:
Dim objPage As Excel.Page
Dim objPane As Excel.Pane
Dim objWindow As Excel.Window
This causes the type mismatch error.
I suggest that you avoid using late binding until you have your code fully working. Then you can change all the object declarations to As Object, if you really feel it is necessary.
Incidentally, if you are thinking that you can use the SaveAsPNG method listed in the documentation to get images of the documents pages, you can't - it doesn't exist.

How can I refer to AutoCAD block

I having autocad project where is 1 dynamic block which I'm trying to change from excel.
Here is vba script which I'm using to change block:
Dim dybprop As Variant, i As Integer
Dim bobj As AcadEntity
For Each bobj In ACADApp.ModelSpace
If bobj.ObjectName = "AcDbBlockReference" Then
If bobj.IsDynamicBlock Then
If bobj.EffectiveName = "AdjBlock" Then
dybprop = bobj.GetDynamicBlockProperties
For i = LBound(dybprop) To UBound(dybprop)
If dybprop(i).PropertyName = "Distance1" Then
dybprop(i).Value = 50.75
Acad.Application.Update
End If
Next i
End If
End If
End If
Next
End With
When I'm running it in AutoCAD VBA it works excellent. Than I'm creating Excel VBA project and copying this code. Before running it I creating connection to existing AutoCad project like this:
On Error Resume Next
Dim ACADApp As AcadApplication
Dim a As Object
Set a = GetObject(, "AutoCAD.Application")
If a Is Nothing Then
Set a = CreateObject("AutoCAD.Application")
If a Is Nothing Then
MsgBox "AutoCAD must be running before performing this action.", vbCritical
Exit Sub
End If
End If
Set ACADApp = a
Set ACADApp.ActiveDocument = ACADApp.Documents.Open("c:\KIRILL\Programming\Drawing1_VBATest.dwg")
When I'm running it from Excel VBA - AutoCAD project appears but nothing change. Honestly I don't have any idea why in Excel VBA it doesn't works while in AutoCAD it work. May be somebody had this problem before? Thanks in advance.
P.S. Full Excel VBA code:
Sub Button9_Click()
On Error Resume Next
Dim ACADApp As AcadApplication
Dim a As Object
Set a = GetObject(, "AutoCAD.Application")
If a Is Nothing Then
Set a = CreateObject("AutoCAD.Application")
If a Is Nothing Then
MsgBox "AutoCAD must be running before performing this action.", vbCritical
Exit Sub
End If
End If
Set ACADApp = a
Set ACADApp.ActiveDocument = ACADApp.Documents.Open("c:\KIRILL\Programming\Drawing1_VBATest.dwg")
Dim dybprop As Variant, i As Integer
Dim bobj As AcadEntity
For Each bobj In ACADApp.ModelSpace
If bobj.ObjectName = "AcDbBlockReference" Then
If bobj.IsDynamicBlock Then
If bobj.EffectiveName = "AdjBlock" Then
dybprop = bobj.GetDynamicBlockProperties
For i = LBound(dybprop) To UBound(dybprop)
If dybprop(i).PropertyName = "Distance1" Then
dybprop(i).Value = 50.75
Acad.Application.Update
End If
Next i
End If
End If
End If
Next
End Sub
Have you tried adding the reference library?
You can go to:
Tools->References
And add:
[AutoCAD 20xx Type Library]

VBA run time error 438

I am trying to write a code to open a PPTX file from excel VBA and update links in ppt.
below is the code that i have got but while the code is trying to update links i am getting the
Run time error 438 Object does not support this property or method
Sub kunal()
Dim PPObj As Object
Set PPObj = CreateObject("PowerPoint.application")
With PPObj
.Presentations.Add
.Presentations.Open Filename:="Y:\Desktop\Month End\One_Shot\Template AVP Report Package\ABD-OME SDeeson.pptx"
.Visible = True
.UpdateLinks
.Presentation.Save
.Quit
Set PPObj = Nothing
End With
End Sub
438 means that you are trying to access a property of a method or object, that does not exist. Thus, you need a presentation object and not application object to update the links.
Try like this:
Option Explicit
Sub kunal()
Dim PPObj As Object
Dim pptPresentation As Object
Set PPObj = CreateObject("PowerPoint.application")
Set pptPresentation = PPObj.presentations.Open("C:\test.pptx")
With PPObj
.presentations.Add
.Visible = True
pptPresentation.UpdateLinks
pptPresentation.Save
pptPresentation.Close
.Quit
'Set PPObj = Nothing - No need for this
End With
End Sub
Application Object MSDN
Presentation Object MSDN
First, not sure why you need .Presentations.Add if at the following line you are opening an existing Presentation.
Second, the line of .UpdateLinks is a property of the Presentation, not the PowerPoint.Application.
Code
Option Explicit
Sub kunal()
Dim ppApp As Object
Dim ppPres As Object
Set ppApp = CreateObject("PowerPoint.application")
With ppApp
.Visible = True
' .Presentations.Add ' <-- not sure why you need to open a new Presentation ?
Set ppPres = .Presentations.Open(Filename:="Y:\Desktop\Month End\One_Shot\Template AVP Report Package\ABD-OME SDeeson.pptx")
ppPres.UpdateLinks
ppPres.Save
ppPres.Close
.Quit
End With
Set ppPres = Nothing
Set ppApp = Nothing
End Sub