Maybe something like the following in place of the example test macro. Change as appropriate.
Sub SaveDoc() Dim sFolder As String Dim sPath As String, sName As String Dim wdApp As Object Dim oRng As Object Dim wb As Excel.Workbook Dim xlName As Excel.Name Set wb = ActiveWorkbook sFolder = CleanFilename(Range("G3")) If Not sFolder = "" Then On Error Resume Next Set wdApp = GetObject(, "Word.Application") If Err Then Set wdApp = CreateObject("Word.Application") End If On Error GoTo 0 Set docword = wdApp.Documents.Add(wb.path & "\zapisnikKP.dot") 'Loop through names in the activeworkbook For Each xlName In wb.Names 'if xlName's name is existing in document then put the value in place of the bookmark If docword.Bookmarks.Exists(xlName.Name) Then Set oRng = docword.Bookmarks(xlName.Name).Range oRng.Text = Range(xlName.value) oRng.Bookmarks.Add xlName.Name End If Next xlName sPath = CreateFolders("C:\Test\" & sFolder & "\") sName = CleanFilename(Range("A1")) 'The cell with the document name docword.SaveAs2 sPath & sName & ".docx" Else MsgBox "The cell G3 content is invalid", vbCritical Exit Sub End If End Sub




Reply With Quote