Click here to Skip to main content
15,885,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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")
Posted
Updated 30-May-20 3:22am
v4

 
Share this answer
 
Comments
Member 14842313 30-May-20 5:56am    
i love you guys, can it also possible on VB.NET
phil.o 30-May-20 6:03am    
Here's the snippet translated to VB.NET:
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
Member 14842313 30-May-20 6:10am    
im getting error, here : Dim word = new Word With {.Visible = True

}


i tried this :
Dim word = New Word With & Visible = True

Still ihave this error :
https://i.ibb.co/Mn9BgjX/Untitled.png
phil.o 30-May-20 6:27am    
I corrected the snippet.
Member 14842313 30-May-20 8:02am    
thank u
Yes, it is possible. You need to use the Microsoft.Office.Interop.Word Namespace | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900