Macro to convert current doc to PDF

D

Dan B

Hi All.

I need to write a Macro that will convert the current active document to PDF
(to a specific location / file name). I've had a look around the forums and
seem to get anything to work.

This will eventually become part of a larger script that will batch convert
bunch of Word docs to PDF (opening each one, running another Macro, saving to
PDF, closing the Word doc and then moving on to the next).

If anybody has any suggestions for the rest of this process I would welcome
them. or now, my main concern is the conversion to PDF part.

I have Acrobat 8 installed and I'm running Word 2003.

Cheers!

Dan.
 
D

Dan B

Apologies for the typos, let's try again.....

....

Hi All.

I need to write a Macro that will convert the current active document to PDF
(to a specific location / file name). I've had a look around the forums and
can't seem to get anything to work.

This will eventually become part of a larger script that will batch convert
a bunch of Word docs to PDF (opening each one, running another Macro, saving
to PDF, closing the Word doc and then moving on to the next).

If anybody has any suggestions for the rest of this process I would welcome
them. For now, my main concern is the conversion to PDF part.

I have Acrobat 8 installed and I'm running Word 2003.

Cheers!

Dan.

....
 
G

Graham Mayor

You can setup the location to store the files from the Adobe driver - for
the rest:

Sub BatchPrintPDF()
On Error GoTo err_FolderContents
Dim FirstLoop As Boolean
Dim DocList As String
Dim DocDir As String
Dim sPrinter As String
Dim fDialog As FileDialog
Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)

With fDialog
.Title = "Select Folder containing the documents to be printed to PDF
and click OK"
.AllowMultiSelect = False
.InitialView = msoFileDialogViewList
If .Show <> -1 Then
MsgBox "Cancelled By User"
Exit Sub
End If
DocDir = fDialog.SelectedItems.Item(1)
If Right(DocDir, 1) <> "\" Then DocDir = DocDir + "\"
End With

If Documents.Count > 0 Then
Documents.Close Savechanges:=wdPromptToSaveChanges
End If
Application.ScreenUpdating = False
FirstLoop = True
DocList = Dir$(DocDir & "*.doc")
Do While DocList <> ""
Documents.Open DocList
With Dialogs(wdDialogFilePrintSetup)
sPrinter = .Printer
.Printer = "Adobe PDF"
.DoNotSetAsSysDefault = True
.Execute
End With
ActiveDocument.PrintOut
ActivePrinter = sPrinter
ActiveDocument.Close Savechanges:=wdDoNotSaveChanges
DocList = Dir$()
FirstLoop = False
Loop
Application.ScreenUpdating = True
ActivePrinter = sPrinter
Exit Sub
err_FolderContents:
MsgBox Err.Description
Exit Sub
ActivePrinter = sPrinter
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

Dan B

Thanks for your reply Graham but I can't seem to get the conversion to happen.

The folder selection dialogue appears and I select a folder containing 3
different word docs. I hit Ok and nothing happens (apart from it closing the
current active word doc).

Any ideas what I'm doing wrong?

Cheers,

Dan.
 
G

Graham Mayor

It works for me? This was a straight lift from my own PC.
Do you have the required printer driver called in the line
..Printer = "Adobe PDF" ?
Can you print normally to that driver?
Try stepping through the code and see where it fails.
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

Dan B

Ah, got it working now - there was a small problem with where the macro was
being stored.

Is there any way to automatically save the PDF with the same name as each
Word doc? It would be nice to be able to hit 'Convert' and leave it running
(and not have to provide a name and hit OK per doc).

Also, how would I run another Macro in between opening and converting each
doc?

Really appreciate the help - thank you, thank you, thank you...

Cheers,

Dan.
 
G

Graham Mayor

Again you can set that in the driver properties - see
http://www.gmayor.com/individual_merge_letters.htm

As for the other macro, without knowing what that macro does, you might
include its functionality where indicated

DocList = Dir$(DocDir & "*.doc")
Do While DocList <> ""
Documents.Open DocList

'do your other macro stuff here

With Dialogs(wdDialogFilePrintSetup)
sPrinter = .Printer

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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