Solved

Find text in Excel and PDF using VB.Net

Posted on 2013-10-29
6
1,615 Views
Last Modified: 2013-12-17
Hi,

Using VB.Net I am accepting search string from the User. I want to find this in Excel and PDF files. How can I do it?

I've tried some code in Excel, but that's searching all Excel files. But PDF code I've not got so far. Excel code is given here.

                            Dim xlApp As Excel.Application
                            xlApp = CreateObject("Excel.Application")
                            Dim xlFile As Excel.Workbook = Nothing
                            Dim xlSheet As Excel.Worksheet = Nothing

                            Try

                                ' Open Excel file, assign sheet
                                xlFile = xlApp.Workbooks.Open(UploadFolder & "\" & FileName)
                                xlFile.Activate()
                                xlSheet = xlFile.Sheets("Sheet1")

                                ' Find text
                                With xlSheet.Range("A1:Z10000")
                                    Dim TextFound As Excel.Range = .Find(What:=SearchString, _
                                                    LookIn:=Excel.XlFindLookIn.xlValues, _
                                                    LookAt:=Excel.XlLookAt.xlWhole, _
                                                    SearchOrder:=Excel.XlSearchOrder.xlByRows, _
                                                    SearchDirection:=Excel.XlSearchDirection.xlNext, _
                                                    MatchCase:=False)

                                    If Not TextFound Is Nothing Then
                                        ExcelFiles += 1
                                    End If
                                End With

                            Catch Ex As Exception
                                MsgBox("Excel search could be completed..." & vbNewLine & Ex.Message, MsgBoxStyle.Information, "Search Excel")
                            Finally
                                ' Release objects
                                GC.Collect()
                                GC.WaitForPendingFinalizers()
                                Marshal.FinalReleaseComObject(xlSheet)
                                xlFile.Close()
                                Marshal.FinalReleaseComObject(xlFile)

                                xlApp.Quit()
                                Marshal.FinalReleaseComObject(xlApp)
                            End Try

Open in new window


Thanks, I will need to know PDF code also.

San.
0
Comment
Question by:sanjay-gandhi
  • 4
  • 2
6 Comments
 
LVL 8

Expert Comment

by:5teveo
ID: 39608597
0
 
LVL 8

Expert Comment

by:5teveo
ID: 39609159
Here's another macro w/ code

Uses Adobe 9.0

http://quickvba.blogspot.com/
0
 

Author Comment

by:sanjay-gandhi
ID: 39609735
Thanks, will try, test, and revert.
San.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:sanjay-gandhi
ID: 39609822
Dear Steveo,

Sorry, but may be I've not explained correctly. I am writing my program in VB.Net. And I am accepting a text string from the User. Now I want to find that text string in the PDF files in a folder. Whether this text string appears in any PDF file or not, is what I want to find out. I know how to browse files in a folder, but I want to find a text string and return true if that string appears in the PDF file.

Any doubt, please let me know. (However, thanks for that post, which you've sent, but my question is different).

San.
0
 

Accepted Solution

by:
sanjay-gandhi earned 0 total points
ID: 39714484
Hi,

I've been able to find this code, using iTextSharp dll.

Imports iTextSharp.text.pdf
Imports iTextSharp.text.pdf.parser

                            Dim Page As Integer
                            Dim Reader = New PdfReader(SearchFile)
                            Dim TextFound As Boolean
                            Dim TextPDF As String = ""

                            For Page = 1 To Reader.NumberOfPages
                                TextPDF += PdfTextExtractor.GetTextFromPage(Reader, Page)
                            Next

                            TextFound = TextPDF.Contains(SearchString)
                            If TextFound Then
                                ' Count
                                PDFFiles += 1
                                TotalSearched = (TextFiles + WordFiles + ExcelFiles + PowerPointFiles + PDFFiles)
                                SearchStringFound = True
                            End If

                            Reader.Close()

Open in new window



This works fine.

San
0
 

Author Closing Comment

by:sanjay-gandhi
ID: 39723484
Could not get any other solution. A friend suggested this, and this works fine.

San.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question