Solved

Find text in Excel and PDF using VB.Net

Posted on 2013-10-29
6
1,516 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
Comment Utility
0
 
LVL 8

Expert Comment

by:5teveo
Comment Utility
Here's another macro w/ code

Uses Adobe 9.0

http://quickvba.blogspot.com/
0
 

Author Comment

by:sanjay-gandhi
Comment Utility
Thanks, will try, test, and revert.
San.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:sanjay-gandhi
Comment Utility
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
Comment Utility
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
Comment Utility
Could not get any other solution. A friend suggested this, and this works fine.

San.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now