question

PeterBirch-9052 avatar image
0 Votes"
PeterBirch-9052 asked

Problems with VSTO and Visual Studio

VSTO and Visual Studio
When I use VSTO with visual studio, I have no trouble in interfacing with Excel 2013. However when I try to interface with Word 2013, I get the following error:

System.InvalidCastException

HResult=0x80004002

Message=Unable to cast COM object of type 'Microsoft.Office.Interop.Word.ApplicationClass' to interface type 'Microsoft.Office.Interop.Word._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00020970-0000-0000-C000-000000000046}' failed due to the following error: Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED)).

Source=mscorlib

StackTrace:

at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease)

at Microsoft.Office.Interop.Word.ApplicationClass.set_Visible(Boolean prop)

at Page067.Program067.Main(String[] args) in E:\Visual Studio 2013\Projects\VSTOBook2007\Page067\Program067.cs:line 15

This is the code which causes the problem.

using System;

using System.Collections.Generic;

using System.IO;

using Word = Microsoft.Office.Interop.Word;

namespace Page067

{

 class Program067

 {

     static void Main(string[] args)

     {

         TextReader reader = new StreamReader(args[0]);

         Word.Application wordApp = new Word.Application();

         wordApp.Visible = true;

         object missing = System.Type.Missing;

         Word.Document doc = wordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);

         string[] separators = new string[1];

         separators[0] = "||";

         int rowCount = 0;

         int columnCount = 0;

         //Read rows and calculate number of rows and columns

         List<string> rowList = new List<string>();

         string row = reader.ReadLine();

         while (row != null)

         {

             rowCount++;

             rowList.Add(row);

             //If this is the first row, calculate the number of rows

             if (rowCount == 1)

             {

                 string[] splitHeaderRow = row.Split(separators, StringSplitOptions.None);

                 //Ignore the first and last separator

                 columnCount = splitHeaderRow.Length - 2;

             }

             row = reader.ReadLine();

         }

         //Create a table

         Word.Range range = doc.Content;

         Word.Table table = range.Tables.Add(range, rowCount, columnCount, ref  missing, ref missing);

         //Populate table

         int columnIndex = 1;

         int rowIndex = 1;

         foreach (string r in rowList)

         {

             string[] splitRow = r.Split(separators, StringSplitOptions.None);

             for (columnIndex = 1; columnIndex <= columnCount; columnIndex++)

             {

                 Word.Cell cell = table.Cell(rowIndex, columnIndex);

                 cell.Range.Text = splitRow[columnIndex];

             }

             rowIndex++;

         }

         //Format Table

         table.Rows[1].Range.Bold = 1;

         table.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);

         //Wait for input from the command line before exiting

         Console.WriteLine("Table complete");

         Console.ReadKey();

         //Quit without saving changes

         object SaveChanges = false;

         wordApp.Quit(ref SaveChanges, ref missing, ref missing);

     }// end class Program067

 }

}// end namespace Page067

The line wordApp.Visible = true; is where the error occurs.

I have done the same thing with an Excel example; but without any errors.

I have scoured the internet for a solution and have tried everything suggested, including completely removing and reinstalling Visual Studio 2022 Community edition and Office 2013 Professional Plus. I would be most grateful for advice on how to solve this problem. Thanks in anticipation.

Peter

office-vsto-com-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

0 Answers