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. -
private void button1_Click(object sender, EventArgs e)
-
{
-
-
Excel.Application xlApp;
-
Excel.Workbook xlWorkBook;
-
Excel.Worksheet xlWorkSheet;
-
xlApp = new Excel.ApplicationClass();
-
xlWorkBook = xlApp.Workbooks.Add(true);
-
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);
-
-
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets[1];
-
-
int count = 0;
-
do
-
{
-
count++;
-
foreach (string f in Directory.GetFiles(@"C:\Users\pdf"))
-
{
-
-
xlWorkSheet.Shapes.AddOLEObject(Filename: @"C:\Users\pdf\ir1.pdf", Link: true, DisplayAsIcon: true, IconFileName: "pdf", IconIndex: 1, IconLabel: "pdf1",Width: 5, Height: 5);
-
-
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);
-
}
-
} while (count <= 3);
-
-
xlWorkBook.SaveAs(@"C:\Users\excelFile03.xlsx");
-
}
-
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
So then use it as such. - foreach (string f in Directory.GetFiles(@"C:\Users\pdf"))
-
{
-
xlWorkSheet.Shapes.AddOLEObject(Filename: f, .... );
-
}
I also don't understand why you have a loop within a loop.
6 6105
What's the point of the loop on line 17 if you're not going to use it?
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?
So then use it as such. - foreach (string f in Directory.GetFiles(@"C:\Users\pdf"))
-
{
-
xlWorkSheet.Shapes.AddOLEObject(Filename: f, .... );
-
}
I also don't understand why you have a loop within a loop.
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?
I got the Solution -
int i = 100;
-
int j = 0;
-
foreach (string f in Directory.GetFiles(@"C:\Users\pdf"))
-
{
-
-
j = j + 100;
-
xlWorkSheet.Shapes.AddOLEObject(Filename: f, Link: true, DisplayAsIcon: true, IconFileName: "pdf", IconIndex: 1, IconLabel: "pdf1", Left : i, Top: j, Width: 5, Height: 5);
-
}
-
I just should read more I guess. I am trying.
Thank you for your help though :-)
Glad you got it working. Good luck with the rest of your project.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
...
|
by: giddy |
last post by:
Hi ,
I've read this a long time ago , read it again:...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
|
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...
| |