Forum Home
Press F1
 
Thread ID: 142918 2016-10-08 23:42:00 Access VBA automation with Acrobat Tony (4941) Press F1
Post ID Timestamp Content User
1427110 2016-10-08 23:42:00 Using Acrobat 9.5 and Access 2016. I'm trying to analyse a problem, so I have this test code:



Sub testObjects()
Dim i As Boolean
Dim objapp As acrobat.AcroApp
Set objapp = New acrobat.AcroApp
objapp.Show
i = objapp.CloseAllDocs
Debug.Print i
i = objapp.Exit
Debug.Print i
Set objapp = Nothing
End Sub

After Objapp.closealldocs I get i = true (succeeded) but after i=objapp.exit I get i= false (failed), and sure enough Acrobat is still active. How do I get Acrobat to close? This is a bit of test code. In the real app task manager shows acrobat using more and more memory as the app loops through the records.


I suspect I'm missing something obvious here, but what? My experience with automation is limited, so be gentle with me! :)
Tony (4941)
1427111 2016-10-09 03:44:00 I think you need to relearn return values.

When exiting a program, any non zero value returned is an error, this has been standard for many years and I am sure this would not have changed.

Understand what value you are expecting, in this case it should be numeric not boolean and you will not run into this problem and misinterpret your data.

As for closing the process, if exit doesnt close it, then there should be an exception to why its still running.
Kame (312)
1427112 2016-10-09 04:53:00 Thanks for the response. From the Acrobat documentation:


CloseAllDocs
Closes all open documents. You can close each individual AVDoc object by calling AVDoc.Close.
You must explicitly close all documents or call App.CloseAllDocs. Otherwise, the process never exits.
Syntax
VARIANT_BOOL CloseAllDocs();
Returns
-1 if successful, 0 if not.


and


Exit
Exits Acrobat. Applications should call App.Exit before exiting.
Note:Use App.CloseAllDocs to close all the documents before calling this method.

Syntax
VARIANT_BOOL Exit();
Returns
Returns -1 if the entire shutdown process succeeded. This includes closing any open documents, releasing OLE references, and finally exiting the application. If any step fails, the function returns 0, and the application continues running. This method does not work if the application is visible (if the user is in control of the application.

I've just noticed the last bit "This method does not work if the application is visible (if the user is in control of the application)". In the test code that is the case, so I would now expect it to fail. However in the "real" code it is not visible so I would expect it to work.

I've just done a test with the object set to hide and I also declared i as integer and I still get a return of -1 (success) then 0 (failure). However Acrobat does terminate. I need to do some more looking.
Tony (4941)
1427113 2016-10-09 07:20:00 Hide
CloseAllDocs
Exit
Kame (312)
1427114 2016-10-09 08:12:00 Hide
CloseAllDocs
ExitWell, yes, exactly. That is where I am up to, as per the last line in my last post. I need to look at the "real" app and see if there is anything else going on.
Tony (4941)
1427115 2016-10-15 22:50:00 OK, here is the full story - sorry it is a bit long:
The situation is that I have an application that loops through a table getting membership info and using it to generate individualised PDF files that are then attached to emails to the members. It all works, but when I look in Task Manager, Acrobat is occupying more and more memory as the job progresses. This is sort-of OK with my test data, but when I start processing the full database it is not going to cope.
Here is a mixture of code and pseudocode that illustrates what I am doing.



Sub ProcessEmails()

For each record in the table:

Call GeneratePDF(various member data arguments…)
Next record
Tidy up

End sub

Sub GeneratePDF(member data parameters…) This is where the work gets done

Dim AcroApp As Acrobat.CAcroApp
Dim ThePage As Object
Dim TheDoc As Acrobat.CAcroPDDoc
Dim jso As Object
Dim strPDFtemplate As String
Dim CAcroRect As New Acrobat.AcroRect

plus other declarations

Set AcroApp = CreateObject("AcroExch.App")
Set TheDoc = CreateObject("AcroExch.PDDoc")

some setup code, then

AcroApp.Hide
TheDoc.Open (strPDFtemplate)
Set jso = TheDoc.GetJSObject
Set ThePage = TheDoc.AcquirePage(0)
jso.addWatermarkFromText "1 January - 31 December " + year, jso.app.Constants.Align.Left, "Arial-Bold", 10, Colarray, 0, 0, True, True, True, jso.app.Constants.Align.Center, jso.app.Constants.Align.Center, 50, 301.5, False, 1, False, 0, 1 'Year

then a whole lot more of the same
To finish:

i = TheDoc.Save(PDSaveFull Or PDSaveCollectGarbage, pubPDFUpdatedFilename)
TheDoc.Close
AcroApp.Exit
Set TheDoc = Nothing
Set AcroApp = Nothing

End Sub


It would appear that I am not terminating something I should be during this process so Acrobat is not releasing memory.
I have tried all sorts of different things, including putting the instantiation and termination of Acrobat outside the read loop, but that everything I have tried either makes no difference or gives me various execution time errors.
I’m not very experienced with Automation and I find the Acrobat documentation fairly opaque, so I’m hoping there is something obvious I’ve missed and that some eagle-eyed forum member will be able to help me.
Tony (4941)
1