.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 proje

  • Thread starter Thread starter Kevin O'Leary
  • Start date Start date
K

Kevin O'Leary

Guest
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

Continue reading...
 
Back
Top