JasonEnsor
Programmer
Hey Guys,
I have some code that I have found (listed below) that works in Excel 2010 to programmatically create a new Module and Name it etc.... I am however struggling to get it to work in Word 2010. I have added the required reference to the "Visual Basic Extensibility" (although if I can get away without adding the reference it would be good). However in Word it kicks up an error on the following line
Code:
Dim NewMod As Module
The code I am using is:
Code:
Option Explicit
Public Sub ModuleStart()
' Create the new module.
Dim NewMod As Module
Set NewMod = Modules.Add
' Get the module name.
Dim ModName As String
ModName = InputBox("Type the Module Name", "Name")
NewMod.Name = ModName
' Get the new projects
Dim MyProj As CodeModule
Set MyProj = Application.VBE.VBProjects(1).VBComponents(ModName).CodeModule
' Open the file.
MyProj.CodePane.Show
' Add the required option statement, module header, and opening Sub.
MyProj.InsertLines 1, "Option Explicit" + vbCrLf + vbCrLf + _
"' Module Name: " + ModName + vbCrLf + "' Author: " + _
Application.UserName + vbCrLf + "' Date: " + CStr(DateTime.Now) + _
vbCrLf + vbCrLf + "Public Sub " & CStr(ModName) & "()" + vbCrLf + vbCrLf + "End Sub"
End Sub
I am trying my best to extend my knowledge however resources online seem sparse, Ideally I would like to update some of my projects to VSTO in the future as I think that would solve a few of my previous user issues I've had in the past but again the resources just don't seem to be around.
Any help is as always appreciated.
J.