It's still not updating the right cell, actually.
In case it matters: These tables have two columns and four rows.
There may be some edge cases where the number of rows varies, so I would rather not key the replacement to the exact cell, but that could be worked out if necessary.
Here's what I'm using:
Code:
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
Set wdDoc = Documents.Open(FileName:=strFolder & "" & strFile, AddToRecentFiles:=False, Visible:=False)
With wdDoc
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.MatchWildcards = True
.Text = "[PR][eu][bv][ils]{3}[ehno]{2}[ Ndo]{1,3}."
.Replacement.Text = ""
.Forward = True
.Format = False
.Wrap = wdFindStop
End With
Do While .Find.Execute
If .Information(wdWithInTable) = True Then
Select Case .Text
Case "Revision No."
.Cells(1).Next.Range.Text = "2.5"
Case "Published"
If .Cells(1).ColumnIndex = 1 Then
.Cells(1).Next.Range.Text = "2 April 2021"
End If
End Select
End If
.Collapse wdCollapseEnd
Loop
End With
Word_ExportPDF
.SaveAs FileName:=Split(.FullName, ".doc")(0) & ".pdf", FileFormat:=wdFormatPDF, AddToRecentFiles:=False
.Close SaveChanges:=True
End With
strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
Sub Word_ExportPDF()
'PURPOSE: Generate A PDF Document From Current Word Document
'NOTES: PDF Will Be Saved To Same Folder As Word Document File
'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault
Dim CurrentFolder As String
Dim FileName As String
Dim myPath As String
Dim UniqueName As Boolean
UniqueName = False
'Store Information About Word File
myPath = ActiveDocument.FullName
CurrentFolder = ActiveDocument.Path & ""
FileName = Mid(myPath, InStrRev(myPath, "") + 1, _
InStrRev(myPath, ".") - InStrRev(myPath, "") - 1)
'Does File Already Exist?
'If so, too bad
' Do While UniqueName = False
' DirFile = CurrentFolder & FileName & ".pdf"
' If Len(Dir(DirFile)) <> 0 Then
' UserAnswer = MsgBox("File Already Exists! Click " & _
' "[Yes] to override. Click [No] to Rename.", vbYesNoCancel)
' If UserAnswer = vbYes Then
UniqueName = True
' ElseIf UserAnswer = vbNo Then
' Do
' 'Retrieve New File Name
' FileName = InputBox("Provide New File Name " & _
' "(will ask again if you provide an invalid file name)", _
' "Enter File Name", FileName)
'Exit if User Wants To
' If FileName = "False" Or FileName = "" Then Exit Sub
' Loop While ValidFileName(FileName) = False
' Else
' Exit Sub 'Cancel
' End If
' Else
' UniqueName = True
' End If
' Loop
'Save As PDF Document
On Error GoTo ProblemSaving
ActiveDocument.ExportAsFixedFormat _
OutputFileName:=CurrentFolder & FileName & ".pdf", _
ExportFormat:=wdExportFormatPDF
On Error GoTo 0
'Confirm Save To User
With ActiveDocument
FolderName = Mid(.Path, InStrRev(.Path, "") + 1, Len(.Path) - InStrRev(.Path, ""))
End With
' MsgBox "PDF Saved in the Folder: " & FolderName
Exit Sub
'Error Handlers
ProblemSaving:
MsgBox "There was a problem saving your PDF. This is most commonly caused" & _
" by the original PDF file already being open."
Exit Sub
End Sub