Error creating a Word document

0

I am trying to create a Word document from Ms Visual Basic 2010 by means of a button on a form, I have already added the reference of Microsoft Word 15.0 Object Library to the project, the Word document is created successfully and saves it, but after doing that shows me the following error.

I attach my code:

Imports Microsoft.Office.Interop

Public Class Form1

    Private Sub btncrear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncrear.Click

        'Declara la variable.
        Dim objWD As Word.Application
        'Crea una nueva instancia de Word
        objWD = CreateObject("Word.Application")
        'Agrega un nuevo documento en blanco
        objWD.Documents.Add()
        'Agrega Texto.
        objWD.Selection.TypeText("Hola mundo")
        'Guarda el documento
        objWD.ActiveDocument.SaveAs(FileName:="cotizacion.doc")
        objWD = objWD.Documents.Open(FileName:="cotizacion.doc")
    End Sub
End Class
    
asked by Kevin Burbano 23.01.2018 в 03:42
source

1 answer

0

The message is clear, you are trying to convert one type to another incorrectly. Your variable objWD is of type Word.Application , when you open a document it is necessary to define another variable of type Document .

For example:

Dim archivoWord As Word.Document 
archivoWord = objWD.Documents.Open(FileName:="cotizacion.doc")

or

Dim archivoWord = objWD.Documents.Open(FileName:="cotizacion.doc")
    
answered by 23.01.2018 / 04:16
source