Hi,
I'm currently doing a program to search for text in powerpoint files using interop in vb.net 2005. When a user does a search, it will open the powerpoint (.ppt) file and do the search. This is working fine.

What I want to achieve next is if the powerpoint file is already opened, I want the program to search for text in the opened powerpoint file (without opening the file again in ReadOnly mode).
How can I do that..? I mean, I need to first search the list of opened program to see if the powerpoint file is opened and then i need to somehow cause my program to gain control of the opened powerpoint file before I can do a search right..? How can I do that..?

Can somebody please advice..? Thanks.

Dani AI

Generated

was on the right track — you attach to the running PowerPoint COM server and then inspect its Presentations collection instead of opening the file again. The reliable pattern is: try to get the active Application, iterate app.Presentations looking for the presentation whose FullName matches your file, and then scan each Slide/Shape/NotesPage for text. Important caveats: Office COM must run in an STA thread, you must match Office bitness (run your process as x86 if Office is 32-bit), release COM objects (Marshal.ReleaseComObject) and do NOT use Office interop from ASP.NET/server code (server-side Automation of Office is unsupported and brittle).

Example VB.NET pattern (requires a reference to Microsoft.Office.Interop.PowerPoint and Office core):

' Attach to running PowerPoint or create one
Dim app As Microsoft.Office.Interop.PowerPoint.Application = Nothing
Dim createdNewApp As Boolean = False

Try
    Try
        app = CType(System.Runtime.InteropServices.Marshal.GetActiveObject("PowerPoint.Application"), Microsoft.Office.Interop.PowerPoint.Application)
    Catch ex As System.Runtime.InteropServices.COMException
        app = New Microsoft.Office.Interop.PowerPoint.Application()
        createdNewApp = True
    End Try

    Dim targetPath As String = "C:\path\to\file.ppt"
    Dim foundPres As Microsoft.Office.Interop.PowerPoint.Presentation = Nothing

    For Each pres As Microsoft.Office.Interop.PowerPoint.Presentation In app.Presentations
        If String.Equals(pres.FullName, targetPath, StringComparison.OrdinalIgnoreCase) Then
            foundPres = pres
            Exit For
        End If
    Next

    If foundPres IsNot Nothing Then
        Dim searchTerm As String = "needle"
        For Each sld As Microsoft.Office.Interop.PowerPoint.Slide In foundPres.Slides
            For Each shp As Microsoft.Office.Interop.PowerPoint.Shape In sld.Shapes
                If shp.HasTextFrame = Microsoft.Office.Core.MsoTriState.msoTrue AndAlso shp.TextFrame.HasText = Microsoft.Office.Core.MsoTriState.msoTrue Then
                    If shp.TextFrame.TextRange.Text.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase) >= 0 Then
                        app.ActiveWindow.View.GotoSlide(sld.SlideIndex)
                    End If
                End If
            Next
            For Each nshp As Microsoft.Office.Interop.PowerPoint.Shape In sld.NotesPage.Shapes
                If nshp.HasTextFrame = Microsoft.Office.Core.MsoTriState.msoTrue AndAlso nshp.TextFrame.HasText = Microsoft.Office.Core.MsoTriState.msoTrue Then
                    If nshp.TextFrame.TextRange.Text.IndexOf(searchTerm, StringComparison.OrdinalIgnoreCase) >= 0 Then
                        app.ActiveWindow.View.GotoSlide(sld.SlideIndex)
                    End If
                End If
            Next
        Next
    Else
        foundPres = app.Presentations.Open(targetPath, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue)
    End If
Finally
    If foundPres IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(foundPres)
    If createdNewApp AndAlso app IsNot Nothing Then
        app.Quit()
    End If
    If app IsNot Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(app)
    GC.Collect()
    GC.WaitForPendingFinalizers()
End Try

Troubleshooting notes: if GetActiveObject throws, it usually means no instance is running or there is a security/bitness mismatch. If your scenario is server-side or needs to process many files, prefer extracting text with Open XML (for .pptx) or a dedicated library (third-party) rather than Office Interop. Finally, do not call Quit on the app if you attached to a user’s running instance — only quit when you created the Application. This keeps the user's PowerPoint session intact.

Recommended Answers

All 2 Replies

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.