I have a word document named "Template" in the same path of thisworkbook.path
How can I copy this Template word document and name it with a cell value (say Z1 cell) then replace the string ##NAME## with cell A2?
I have this code but it works on an existing document
Code: Select all
Sub Test()
Dim objWd As Object
Dim objDoc As Object
Dim strName As String
With Application.FileDialog(1)
.Filters.Clear
.Filters.Add "Word Documents", "*.doc*"
.InitialFileName = ThisWorkbook.Path & "\*.doc*"
If .Show Then
strName = .SelectedItems(1)
Else
Beep
Exit Sub
End If
End With
On Error Resume Next
Set objWd = GetObject(Class:="Word.Application")
If objWd Is Nothing Then Set objWd = CreateObject(Class:="Word.Application")
On Error GoTo 0
objWd.Visible = True
Set objDoc = objWd.Documents.Open(strName)
With ThisWorkbook.Worksheets("INF")
objDoc.Content.Find.Execute findText:="XXXXX", ReplaceWith:=.Range("B3").Value, MatchWholeWord:=False, Replace:=2
objDoc.Content.Find.Execute findText:="YYYYY", ReplaceWith:=.Range("B5").Value, MatchWholeWord:=False, Replace:=2
End With
Rem objDoc.Close SaveChanges:=True
End Sub