I am using VBA to automate some tasks in and between some Word and Excel files (Office 2010). Right now I have the VBA code that is used only by Word stored in the Word file, and the VBA code that is used only by Excel stored in the Excel file, with the cross-application VBA stuff stored in the Word file. However, there are some user-defined types and constants that I would like to use in both the Word-specific and Excel-specific VBA code. Is there an application-neutral way I can store these shared data types and constants in a separate module, such as maybe in VBscript, where it can be used by both Excel and Word, without having to define them in both places? If so, how do I reference that outside file in each template?
You could store them in a .bas file, and import an update. For excel, import using:
Public Sub GetModules()
ThisWorkbook.VBProject.VBComponents("modCommons").Name = "DELETEME"
ThisWorkbook.VBProject.VBComponents.Remove ThisWorkbook.VBProject.VBComponents("DELETEME")
ThisWorkbook.VBProject.VBComponents.Import ("C:\modCommons.bas")
End Sub
modCommons.bas
Attribute VB_Name = "modCommons"
Public Const itest As Integer = 101
Public Const itest2 As Integer = 102
Public Sub TestSub
msgbox "test success"
End Sub
For Word, replace ThisWorkbook with ThisDocument.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.