manipulating pdf files from vba

N

netloss

Hi -

I am processsing a file folder with about 60 pdf files in it, from word
2002 vba (windows XP - using acrobat 6). My code opens each file and
deletes some bookmarks before moving on to the next. Around pdf file 35
or so, I get the following error:

Runtime error '-2147417851(80010105)'
Automation Error
The server threw an exception

I have to go in and kill the Acrobat process to get it running again.
The program will then get a liltle further through the file list before
throwing the same exception.

The code uses GetJSObject to get at the bookmarks, if that makes any
difference ...

I tried closing (or set to Nothing) all the objects and the acrobat app
itself at the end of the process for each file, but this doesn't help.

Unlike most of the posts I've read through, this error happens every
time. I've tried it on two different computers. The other one has word
2000, but otherwise is the same.

Thanks in advance for any advice,
NL
 
W

Word Heretic

G'day "netloss" <[email protected]>,

I suspect a variable isn't being destroyed properly, but if you are
freezing screen updates, you may want to refresh the screen every 20
pdf's

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


netloss reckoned:
 
N

netloss

Here is the code that creates all the pdf objects and at the end (tries
to) destroy them. What is the proper way to destroy them?


Thanks,
NL




Sub pdfEraseExternalLowLevelBookmarks(sDocSectionNum As String,
sFullFileName As String)
'### 1-31-06
'On Error GoTo Problem

'### open the acrobat application
Dim acroApp As Acrobat.CAcroApp
Set acroApp = CreateObject("AcroExch.App", "")


'### open the pdf file
Dim pdDoc As Acrobat.CAcroPDDoc
Set pdDoc = CreateObject("AcroExch.PDDoc", "")

pdDoc.Open (sFullFileName)

'### get the book mark root
Dim oJSO As Object
Dim oBMR As Object
Set oJSO = pdDoc.GetJSObject
Set oBMR = oJSO.BookMarkRoot

Dim aTop() As Variant
aTop = oBMR.Children

Dim i As Integer
Dim s As String
Dim k As Integer
Dim m As Integer

Dim oBMTop As Object
Set oBMTop = aTop(0)

Dim aPart() As Variant
aPart = oBMTop.Children

Dim aSection() As Variant
Dim oBMCurrentPart As Object
Dim oBMCurrentSection As Object
Dim aSubsection() As Variant
Dim oBMCurrentSubsection As Object
Dim oBM As Object

'### step through each Part
For i = LBound(aPart) To UBound(aPart)
Set oBMCurrentPart = aPart(i)
If pdfBookmarkHasChildren(oBMCurrentPart) = True Then
aSection = oBMCurrentPart.Children
Else
GoTo SkipI
End If
'### step through each section
For k = LBound(aSection) To UBound(aSection)
Set oBMCurrentSection = aSection(k)
If Left(oBMCurrentSection.Name, 11) = sDocSectionNum Then GoTo
SkipK

'### step through each subsection (level 1) and delete them and
their children
If pdfBookmarkHasChildren(oBMCurrentSection) Then
aSubsection = oBMCurrentSection.Children
For m = LBound(aSubsection) To UBound(aSubsection)
Set oBM = aSubsection(m)

oBM.Remove
Next m
End If

SkipK:
Next k
SkipI:
Next i

Dim lTemp As Long
lTemp = pdDoc.Save(PDSaveFull, sFullFileName)
lTemp = pdDoc.Close
Set oJSO = Nothing
Set oBMR = Nothing
Set oBMCurrentPart = Nothing
Set oBMCurrentSection = Nothing
Set oBM = Nothing
Set oBMTop = Nothing

acroApp.CloseAllDocs
acroApp.Exit


Exit Sub
Problem:
MsgBox "Exception thrown by pdfEraseExternalLowLevelBookmarks()."
End Sub
 
W

Word Heretic

G'day "netloss" <[email protected]>,

At the end,

Set AcroApp = Nothing


Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


netloss reckoned:
 
N

netloss

I put the line

set acroApp = Nothing

immeditately after

acroApp.exit

and I still get the same error in the same location in my list of pdfs.
 
W

Word Heretic

G'day "netloss" <[email protected]>,

you need to do this with every object. Anything dimmed not as a
string, long, int etc is an object - destroy them all. Observe the
hierarchy, destroy children before parents.


Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


netloss reckoned:
 
N

netloss

Still having the same problem ...

1. I think I've set all objects to Nothing, in the proper order. I also
used Erase on the ararys.

