Dear all, I am facing a strange problem with Excel.
I created an excel file (let's call it File A) to see if the following code was working
and everything was working ok.
I then created a copy of this file, renamed it (let's call this new file as File B) and added forms, additional Sheets etc... but, to my surprise, the code was no longer working: I am now receiving "Run-time error '448': Named argument not found". The error points to the line
Could someone be so kind to explain how it is possible that the same code was working (and is still working) for FileA, but not for FileB ?
Thank you in advance for your help!
Best,
Silvano
I created an excel file (let's call it File A) to see if the following code was working
VBA Code:
Const wdReplaceAll = 2
Sub Test()
Dim wApp As Object
Dim wDoc As Object
Dim LastR As Long
Dim ST, RT As Variant
Dim R As Integer
Dim fiName, vers, utente As Variant
LastR = Sheets("Fields").Cells(Sheets("Fields").Rows.Count, "A").End(xlUp).Row
fiName = Sheets("Main").Cells(2, 4)
vers = Sheets("Main").Cells(3, 4)
utente = Sheets("Main").Cells(5, 4)
On Error Resume Next
Set wApp = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set wApp = CreateObject("Word.Application")
End If
On Error GoTo 0
Temp = "C:\Users\" & utente & "\Downloads\"
Set wDoc = wApp.Documents.Open("C:\Users\" & utente & "\Downloads\Specs.docx", ReadOnly = True)
If wDoc Is Nothing Then
MsgBox "Could not find the template file at:" & "C:\Users\" & utente & "\Downloads\Specs.docx"
Exit Sub
End If
On Error Resume Next
wDoc.SaveAs Temp & "\" & fiName & "_" & vers & ".docx"
On Error GoTo 0
If wDoc Is Nothing Then
MsgBox "Could not save the word file at: " & Temp & "\" & fiName & "_" & vers & ".docx"
Exit Sub
End If
For R = 27 To LastR
ST = Sheets("Fields").Cells(R, 1)
RT = Sheets("Fields").Cells(R, 2)
wDoc.Content.Find.Execute Wrap:=1, FindValue:=ST, ReplaceWith:=RT, Replace:=wdReplaceAll
didSome = wDoc.Content.Find.Found
Next R
wDoc.Save
wDoc.Close
Set wApp = Nothing
MsgBox ("The file " & fiName & "_" & vers & ".docx was successfully created in C:\Users\" & utente & "\Downloads\")
End Sub
and everything was working ok.
I then created a copy of this file, renamed it (let's call this new file as File B) and added forms, additional Sheets etc... but, to my surprise, the code was no longer working: I am now receiving "Run-time error '448': Named argument not found". The error points to the line
VBA Code:
wDoc.Content.Find.Execute Wrap:=1, FindValue:=ST, ReplaceWith:=RT, Replace:=wdReplaceAll
Could someone be so kind to explain how it is possible that the same code was working (and is still working) for FileA, but not for FileB ?
Thank you in advance for your help!
Best,
Silvano