View Single Post
 
Old 04-30-2017, 04:36 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Did you check which line was causing the error? Doubtless you'd get one with:
With MSWord.Selection
since MSWord in your code is the application, not the document.

And yes, the SaveAs2 method can produce PDFs. There are plenty of examples of that in these forums.

Try:
Code:
Private Sub CommandButton1_Click()
    Dim wdApp As Object, wdDoc As Object
    Const docName As String = "D:\WordTest.doc"
    Const pdfName As String = "D:\WordTest.pdf"
    Set wdApp = CreateObject("Word.Application")
    wdApp.Visible = True
    Set wdDoc = wdApp.Documents.Add
    With wdDoc
        .Font.Name = "Helvetica"
        .Font.Size = 20
        .Font.Color = wdColorRed
        .Font.Bold = True
        .Font.Italic = True
        .TypeText Text:="Text"
        .TypeParagraph
        'SaveAs2 Parameters: FileName, FileFormat, LockComments, Password, AddToRecentFiles, & _
            WritePassword, ReadOnlyRecommended, EmbedTrueTypeFonts, SaveNativePictureFormat, & _
            SaveFormsData, SaveAsAOCELetter, Encoding, InsertLineBreaks, AllowSubstitutions, & _
            LineEnding, AddBiDiMarks, CompatibilityMode
        .SaveAs2 docName, 0, , , False    '0 = wdFormatDocument
        .SaveAs2 pdfName, 17, , , False ' 17 = wdFormatPDF
        .Close False
    End With
    wdApp.Quit
    Set wdDoc = Nothing: Set wdApp = Nothing
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote