Solved

Convert Word doc to PDF

Posted on 2009-04-09
7
472 Views
Last Modified: 2012-05-06
Hello All-

I have code below that creates mailing labels in a Word document and saves the document.  How can I tweak my code to create a PDF document from the Word document?

Thanks,
'++++++++++++ for word

        Dim oApp As Microsoft.Office.Interop.Word.Application

        Dim oDoc As Microsoft.Office.Interop.Word.Document 'Word.Document
 

        'Start a new document in Word

        oApp = CreateObject("Word.Application")

        oDoc = oApp.Documents.Add
 

        With oDoc.MailMerge
 

            'Insert the mail merge fields temporarily so that

            'you can use the range that contains the merge fields as a layout

            'for your labels -- to use this as a layout, you can add it

            'as an AutoText entry.

            With .Fields

                .Add(oApp.Selection.Range, "owner_name")

                oApp.Selection.TypeParagraph()

                .Add(oApp.Selection.Range, "address")

                oApp.Selection.TypeParagraph()

                .Add(oApp.Selection.Range, "Mail_City")

                oApp.Selection.TypeText(",  ")

                .Add(oApp.Selection.Range, "Mail_State")

                oApp.Selection.TypeText("  ")

                .Add(oApp.Selection.Range, "Mail_Zip")

            End With

            Dim oAutoText As Microsoft.Office.Interop.Word.AutoTextEntry 'Word.AutoTextEntry

            oAutoText = oApp.NormalTemplate.AutoTextEntries.Add("MyLabelLayout", oDoc.Content)

            oDoc.Content.Delete() 'Merge fields in document no longer needed now

            'that the AutoText entry for the label layout

            'has been added so delete it.
 

            'Set up the mail merge type as mailing labels and use

            'a tab-delimited text file as the data source.

            .MainDocumentType = WdMailMergeMainDocType.wdMailingLabels 'wdMailingLabels

            .OpenDataSource(Name:="C:\LabelsFile.csv") 'Specify the data source here
 

            'Create the new document for the labels using the AutoText entry

            'you added -- 5160 is the label number to use for this sample.

            'You can specify the label number you want to use for the output

            'in the Name argument.

            oApp.MailingLabel.CreateNewDocument(Name:="5160", Address:="", _

                AutoText:="MyLabelLayout") 'LaserTray:=wdPrinterManualFeed)
 

            'Execute the mail merge to generate the labels.

            .Destination = WdMailMergeDestination.wdSendToNewDocument 'wdSendToNewDocument

            .Execute()
 

            'Delete the AutoText entry you added

            oAutoText.Delete()
 

        End With

 
 

        'Prevent save to Normal template when user exits Word

        oApp.NormalTemplate.Saved = True

        Dim thename As String = "MailWord"

        Dim laststring As String
 

        laststring = "C:\Inetpub\wwwroot\Test\DirectoryDump\" & thename & ".doc" '"C:\Inetpub\wwwroot\Test\DirectoryDump\" & thename & ".doc"
 

        Dim neededstring1 As String = GetUniqueFilename(laststring)

        oApp.ActiveDocument.SaveAs(neededstring1)

Open in new window

0
Comment
Question by:JoshinHtown
  • 3
  • 3
7 Comments
 
LVL 19

Expert Comment

by:dlc110161
ID: 24111924
What PDF Maker are you using? Acrobat?

Dawn Bleuel
Word MVP
0
 

Author Comment

by:JoshinHtown
ID: 24112035
Yeah I would assume so but what do you think would be the best option? I have a machine that just has MS Word and my Web Application.  It creates the Word document just fine.  If I install Acrobat reader or something along those lines will I have the ability to call a PDF printing object?  let me know your thoughts.

Regards,
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 24112655
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:JoshinHtown
ID: 24115145
Thanks emoreau but I'd rather not pay for a product to do this.  If its possible to accomplish this using what software I already have (mentioned above) and/or adding an acrobat product then I could be fine with that.

Suggestions anyone?  I'm sure there is a way to add a few lines of code to my above code that will convert that Word document to a PDF.  Thoughts?

Regards,
0
 
LVL 19

Accepted Solution

by:
dlc110161 earned 250 total points
ID: 24115461
Unfortunately you need a PDF writer to convert to PDF. There are several free options available to choose. See http://www.experts-exchange.com/Web_Development/Document_Imaging/Adobe_Acrobat/Q_23103347.html for come code using Acrobat.

Dawn Bleuel
Word MVP
0
 

Author Comment

by:JoshinHtown
ID: 24115551
Hi Dawn-

Based on your last response, if I wanted to use the below code where do I get the PDF writer or distiller?  Is it a free download?  Would I only need to install pdf distiller and then it is completely useable with the below code?

Thanks, this might be getting close..

Regards,
Private Sub Command1_Click()

Dim wrd As New Word.Application

Dim doc As Word.Document

wrd.Visible = False

wrd.ScreenUpdating = False

wrd.ActivePrinter = "Acrobat Distiller"

'Open the document

Set doc = wrd.Documents.Open("c:\worddoc.doc")

'Print it to a PostScript file using Distiller

doc.PrintOut False, False, , "c:\test.ps", , , , , , , True

doc.Close False

wrd.Quit False

Set doc = Nothing

Set wrd = Nothing

'Now call distiller to convert the postscript file to PDF

Dim acr As New ACRODISTXLib.PdfDistiller  //ERROR

acr.bShowWindow = False

acr.FileToPDF "c:\test.ps", "pdfdoc.pdf", ""

Set acr = Nothing

'Delete the old PS file

Kill "c:\test.ps"

'Done

 

End Sub

 

Open in new window

0
 
LVL 19

Expert Comment

by:dlc110161
ID: 24116005
CutePDF is a very popular free PDF creator. I've also heard good things about PDF995.

Dawn Bleuel
Word MVP
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now