I have been using some VBA for quite a few years to help me replace names and dates etc in some word docs. Since I moved from Office 2016 to 365, I have been getting an error. I am by no means a VBA expert and I remember a lot of trial and error was used to get the process working. I start a macro that opens a user form. I fill out the user form and then that data replaces placeholder data as I open up word templates in a folder that I choose. The docs in that folder are opened one after the other and a find and replace takes place.
When I open Word and do this the first time, I get an error on the code in Bold below.
If I hit debug and then in the VB window play the process finishes, when the next template doc opens, I get the same error.
The strange thing is, that if I don't close Word and then start the whole process again by opening the userform, the code runs flawlessly. If I close Word and then reopen it, I will get the error on the first run through again. It is quite bizarre to me.
Any ideas on what is causing this?
Here is the Userform code
When I open Word and do this the first time, I get an error on the code in Bold below.
Code:
Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=True)
With wdDoc '''''This is the line giving me grief
The strange thing is, that if I don't close Word and then start the whole process again by opening the userform, the code runs flawlessly. If I close Word and then reopen it, I will get the error on the first run through again. It is quite bizarre to me.
Any ideas on what is causing this?
VBA Code:
ub UpdateDocuments()
''''Find files in a folder. Opens, changes then saves them in same location
Application.ScreenUpdating = True
Dim strFolder As String, strFile As String, wdDoc As Document
Dim dNewname As String, dNewfolder As String
On Error Resume Next
'''Calls the Folder Dialog then sets the value
strFolder = GetFolder
Dim diaFolder As FileDialog
Dim Fname As String
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.docx", vbNormal)
'''Starts the loop
While strFile <> ""
[B]
Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=True)
With wdDoc '''''This is the line giving me grief[/B]
FindReplace
'RemoveAllHighlights
'.Close SaveChanges:=True
dNewfolder = strFolder & "\" & UserForm1.BusinessTextBox.Value
dNewname = dNewfolder & "\" & UserForm1.BusinessTextBox.Value & " " & strFile
On Error Resume Next
MkDir dNewfolder
On Error GoTo 0
ActiveDocument.SaveAs dNewname
'MsgBox dNewname
'.Close SaveChanges:=True
End With
strFile = Dir()
Wend
Here is the Userform code
VBA Code:
Private Sub Label1_Click()
End Sub
Private Sub Label2_Click()
End Sub
Private Sub Label3_Click()
End Sub
Private Sub UserForm_Initialize()
''''This opens the userform, clears all text boxes then populates current date and the year of next financial year end.
Dim sThisFinancialYear As String: sThisFinancialYear = IIf(Month(Date) <= 6, Year(Date), Year(Date) + 1) '''determines the next financial year end date year
Me.BusinessTextBox.Value = ""
Me.CustomerTextBox.Value = ""
Me.DateTextBox.Value = ""
Me.YearTextBox.Value = ""
DateTextBox.Value = Format(Now, "mmmm dd, yyyy") '''current date.
YearTextBox.Value = sThisFinancialYear 'populates the next financial year end year text box.
End Sub
Private Sub OKButton_Click()
''''This is the find and replace routine for the four items in the userform textboxes.
If Trim(UserForm1.BusinessTextBox.Value) = "" Then Exit Sub
If Trim(UserForm1.CustomerTextBox.Value) = "" Then Exit Sub
If Trim(UserForm1.DateTextBox.Value) = "" Then Exit Sub
If Trim(UserForm1.YearTextBox.Value) = "" Then Exit Sub
'Set uBus = UserForm1.BusinessTextBox.Text
'Unload Me
UpdateDocuments
''''These if commands use trim to make sure there is no space at the start or end of the text in the textbox. If the text box is blank, the find and replace routine will fail.
''''All the text boxes in the userform must have data for this routine to run.
''''This calls the routine to remove any highlights found anywhere in the document.
'''''The closes the userform.
Unload Me
End Sub
Private Sub CancelButton_Click()
Unload Me
End Sub
Sub ClearButton_Click()
CustomerTextBox.Value = ""
BusinessTextBox.Value = ""
End Sub
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
Last edited: