473,320 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Adding multiple pdf files into EXCEL using OLEObject??

I have the following code which allows me to add the pdf files if I give the exact name and path of the file. But I have multiple pdf files in a folder which I want to add to each cell in the first column. I am having trouble thinking of the logic to have a for loop for a folder which contains the pdf files.
Expand|Select|Wrap|Line Numbers
  1.  private void button1_Click(object sender, EventArgs e)
  2.         {
  3.  
  4.             Excel.Application xlApp;
  5.             Excel.Workbook xlWorkBook;
  6.             Excel.Worksheet xlWorkSheet;
  7.             xlApp = new Excel.ApplicationClass();
  8.             xlWorkBook = xlApp.Workbooks.Add(true);
  9.             xlWorkBook = xlApp.Workbooks.Open(@"C:\Users\excelFile01.xlsx", 0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, true, 0, true, 1, 0);
  10.  
  11.             xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets[1];
  12.  
  13.             int count = 0;
  14.             do
  15.             {
  16.                 count++;
  17.                 foreach (string f in Directory.GetFiles(@"C:\Users\pdf"))
  18.                 {
  19.  
  20.                     xlWorkSheet.Shapes.AddOLEObject(Filename: @"C:\Users\pdf\ir1.pdf", Link: true, DisplayAsIcon: true, IconFileName: "pdf", IconIndex: 1, IconLabel: "pdf1",Width: 5, Height: 5);
  21.  
  22.                     xlWorkSheet.Shapes.AddOLEObject(Filename: @"C:\Users\pdf\ir2.pdf", Link: true, DisplayAsIcon: true, IconFileName: "pdf2", IconIndex: 1, IconLabel: "pdf2", Left: i+10 ,Top: j+10, Width: 5, Height: 5);
  23.                 }
  24.             } while (count <= 3);
  25.  
  26.             xlWorkBook.SaveAs(@"C:\Users\excelFile03.xlsx");
  27.         }
  28.  
But this is very very rough draft. I have say 20 files in the folder pdf with different names(Title). How can I add all the 20 pdf files in the excel sheet?

Thank you in advance
Jun 24 '13 #1

✓ answered by Rabbit

So then use it as such.
Expand|Select|Wrap|Line Numbers
  1. foreach (string f in Directory.GetFiles(@"C:\Users\pdf")) 
  2.    xlWorkSheet.Shapes.AddOLEObject(Filename: f, .... ); 
  3. }
I also don't understand why you have a loop within a loop.

6 6105
Rabbit
12,516 Expert Mod 8TB
What's the point of the loop on line 17 if you're not going to use it?
Jun 24 '13 #2
Tht is the part of code where I need some help. I am hoping to have some "for loop" for that particular pdf folder (@"C:\Users\pdf"), which has all the 20 pdf files.
Does this make any sense?
Jun 24 '13 #3
Rabbit
12,516 Expert Mod 8TB
So then use it as such.
Expand|Select|Wrap|Line Numbers
  1. foreach (string f in Directory.GetFiles(@"C:\Users\pdf")) 
  2.    xlWorkSheet.Shapes.AddOLEObject(Filename: f, .... ); 
  3. }
I also don't understand why you have a loop within a loop.
Jun 24 '13 #4
I was just trying. I am very very new to coding.

Also, this works but now all the 20 pdf files are piled up on one another in the first column of excel sheet. How can I have them separated. In the sense, how can I have them at different cells or places in the Excel sheet?
Jun 24 '13 #5
I got the Solution
Expand|Select|Wrap|Line Numbers
  1. int i = 100;
  2.             int j = 0;
  3.             foreach (string f in Directory.GetFiles(@"C:\Users\pdf"))
  4.             {
  5.  
  6.                 j = j + 100;
  7.                 xlWorkSheet.Shapes.AddOLEObject(Filename: f, Link: true, DisplayAsIcon: true, IconFileName: "pdf", IconIndex: 1, IconLabel: "pdf1", Left : i, Top: j, Width: 5, Height: 5);
  8.             }
  9.  
I just should read more I guess. I am trying.
Thank you for your help though :-)
Jun 24 '13 #6
Rabbit
12,516 Expert Mod 8TB
Glad you got it working. Good luck with the rest of your project.
Jun 24 '13 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Brian Conklin | last post by:
Hello Eneryone, I am having a problem. I have written a little app that will take a text "pipe" delimited file and place all of the values in to an Excel spreadsheet. It works great on any of my...
7
by: Eran.Yasso | last post by:
Hi, I have project that automate excel(using Excel COM) which works fine in my home. I took the project from my home to work and tried to build the project but it won't built. I get error "The...
2
by: rengask | last post by:
I got the code to find and replace within an open text file. ------------------ Private Sub cmdFile_Click() Dim strTemp As String txtFile = "" dlg.FileName = "*.*" dlg.ShowOpen ...
1
by: giddy | last post by:
Hi , I've read this a long time ago , read it again:...
0
by: wankhusairi | last post by:
hallo sir .. i am still new on using vb and i have tried to load an excel using a visual basic interface.. but if i wanted to plot my excel data what should i do.. must i cahnge my program on loading...
10
by: kimiraikkonen | last post by:
Hi, I have an app which has a listbox and when i double click an associated fileS, i want their paths to be added into listbox in my application. This code works good when i try to open a...
1
by: rameshmandapati | last post by:
hi i have a problem with Excel. iam adding data to a cell in Excel using java script var excel = new ActiveXObject ("Excel.Application"); but i want image also i search in google i...
1
by: ronakinuk | last post by:
how to insert multiple rows in excel using vba how to insert multiple rows in excel using vba how to insert multiple rows in excel using vba
9
by: Man4ish | last post by:
Hi, I am newbie to multi-threading. I am working on one pblm where i need to process 24 files (each size = 3 GB) and write the output onto multiple files(24). Each file takes around 1 hour to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.