View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Matthew Dyer Matthew Dyer is offline
external usenet poster
 
Posts: 178
Default Running Word to Save as PDF

I am obviously doing something wrong... I am trying desperately to avoid having to activate the Microsoft Word XXXX Object Library as this may possibly be used by others in the future and you know trying to describe how to activate references can be difficult...
The error occurs in the .ExportAsFixedFormat line, and I dunno what's wrong.. I recorded a save-do-pdf event from word itself and those are the lines I was given...
I'm running office 2003 on Win7 Pro also.



Sub DocToPDF()
Dim WordObject As Object
Dim WordDoc As Object
Dim ws As Worksheet
Dim path As String
Dim fname As String, filename2 As String, name As String, ext As String
Set WordObject = CreateObject("word.application")
WordObject.Visible = False

Set ws = ActiveSheet
path = ws.Range("a1").Value
If Right(path, 1) < "\" Then
path = path & "\"
End If
For i = 5 To ws.UsedRange.Rows.count
name = ws.Range("b" & i).Value
ext = Mid(name, InStr(name, "."), Len(name) - InStr(name, ".") + 1)
fname = path & name
filename2 = Replace(fname, ext, ".pdf")
Set WordDoc = WordObject.documents.Add(fname)
With WordDoc
.ExportAsFixedFormat OutputFileName:=filename2, ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False
.Close
End With
Next i
Set WordObject = Nothing
Set WordDoc = Nothing
End Sub