[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Find text in Excel and PDF using VB.Net

Posted on 2013-10-29
6
Medium Priority
?
1,909 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

656 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