Convert .ppt, .xls, .doc to PDF using Blue Prism ?

mmanasa2525

New Member
Hi All,

I have different formats of input files which I need to pick and convert them to PDF and later extract data from that PDF to process.

Can anyone help me how we can Word/Excel/PPT to a PDF using blue prism and what are the objects we can use to extract data from PDF.
 

sivagelli

Well-Known Member
For MS Word there is an available Export PDF action available in MS Word VBO. For the other types: xls/ppt, you need to code to convert to pdf.
 
Last edited:

VJR

Well-Known Member
No, there is no code already written on the forum but you can use the ExportAsFixedFormat for the Excel file with the help of the below links. You need to create the relevant Workbook/Worksheet objects which you can find in the code stages of the other Excel VBO actions or some on the forum.
https://docs.microsoft.com/en-us/office/vba/api/excel.workbook.exportasfixedformat
https://powerspreadsheets.com/save-excel-file-pdf-vba/
https://stackoverflow.com/questions/32575318/save-excel-spreadsheet-as-pdf


Likewise there is the same method for Powerpoint files.
https://docs.microsoft.com/en-us/office/vba/api/powerpoint.presentation.exportasfixedformat
 

VJR

Well-Known Member
I happened to install the pdf add-in on my machine and used the below code to generate the pdf.

- Create a new object action inside the Ms Excel VBO and have the Start and Code stages as below. I made a duplicate copy of the Open Workbook stage and used it.
View attachment 1541067028154.png

- Start stage Input parameters:
View attachment 1541066786337.png

- Code stage Input parameters:
View attachment 1541066688041.png

- Code stage:
View attachment 1541066911511.png
Code:
Dim wb as Object = GetInstance(handle).Workbooks.Open(filename)
wb.Activate()  'can skip this line
wb.ExportAsFixedFormat (Type:=0, Filename:=FullPdfFilePath, Quality:=0) '0:=xlTypePDF, 0:=xlQualityStandard
'Refer other list of parameters in below link
'https://docs.microsoft.com/en-us/office/vba/api/excel.workbook.exportasfixedformat
wb = Nothing

- Input Excel File (Sheet1) and Process Diagram:
I have used the 'Open Workbook' action in the diagram to 'Show' the workbook, you may skip that if you don't need to display.
View attachment 1541066719252.png

- Call to the new action from the Process diagram:
View attachment 1541067120310.png

- Sample Output.pdf (opened in Chrome):
Page 1 from Sheet 1:
View attachment 1541067205313.png

Page 2 from Sheet 2:
View attachment 1541067264173.png
 

mmanasa2525

New Member
Thanks a lot. The Excel part worked for me :) I have a question on the PPT part. What is the application name I need to give in the code stage? Presentation Application ?. Can I replicate the code stages for Open, and other action stages same as excel vbo?
 

VJR

Well-Known Member
Replicating the Excel code will require a bit more coding with all the other code correctly done for creating the Handle etc similar to the one for Excel since it has to be done from scratch. Although that can be done if you are planning to create a new VBO for Powerpoint with your own set of customised actions, and will be re-usable too.

The below code saves a Powerpoint as a PDF.

Code:
'If creating a new object with just the Code stage then add Microsoft.VisualBasic 
'to the Namespace Imports section of the Initialise tab

Dim pptApp as Object
Dim pptObj as Object

pptApp = CreateObject("PowerPoint.Application")
pptApp.visible = True

pptObj = pptApp.Presentations.Open("C:\MySavedPPT.pptx")

pptObj.SaveAs("c:\PPTToPdf.pdf", 32) ' 32:=ppSaveAsPDF
'If the Export quality of the pdf is not as expected OR if you need more options like Hidden slides etc,
' then you need to use the other method as in the below link with the additional parameters shown in it
'https://docs.microsoft.com/en-us/office/vba/api/powerpoint.presentation.exportasfixedformat

pptObj.Close
pptObj = Nothing

pptApp.Quit
pptApp = Nothing

'Note: It is observed that in spite of quitting the application in the above
'line of code, the Powerpoint window still remains open. This appears to be
'a common problem among many developers over the internet and so a new action
'in the diagram with a Kill Process on the powerpnt.exe will close the Application window.

PDF generated from a ppt file with 2 slides:
View attachment 1541075954497.png
 

mmanasa2525

