[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1952
  • Last Modified:

Find text in Excel and PDF using VB.Net

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
sanjay-gandhi
Asked:
sanjay-gandhi
  • 4
  • 2
1 Solution
 
5teveoCommented:
Here's another macro w/ code

Uses Adobe 9.0

http://quickvba.blogspot.com/
0
 
sanjay-gandhiAuthor Commented:
Thanks, will try, test, and revert.
San.
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

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

San.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now