I there any way to add a macro programmatically to a doc(Word) document and then save it?
Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application(); newApp.Visible = false; object Unknown = Type.Missing; var Source = filePath; var doc = newApp.Documents.Open(Source); var project = doc.VBProject; var module = project.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule); var macro = @" Public Sub DoKbTest() MsgBox ""Hello"" End Sub Public Sub DoKbTestWithParameter(ByVal msg As String) MsgBox msg End Sub "; module.CodeModule.AddFromString(macro); //module.CodeModule.AddFromFile(vba); newApp.Run("DoKbTest"); doc.SaveAs(finaldoc);
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)