2. for those files that DID get processed, I find that if I open them
and use SaveAs to save them (with the same name) their size is about
half of what it was before. How do I get this effect from code? I am
using this code to save files:

lTemp = pdDoc.Save(PDSaveFull + PDSaveCollectGarbage, sFullFileName)

Thanks, here is the full code with changes.
NL
============================


Sub pdfEraseExternalLowLevelBookmarks(sDocSectionNum As String,
sFullFileName As String)
'### 1-31-06
'On Error GoTo Problem

'### open the acrobat application
Dim acroApp As Acrobat.CAcroApp
Set acroApp = CreateObject("AcroExch.App", "")


'### open the pdf file
Dim pdDoc As Acrobat.CAcroPDDoc
Set pdDoc = CreateObject("AcroExch.PDDoc", "")

pdDoc.Open (sFullFileName)

'### get the book mark root
Dim oJSO As Object
Dim oBMR As Object
Set oJSO = pdDoc.GetJSObject
Set oBMR = oJSO.BookMarkRoot

Dim aTop() As Variant
aTop = oBMR.Children

Dim i As Integer
Dim s As String
Dim k As Integer
Dim m As Integer

Dim oBMTop As Object
Set oBMTop = aTop(0)

Dim aPart() As Variant
aPart = oBMTop.Children

Dim aSection() As Variant
Dim oBMCurrentPart As Object
Dim oBMCurrentSection As Object
Dim aSubsection() As Variant
Dim oBM As Object

'### step through each Part
For i = LBound(aPart) To UBound(aPart)
Set oBMCurrentPart = aPart(i)
If pdfBookmarkHasChildren(oBMCurrentPart) = True Then
aSection = oBMCurrentPart.Children
Else
GoTo SkipI
End If
'### step through each section
For k = LBound(aSection) To UBound(aSection)
Set oBMCurrentSection = aSection(k)
If Left(oBMCurrentSection.Name, 11) = sDocSectionNum Then GoTo
SkipK

'### step through each subsection (level 1) and delete them and
their children
If pdfBookmarkHasChildren(oBMCurrentSection) Then
aSubsection = oBMCurrentSection.Children
For m = LBound(aSubsection) To UBound(aSubsection)
Set oBM = aSubsection(m)

oBM.Remove
Next m
End If

SkipK:
Next k
SkipI:
Next i

Dim lTemp As Long
lTemp = pdDoc.Save(PDSaveFull + PDSaveCollectGarbage, sFullFileName)
lTemp = pdDoc.Close


Erase aTop
Erase aSubsection
Erase aSection
Erase aPart



Set oBM = Nothing
Set oBMCurrentSection = Nothing
Set oBMCurrentPart = Nothing

Set oBMTop = Nothing
Set oBMR = Nothing
Set oJSO = Nothing
Set pdDoc = Nothing

acroApp.CloseAllDocs
acroApp.Exit

Set acroApp = Nothing

Debug.Print "work complete"

Exit Sub
Problem:
MsgBox "Exception thrown by pdfEraseExternalLowLevelBookmarks()."
End Sub
 
W

Word Heretic

G'day "netloss" <[email protected]>,

pdDoc.SaveAs

That PDSaveCollectGarbage is a bloat suspect, maybe just getting rid
of that would help.

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


netloss reckoned:
 
N

netloss

OK, I'll try that. Any other ideas on the original problem i.e.Runtime
error '-2147417851(80010105)'
Automation Error
The server threw an exception


Thanks,
NL
 
W

Word Heretic

G'day "netloss" <[email protected]>,

Obvious suspect is blowing your buffer space, if you do are lots of
editing, you need a regular undoclear to flush the
not-as-quite-infinite-as-we-would-like undo buffer.

If you are doing insane amounts of editing or are trying for automated
pagination type stuff, you also need to regularly update the screen if
you have refresh turned off for speed.

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


netloss reckoned:
 
N

netloss

Thanks for your reply - I'm pretty new to trying to automate things
with pdfs. I guess I would have thought that quitting the application
would clear any undo buffers or any other memory that was getting used
by Acrobat.

I (try to) close acrobat after each file is processed. Each individual
file is 800k or so. But acrobat is apparently not closing ...

Do you mean regularily update the screen in Acrobat? The instance of
Acrobat is hidden, so is there still a 'screen' to update?

How would one do a 'undo clear' or otherwise Flush the Buffer from vba?

Thanks again,
NL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top