cancel
Showing results for 
Search instead for 
Did you mean: 

Insert PDF file into excel sheet

SOLVED
nimishbhuta
Frequent Contributor

Insert PDF file into excel sheet

Hello All, 

 

Scenario : I need to have the PDF document shown in the excel sheet. One way I can do it by taking screenshot of PDF file but then it will show for the first page shown on the screen which is not good approach as I require all the pages in the PDF file. 

 

Another way approach is to insert the PDF file in the excel sheet. Like how we do insert object in excel and select the file name which will show all the pages of PDF file. 

 

In this approach, I need to know how can I insert PDF file in excel sheet using vbscript.

 

Regards,

 

Nimish  

1 ACCEPTED SOLUTION

Accepted Solutions
TanyaYatskovska
Community Manager

Re: Insert PDF file into excel sheet

Hi @nimishbhuta,

 

The following code adds a pdf file to an Excel file - it works on my computer:

Sub Test

  Set Excel = Sys.OleObject("Excel.Application")
  Excel.Workbooks.Open("<path to your Excel file>")
  
  Set Ws = Excel.ActiveSheet

  'Add the pdf file
  Set OLEObj=Ws.OLEObjects.Add(,"<path to your PDF file>",true,false)
  
  'If you want to add the file to a specific place
    With OLEObj
        .Left = Ws.Range("A1").Left
        .Height = Ws.Range("A1").Height
        .Width = Ws.Range("A1").Width
        .Top = Ws.Range("A1").Top
    End With
 
  Excel.Quit
End Sub

 

P.S. Sometimes I simply love coding 🙂

---------
Tanya Yatskovskaya
SmartBear Community and Education Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

View solution in original post

10 REPLIES 10
cunderw
Community Hero

Re: Insert PDF file into excel sheet

Do to this you will need to work with Excel using COM:

 

https://support.smartbear.com/testcomplete/docs/testing-with/advanced/working-with-external-data-sou...

 

There is an insert file method: 

https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.module.insertfile?view=ex...

 

 


Thanks,
Carson

Click the Accept as Solution button if my answer has helped
nimishbhuta
Frequent Contributor

Re: Insert PDF file into excel sheet

Hello Carson, 

 

This is not much useful as it shows how to do it in C#. I am using the vbscript, please let me know how can I insert the PDF file into excel sheet. 

 

Regards,

 

Nimish 

tristaanogre
Community Hero

Re: Insert PDF file into excel sheet

What @cunderw  posted was simply the article indicating the existance of the method.  The first link has examples in multiple code languages that show how to instantiate the excel object and call methods.  You should be able to adapt the two into what you need.


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
nimishbhuta
Frequent Contributor

Re: Insert PDF file into excel sheet

I have checked on the COM object and it's method but I could find any method that insert file into excel sheet. 

 

Please point me to the exact method if you are aware about it.

 

Regards,

 

Nimish

tristaanogre
Community Hero

Re: Insert PDF file into excel sheet

@cunderw  provided that.

https://docs.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.module.insertfile?view=ex...

 

It's called "InsertFIle"


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
nimishbhuta
Frequent Contributor

Re: Insert PDF file into excel sheet

Hi Robert, 

 

I checked but it seems that InsertFile method is not available in vbscript. Can you check and give me a sample code in vsbcript for InsertFile Method that will be really great?

 

Regards,

 

Nimish

tristaanogre
Community Hero

Re: Insert PDF file into excel sheet

As far as I know, InsertFile is not code language specific.  It is a method of the Excel object that is published whenever Excel is installed on the workstation.  So... there's not any "VBScript" code to share...


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
TanyaYatskovska
Community Manager

Re: Insert PDF file into excel sheet

Hi @nimishbhuta,

 

The following code adds a pdf file to an Excel file - it works on my computer:

Sub Test

  Set Excel = Sys.OleObject("Excel.Application")
  Excel.Workbooks.Open("<path to your Excel file>")
  
  Set Ws = Excel.ActiveSheet

  'Add the pdf file
  Set OLEObj=Ws.OLEObjects.Add(,"<path to your PDF file>",true,false)
  
  'If you want to add the file to a specific place
    With OLEObj
        .Left = Ws.Range("A1").Left
        .Height = Ws.Range("A1").Height
        .Width = Ws.Range("A1").Width
        .Top = Ws.Range("A1").Top
    End With
 
  Excel.Quit
End Sub

 

P.S. Sometimes I simply love coding 🙂

---------
Tanya Yatskovskaya
SmartBear Community and Education Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

View solution in original post

nimishbhuta
Frequent Contributor

Re: Insert PDF file into excel sheet

Hi Tanya, 

 

I tried the below code but it is giving an error message as "cannot insert object". Can you let me know what may be the issue?

 

Regards,

 

Nimish

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors