Import vba module from specified file (*.bas). Args: module_file (str): Module file with module name declared in the first line: Attribute VB_Name = "xxxx"
(self, module_file:str)
| 27 | |
| 28 | |
| 29 | def import_module(self, module_file:str): |
| 30 | '''Import vba module from specified file (*.bas). |
| 31 | |
| 32 | Args: |
| 33 | module_file (str): Module file with module name declared in the first line: |
| 34 | Attribute VB_Name = "xxxx" |
| 35 | ''' |
| 36 | # check module name |
| 37 | with open(module_file, 'r') as f: |
| 38 | header = f.readline().strip() |
| 39 | |
| 40 | if not header.startswith('Attribute VB_Name'): |
| 41 | raise AddInException('The first line of a valid module file should be: Attribute VB_Name = "xxxx"') |
| 42 | |
| 43 | # delete module if existed already |
| 44 | module_name = header.split('"')[-2] |
| 45 | for comp in self.wb.VBProject.VBComponents: |
| 46 | if comp.Name == module_name: |
| 47 | self.wb.VBProject.VBComponents.Remove(comp) |
| 48 | break |
| 49 | |
| 50 | # import module |
| 51 | self.wb.VBProject.VBComponents.Import(module_file) |
| 52 | |
| 53 | |
| 54 | def import_named_module(self, module_name:str, module_file:str): |