MCPcopy Create free account
hub / github.com/dothinking/PyAddin / import_module

Method import_module

pyaddin/xlam/vba.py:29–51  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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):

Callers 1

createMethod · 0.95

Calls 1

AddInExceptionClass · 0.85

Tested by

no test coverage detected