MS Word from VB.NET

0

I have the following form with code:

Imports Word = Microsoft.Office.Interop.Word 
Public Class Form1
    **Dim oWord as New Word.Application()** 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim oDoc As Word.Document = oWord.Documents.Add()
        dim oParas = oDoc.Paragraphs 
        dim oPara = oParas.Add() 
        dim oParaRng = oPara.Range 
        oParaRng.Text = "Heading 1" 
        dim oFont = oParaRng.Font 
        oFont.Bold = 1 
        oParaRng.InsertParagraphAfter() 

        dim oBookmarkRng = oDoc.Bookmarks.Item("\endofdoc").Range 


        Dim oTable = oDoc.Tables.Add(oBookmarkRng, 5, 2) 
        oTable.Range.ParagraphFormat.SpaceAfter = 6 


        For r As Integer = 1 To 5 
            For c As Integer = 1 To 2 
                oTable.Cell(r, c).Range.Text = "r" & r & "c" & c 
            Next 
        Next 

        ' Change width of columns 1 & 2 
        oTable.Columns(1).Width = oWord.InchesToPoints(2) 
        oTable.Columns(2).Width = oWord.InchesToPoints(3) 
        oDoc.Close() 
        oWord.Quit(False) 

    End Sub
End Class

Copied from the internet, since I have tried different ways to make the connection and in all the tested I get the error (On the line marked with ** or in bold letters) below:

  

An exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll but was not controlled in the user's code

     

Additional information: The COM class generator for the component with CLSID {000209FF-0000-0000-C000-000000000046} could not be retrieved due to the following error: 80080005

     

Error in server execution (HRESULT exception: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).

In other cases (Testing) it tells me that it can not create the activex control.

What can be wrong, or what I have not done to make this work?

My environment is: Windows 10 64 Bits. Visual Studio 2015 and Office 2013

    
asked by Maximiliano Gil Toro 26.01.2017 в 17:09
source

2 answers

1

Dear the only change is the following line, try it, if you still get an error verifies that your Word is licensed, so it does not show you the window with red title license.

'Dim oWord As New Word.Application()

for

Dim oWord = CreateObject("Word.Application")

Since personally it was the error that vb showed me.

Greetings.

    
answered by 08.06.2017 в 22:00
0

Try to initialize your variable oWord in the following way:

Dim oWord As Object
oWord = New Microsoft.Office.Interop.Word.Application
    
answered by 26.01.2017 в 17:20