Okay i think everything looks good, i writed below code to button, when i click the button, word opens, but im getting error, and nothing on macro, here : https://i.ibb.co/KKZh2j2/Untitled.png im getting, Programmatic access to Visual Basic Project is not trusted What I have tried: <pre> Dim file1 As String Dim word = New Word word.Visible = True Dim doc = word.Documents.Open("C:\Users\wdqdwq\Desktop\file1.docx") Dim project = doc.VBProject Dim [module] = project.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule) Dim script = String.Format("Sub MacroTest() " & "Const path1 As String = ""{0}"" " & "Set doc1 = Documents.Open(path1) " & "End Sub", file1) [module].CodeModule.AddFromString(script) word.Run("MacroTest")
Imports System.Runtime.InteropServices Imports Microsoft.Vbe.Interop Imports Word = Microsoft.Office.Interop.Word.Application Private Sub TheMethod() Dim file1, file2 As String Dim word = New Word word.Visible = True Dim doc = word.Documents.Open(file1) Dim project = doc.VBProject Dim [module] = project.VBComponents.Add(vbext_ComponentType.vbext_ct_StdModule) Dim script = String.Format("Sub MacroTest() " & _ "Const path1 As String = ""{0}"" " & _ "Set doc1 = Documents.Open(path1) " & _ "End Sub", file2) [module].CodeModule.AddFromString(script) word.Run("MacroTest") End Sub
&
& VbCrLf &
pre
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)