×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Acrobat 7 not closing through OLE in VB.net

Acrobat 7 not closing through OLE in VB.net

Acrobat 7 not closing through OLE in VB.net

(OP)
Hi,

I've written an app in VB.net for bulk mailing of clients customers.

As part of this app I have to insert OMR markers into the documents that are being printed off.

The issue that I am having is that I can open an Acrobat file and insert the markers then print off the document for the first customer but when the app gets to the second customers documents and tries to open the same acrobat document for them I get an error telling me the document is already open by another user / process.

When I look at task manager, I can see an Acrobat process is still open on my machine. I've tried specifically closing the app, the avdocument and the PDdocument I've also used a sub routine to release the activeX / com objects but still no luck.

Here is my code:

CODE

Public Sub PrintAdobe (byref sFile as String)
dim destFile, strOMR as string
dim acroApp, acroAV, acroPD, acroAV1, acroPD1, srcPage, destPage, PageSize, srcAnnot, destAnnot, acroRect As object
dim intPage, endPage, srcPageNum, PageHeight, intAnnot as integer



On Error GOTO ErrHandler
if File.Exists(sFile) then
    'copy to new destination file
    destFile = replace(sFile, ".pdf", "_OMR.pdf")
    if File.Exists(destFile) Then File.Delete(destFile)
    File.Copy(sFile, destFile)
    'Initialise our Acrobat objects
    acroApp = CreateObject("AcroExch.App")
    acroAV = createObject("AcroExch.AVDoc")
    acroAV.Open(destFile, dir(destFile))
    acroPD = CreateObject("AcroExch.PDDoc")
    acroPD = acroAV.GetPDDoc
    endPage = acroPd.GetNumPages - 1
    'need to insert a template PDF page with OMR markers perdefined as Annots
    acroAV1 = CreateObject("AcroExch.App")
    acroAV1.Open (OMRTEMPLATEPATH, "OMRTemplate.pdf")
    acroPD1  = CreateObject("AcroExch.PDDoc")
    'inserting Template
    acroPD.InsertPages(-2,acroPD1,0,1,False)
    srcPageNum = acroPD.GetNumPages - 1
    srcPage = acroPD.AcquirePage(srcPageNum)
    for intPage = 0 to endPage
        'increase global page count
        gPageCount = gPageCount + 1
        strOMR ="OMR_"
        'if last page in the batch need envelope Insert
        if gPageCount = gLastPage then
            strOMR = strOMR & "I_"
        else
            strOMR = strOMR & "S_"
        end if
        'add our OMR sequence Number
        strOMR = strOMR & gIntSeq
        ' now we have the name of the OMR Marker we are looking for e.g. OMR_S_1
        'get the current page object
        destPage = acroPD.AcquirePage(intPage)
        'need to find height of page in order to place OMR
        PageSize = destPage.GetSize()
        PageHeight = PageSize.y
        'parse through each template Annot and find matching name
        for intAnnot = 0 to srcPage.GetNumAnnots - 1
            srcAnnot = srcPage.GetAnnot(intAnnot)
            if srcAnnot.GetTitle = strOMR then
                'add the src annot  to the page
                destPage.AddAnnot(-2, srcAnnot)
                destAnnot = destPage.GetAnnot(0)
                acroRect = destAnnot.GetRect
                with acroRect
                    'position our annot
                    .Left = 3.3
                                    .right = 54
                    if PageHeight > 253.85 then 'should be but just in case...
                        'N.B. Acrobat coordinates in points @ 72pt/inch starting in bottom left of page
                        .top = PageHeight - 163.75
                        .bottom = PageHeight - 253.85    
                    end if
                end with
                destAnnot.SetRect(acroRect) 'update  our Rectangle
                destAnnot.SetOpen(1) 'Open our Annot in print
                gintSeq = gintSeq + 1
                if gintSeq > 7 then gintSeq = 0
                Exit For
            End If
        next    
    next
    acroPD.Save(PDSaveFlags.PDSaveFull, destFile)
    acroAV.PrintPagesSilent(0, endPage, 2, False, False)
    System.Threading.thread.sleep(1000) 'give it a second to print    
