VBA works in Word 2016 and throws error in Word 2019 and 365

Stan101

New Member
Joined
Sep 2, 2016
Messages
26
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.
Code:
Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=True)
  With wdDoc '''''This is the line giving me grief
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?

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:

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
You commented out the close doc. I'm not that good with Word VBa but I think you still need to close each doc after saving it. I'm guessing that closing the doc and setting the wddoc to nothing before trying to set it to another document in the loop might fix the problem. HTH. Dave
 
Upvote 0
It also may help to tell us the actual error message. ;)
 
Upvote 0
My apologies for not getting back on this. I'm sorry that I was "that" guy who posted and didn't respond.

I found my issue. The blanks word doc that I have open needs to be saved before I run the code and then there is no issue.
 
Upvote 0
Solution

Forum statistics

Threads
1,224,812
Messages
6,181,084
Members
453,021
Latest member
Justyna P

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