New Member
Thanks again. I tried to use the code and the stage failed as the error said it cannot open the file. See codestage error and codelogic.Also I wanted to make it generic as I have many files and plan to put a loop. Should I give anything in inputs and outputs?

Alternatively I thought Il start a VBO from scratch and replicated excel code for handle and Open.see pic opencode.Do I need to add anything extra But here as well it failed at Open stage. The Export PDF code I planned to place this - Let me know if this works-exportppttpdf.png


Thanks inadvance for your help!
 

Attachments

  • Codelogic.PNG
    103.8 KB · Views: 50
  • Codestageerror.PNG
    74.3 KB · Views: 32
  • exportppttpdf.PNG
    27.6 KB · Views: 28
  • Open code for ppt.PNG
    22 KB · Views: 27

VJR

Well-Known Member
Hi mmanasa2525,

Do double check the path of the file. Also is your file a ppt instead of pptx?

The custom code you have used is that of a Workbook which is applicable to Excel but in Powerpoint the objects are Presentations and Slides. Also for the GetInstance function to work you will have to take a look at the Initialise tab -> Global code of the Ms Excel VBO, modify everything refers to a Worksheet and a Workbook and develop a similar function for Powerpoint including most of the other functions written in that tab. This is the reason I mentioned that it requires more coding to develop from scratch although you still can continue if you have the time, patience, energy, and lots of research :).
 

mmanasa2525

New Member
The file is pptx file.it opened the instance of the file but it says cannot open the file ..I just changed the path to my file ..will there b any custom code for ppt n different for pptx
 

VJR

Well-Known Member
No separate code for ppt/x. Have you had to add the Visual Basic namspace mentioned at the top of the code?
Not sure what's wrong with the file. Are you able to zip the file and attach after removing any sensitive information?
 

mmanasa2525

New Member
Yes the file has data just put one slide and added Microsoft.Visual Basic in Namespace imports too. Do I need to pass any inputs apart from code ?Did you create any Open instance object too before you export to PDF? I just put code stage and ran
 

VJR

Well-Known Member
Yes the file has data just put one slide and added Microsoft.Visual Basic in Namespace imports too. Do I need to pass any inputs apart from code ?Did you create any Open instance object too before you export to PDF? I just put code stage and ran
Yes it has just a single Code stage with the namespace added. Did you try with other files too?
 

mmanasa2525

New Member
Hey It worked now with other PPT. Not sure what went wrong :) By the way I can add inputs to this in generic fashion and give it right ? Instead of hardcoding the paths?
 

VJR

Well-Known Member
The above code is to get started on the conversion of ppt to pdf and can be customised as per requirement.
 

Lalithkumar

New Member
I happened to install the pdf add-in on my machine and used the below code to generate the pdf.

- Create a new object action inside the Ms Excel VBO and have the Start and Code stages as below. I made a duplicate copy of the Open Workbook stage and used it.
View attachment 2389

- Start stage Input parameters:
View attachment 2387

- Code stage Input parameters:
View attachment 2386

- Code stage:
View attachment 2388
Code:
Dim wb as Object = GetInstance(handle).Workbooks.Open(filename)
wb.Activate()  'can skip this line
wb.ExportAsFixedFormat (Type:=0, Filename:=FullPdfFilePath, Quality:=0) '0:=xlTypePDF, 0:=xlQualityStandard
'Refer other list of parameters in below link
'https://docs.microsoft.com/en-us/office/vba/api/excel.workbook.exportasfixedformat
wb = Nothing

- Input Excel File (Sheet1) and Process Diagram:
I have used the 'Open Workbook' action in the diagram to 'Show' the workbook, you may skip that if you don't need to display.
View attachment 2385

- Call to the new action from the Process diagram:
View attachment 2390

- Sample Output.pdf (opened in Chrome):
Page 1 from Sheet 1:
View attachment 2391

Page 2 from Sheet 2:
View attachment 2392



I tried same steps same as above,getting error in export pdf stage

Internal:Could not execute code stage because exception thrown by code stage:The given key was not present in the dictionary
 

VJR

Well-Known Member
I tried same steps same as above,getting error in export pdf stage

Internal:Could not execute code stage because exception thrown by code stage:The given key was not present in the dictionary
The dictionary key error generally appears when the parameters either input or output or both are not correctly generated from its corresponding actions for eg; handle from Create Instance Or Workbook Name data item Or sheet name or incorrect path. Anything else like if the parameters are not passed correctly eg; handle is created for one workbook but passed for another also creates the issue.
 
Top