Link to home
Start Free TrialLog in
Avatar of Sanjay Gandhi
Sanjay GandhiFlag for India

asked on

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.
Avatar of 5teveo
5teveo
Flag of United States of America image

Here's another macro w/ code

Uses Adobe 9.0

http://quickvba.blogspot.com/
Avatar of Sanjay Gandhi

ASKER

Thanks, will try, test, and revert.
San.
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.
ASKER CERTIFIED SOLUTION
Avatar of Sanjay Gandhi
Sanjay Gandhi
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Could not get any other solution. A friend suggested this, and this works fine.

San.