end if
OUT:
    If not acroPD is nothing then
        acroPD.close()
        ReleaseActiveX(acroPD)
        acroPD = nothing
    end if

    If not acroAV is nothing then
        acroAV.Close()
        ReleaseActivex(acroAV)
        acroAV = nothing
    End If
    If not acroApp is nothing then
        acroApp.CloseAllDocs()
        acroApp.Exit()
        ReleaseActiveX(acroApp)
        acroApp =  nothing
    End If
    Exit Sub
ErrHandler:
    On Error Resume Next
    msgbox(Err.Number &": " &Err.Description, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
    Resume OUT
End Sub

Public Sub ReleaseActiveX(ByVal o As Object)
        'ensures active X / Com objects are closed
        Try
       System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
        Catch ex As Exception
        Finally
            o = Nothing
        End Try
End Sub

Any Ideas?

RE: Acrobat 7 not closing through OLE in VB.net

(OP)
OK,

I'm getting desperate here I've tried the following code and even though I think it is over kill I'm still not getting anywhere....

CODE

Imports System.Diagnostics 'put in your imports area of the form etc.
..........
Public sub KillAcrobat()
        dim MyProcess as Process

        On Error Resume Next
        If Process.GetProcessesByName("Acrobat").Length > 0 Then
            'acrobat is still open
            Do Until Process.GetProcessesByName("Acrobat").Length = 0
                'get each instance of Acrobat
                For Each myProcess In Process.GetProcessesByName("Acrobat")
                    'Attempt to close it
                    myProcess.Close()
                    Threading.Thread.Sleep(1000) 'wait for a second
                    If Not myProcess.HasExited Then 'still not gone
                        myProcess.Kill() ' force closure
                    End If
                    myProcess = Nothing
                Next
            Loop
        End If
        myProcess = Nothing
End Sub
 

RE: Acrobat 7 not closing through OLE in VB.net

(OP)
Hi all,

It looks as if the PDF stuff is behaving like static variables even though they should be dynamic!

I tried not saving anything just modify print and the close with no save and I could print multiple batches.

The trouble is in order to put the correct mark up in to the document I had to insert a template document the already had the mark up I needed so I could copy it over to each page. I then deleted the template page from the document but the following two things are happening:
1. The template page is not deleting
2. The next time I go back into the document, the template page is there.
3.  The next time I go back into the document, the mark up I put in previously is still there and affects the mark up I'm trying to put in now.

Any ideas?

CODE

    Private Sub PrintPDF(ByRef sFile As String)
        Dim prtDoc As New PrintDocument
        Dim strDefaultPrinter As String = prtDoc.PrinterSettings.PrinterName 'find current default printer and store
        Dim lngTray As Long = prtDoc.DefaultPageSettings.PaperSource.RawKind 'find current default paper tray and store
        Dim strPrinter As String
        Dim strFileName As String
        Dim WshNetwork As Object
        Dim strOMR As String
        Dim boolOK As Boolean
        Dim destPageNum, pageHeight As Integer

        On Error GoTo errHandler

        'what printer are we using?
        If gColour Then
            strPrinter = gCPrinter
        Else
            strPrinter = gBWPrinter
        End If
        'TODO: find how to change default print trays....
        'set default printer to selected Item if necessary adobe always prints to default
        If strDefaultPrinter <> strPrinter Then
            WshNetwork = Microsoft.VisualBasic.CreateObject("WScript.Network")
            WshNetwork.SetDefaultPrinter(strPrinter)

            If Not prtDoc.PrinterSettings.IsValid Then
                'invalid print name change back to original
                WshNetwork.SetDefaultPrinter(strDefaultPrinter)
                strPrinter = strDefaultPrinter
            End If
        End If
        strFileName = Dir(sFile)
        Dim AcroAVDoc, AcroPDDoc As Object
        Dim AcroAVDoc1, AcroPDDoc1 As Object
        If File.Exists(sFile) Then
            AcroAVDoc = CreateObject("AcroExch.AVDoc")
            AcroPDDoc = CreateObject("AcroExch.PDDoc")
            AcroAVDoc.Open(sFile, strFileName)
            AcroPDDoc = AcroAVDoc.GetPDDoc
            ' OMR Stuff
            If gOMR Then
                AcroAVDoc1 = CreateObject("AcroExch.AVDoc")
                AcroAVDoc1.open(gOMRTemplate, "OMR Template.pdf")
                AcroPDDoc1 = CreateObject("AcroExch.PDDoc")
                AcroPDDoc1 = AcroAVDoc1.GetPDDoc
                AcroPDDoc.InsertPages(-2, AcroPDDoc1, 0, 1, False)
                destPageNum = AcroPDDoc.GetNumPages - 1
                Dim srcPage As Object
                srcPage = AcroPDDoc.AcquirePage(destPageNum)
                For intPage = 0 To destPageNum - 1
                    gPageCount = gPageCount + 1
                    strOMR = "OMR_"
                    'the last page do insert...
                    If gPageCount = gLastPage Then
                        'insert
                        strOMR = strOMR & "I_"
                    Else
                        'store
                        strOMR = strOMR & "S_"
                    End If
                    strOMR = strOMR & gintSequence
                    Dim destPage As Object
                    destPage = AcroPDDoc.AcquirePage(intPage)
                    'get size of page, N.B. adobe grid starts in bottom left corner
                    Dim pageSize As Object
                    pageSize = destPage.GetSize()
                    'Assume 72 pt per inch  - from adobe example A4 =842.4 pts
                    pageHeight = pageSize.y
                    For intAnnot = 0 To srcPage.GetNumAnnots - 1
                        Dim srcAnnot As AcroPDAnnot
                        srcAnnot = srcPage.GetAnnot(intAnnot)
                        If srcAnnot.GetTitle = strOMR Then
                            destPage.AddAnnot(-2, srcAnnot)
                            Dim destAnnot As AcroPDAnnot
                            destAnnot = destPage.GetAnnot(0)
                            Dim AcroRect As AcroRect
                            AcroRect = destAnnot.GetRect
                            With AcroRect
                                .Left = 3.3
                                .right = 54
                                If pageHeight > 253.85 Then
                                    .bottom = pageHeight - 253.85
                                    .Top = pageHeight - 163.75
                                End If
                            End With
                            destAnnot.SetRect(AcroRect)
                            destAnnot.SetOpen(1)
                            AcroRect = Nothing
                            destAnnot = Nothing
                            srcAnnot = Nothing
                            gintSequence = gintSequence + 1
                            If gintSequence > 7 Then gintSequence = 0
                            Exit For
                        End If
                    Next
                    pageSize = Nothing
                    destPage = Nothing
                    System.Windows.Forms.Application.DoEvents()
                Next
                srcPage = Nothing
                boolOK = AcroPDDoc.DeletePages(destPageNum, destPageNum)
                If boolOK = False Then
                    MsgBox("Can delete appended page " & destPageNum, MsgBoxStyle.OkOnly)
                End If
            Else
                AcroPDDoc1 = Nothing
                AcroAVDoc1 = Nothing
                destPageNum = AcroPDDoc.GetNumPages
            End If
            ' Print the Adobe file
            AcroAVDoc.PrintPagesSilent(0, destPageNum - 1, 2, False, False)
            System.Threading.Thread.Sleep(1000)
        Else
            AcroPDDoc1 = Nothing
            AcroAVDoc1 = Nothing
            AcroPDDoc = Nothing
            AcroAVDoc = Nothing
        End If
Out:    'general house keeping
        If strDefaultPrinter <> strPrinter Then
            WshNetwork = Microsoft.VisualBasic.CreateObject("WScript.Network")
            WshNetwork.SetDefaultPrinter(strDefaultPrinter)
        End If
       
        If Not AcroPDDoc1 Is Nothing Then
            AcroPDDoc1.close()
            ReleaseActiveX(AcroPDDoc1)
            AcroPDDoc1 = Nothing
        End If
        If Not AcroPDDoc Is Nothing Then
            AcroPDDoc.close()
            ReleaseActiveX(AcroPDDoc)
            AcroPDDoc = Nothing
        End If
        If Not AcroAVDoc1 Is Nothing Then
            AcroAVDoc1.close(True)
            ReleaseActiveX(AcroAVDoc1)
            AcroAVDoc1 = Nothing
        End If
        If Not AcroAVDoc Is Nothing Then
            AcroAVDoc.close(True)
            ReleaseActiveX(AcroAVDoc)
            AcroAVDoc = Nothing
        End If
        Exit Sub
errHandler:
        On Error Resume Next
        MsgBox("PDF Print error:" & Err.Number & ":" & Err.Description, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
        Resume Out
    End Sub
 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close