none
.Net VB using vbProj.References.AddFromFile to add reference to Word VBA project throws an error :'Requested type library or wizard is not a VBA project' RRS feed

  • Question

  • Hi All,

    I'm writing a .Net console app to convert .dot word files to .dotm. I'm not using the wizards because they don't keep the macros.

    I can get the files saved as .dotm files and keep the macros intact, but I need to change the references in the doc templates to point at the new .dotm files.

    I can iterate through the references and find the entries I want to remove, and remove them no issue. But whenever I try to add a reference to the new .dotm files I get an error stating: 'Requested type library or wizard is not a VBA project'. If I open word and manually add the same reference there is no issue.

    Here is the code I'm using:

            Dim objWord As Word.Application
            Dim objDoc As Word.Document
            Dim firstFullPath As String
            Dim filename As String

            objWord = CreateObject("Word.Application")

            Dim ResultPath As String = (String.Concat(ConfigurationManager.AppSettings.Get("Path"), "\Result\"))

            Dim diResult As New IO.DirectoryInfo(ResultPath)

            Dim aryFi As IO.FileInfo() = diResult.GetFiles("*.dotm")
            Dim HadErr As Boolean
            Dim fri As FileInfo

            For Each fri In aryFi
                Try
                    objDoc = objWord.Documents.Open(fri.FullName)
                    Console.WriteLine()
                    Console.WriteLine("-----------------------------------------------------------------------")
                    Console.WriteLine(fri.FullName)
                    objWord.Visible = False

                    Dim vbProj As VBProject
                    Dim chkRef As Reference

                    vbProj = objWord.ActiveDocument.VBProject

                    For Each chkRef In vbProj.References
                        Try
                            Console.WriteLine(chkRef.Name)
                            If chkRef.Name.Contains(".DOT") Or chkRef.Name.Contains(".dot") Then

                                Console.WriteLine(String.Concat(chkRef.FullPath.Replace(".DOT", ".dot"), "m"))
                                vbProj.References.AddFromFile(String.Concat(chkRef.FullPath.Replace(".DOT", ".dot"), "m"))
                                vbProj.References.Remove(chkRef)

                            End If

                        Catch ex As Exception
                            Console.WriteLine(ex.ToString())
                            IO.File.AppendAllText("Log.txt", String.Format("{0}{1}", String.Concat(Environment.NewLine, Environment.NewLine, Environment.NewLine), ex.ToString()))
                        End Try
                    Next

                    objWord.ActiveDocument.SaveAs(String.Concat(fri.FullName), WdSaveFormat.wdFormatXMLTemplateMacroEnabled)

                Catch ex As Exception
                    IO.File.AppendAllText("Log.txt", String.Format("{0}{1}", String.Concat(Environment.NewLine, Environment.NewLine, Environment.NewLine), ex.ToString()))
                End Try
            Next fri

            objWord.Quit()
            objDoc = Nothing
            objWord = Nothing

    Any help would be appreciated.

    regards

    Kevin

    Tuesday, March 24, 2020 5:36 PM

All replies

  • Since this is more of an issue with Word automation I suggest also asking in the Word for developers forum while keeping this post. When posting in the word for developer forum make sure you indicate you have a post here too else a moderator in that forum most likely move your question here.

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=worddev&filter=alltypes&sort=lastpostdesc


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, March 24, 2020 5:48 PM
    Moderator