VBA - Open Word Save As PDF

NewbieAU

New Member
Joined
Oct 25, 2021
Messages
6
Office Version
  1. 2019
Platform
  1. Windows
To all the forum members and excel gurus! I hope someone can help.

I'm trying to put together something to try and convert Word to PDF.

The file path in B1 and I referenced it as the path in the code.
The files I am trying to open are rather similar, all starting with "Doc" and then the yyyymmdd (I can change this when needed), then another dot (".") and then a string of numbers (which I have listed in Column D).
I list the "new" filenames in column E. What I want to do is get Word to open those files, format it, then save as a PDF.

This is what I have so far. I can get the first document in row 2 to open and format but cannot do anything further. Where have I gone wrong?

VBA Code:
Sub WordToPDF()
    Dim n As Long
    Dim strPath As String
    Dim TheFile As String
    Dim PathFile As String
    Dim NewFile As String
    Dim objWord
    Dim objDoc
    Dim objSelection
    
    strPath = Sheets("Sheet1").Range("B1")

    For n = 2 To 10
        TheFile = "Doc20211025." & Sheets("Sheet1").Cells(n, 4) & ".docx"
        PathFile = strPath & TheFile
        NewFile = Sheets("Sheet1").Cells(n, 5)

        Set objWord = CreateObject("Word.Application")
        Set objDoc = objWord.Documents.Open(PathFile)

        objWord.Visible = True

        Set objSelection = objWord.Selection
    
        objSelection.WholeStory

        With objSelection
            .Font.Name = "Arial"
            .Font.Size = 10
        End With
         
        On Error GoTo 0
            ActiveDocument.ExportAsFixedFormat _
                OutputFileName:=strPath & NewFile & ".pdf", _
                ExportFormat:=wdExportFormatPDF
        On Error GoTo 0
        
    Next
  
End Sub

Also, in the event I put in the wrong number (and the file does not exist), I want to put a message in the corresponding F cell so I can see it when the macro finishes running. I don't know how to error this.

Can someone help me?
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
The documents are named:
Doc20211025.135352.docx
Doc20211025.117854.docx
Doc20211025.209892.docx
Doc20211025.108391.docx
Doc20211025.291356.docx
Doc20211025.641309.docx

In column D, I have the following numbers which are the inside bit:

135352
117854
209892
108391
291356
641309

The code is picking up the first file, just stopping at the saving bit. So I think it's picking up the files just fine.
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,536
Members
449,037
Latest member
tmmotairi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top