1
- using System ;
2
1
using System . Collections . Generic ;
3
- using System . Runtime . InteropServices ;
4
2
using Microsoft . Office . Interop . Access ;
5
3
using Rubberduck . VBEditor . SafeComWrappers . Abstract ;
6
4
@@ -11,117 +9,73 @@ public class AccessApp : HostApplicationBase<Microsoft.Office.Interop.Access.App
11
9
{
12
10
public AccessApp ( ) : base ( "Access" ) { }
13
11
14
- public override IHostDocument GetDocument ( QualifiedModuleName moduleName )
12
+ public override HostDocument GetDocument ( QualifiedModuleName moduleName )
15
13
{
16
14
if ( moduleName . ComponentName . StartsWith ( "Form_" ) )
17
15
{
18
16
var name = moduleName . ComponentName . Substring ( "Form_" . Length ) ;
19
- _CurrentProject currentProject = null ;
20
- AllObjects allForms = null ;
21
- AccessObject accessObject = null ;
22
- Forms forms = null ;
23
- try
24
- {
25
- currentProject = Application . CurrentProject ;
26
- forms = Application . Forms ;
27
- allForms = currentProject . AllForms ;
28
- accessObject = allForms [ name ] ;
29
-
17
+ using ( var currentProject = new SafeIDispatchWrapper < _CurrentProject > ( Application . CurrentProject ) )
18
+ using ( var allForms = new SafeIDispatchWrapper < AllObjects > ( currentProject . Target . AllForms ) )
19
+ using ( var forms = new SafeIDispatchWrapper < Forms > ( Application . Forms ) )
20
+ using ( var accessObject = new SafeIDispatchWrapper < AccessObject > ( allForms . Target [ name ] ) )
21
+ {
30
22
return LoadHostDocument ( "Access.Form" , accessObject , forms ) ;
31
23
}
32
- finally
33
- {
34
- if ( forms != null ) Marshal . ReleaseComObject ( forms ) ;
35
- if ( accessObject != null ) Marshal . ReleaseComObject ( accessObject ) ;
36
- if ( allForms != null ) Marshal . ReleaseComObject ( allForms ) ;
37
- if ( currentProject != null ) Marshal . ReleaseComObject ( currentProject ) ;
38
- }
39
24
}
40
25
41
26
if ( moduleName . ComponentName . StartsWith ( "Report_" ) )
42
27
{
43
28
var name = moduleName . ComponentName . Substring ( "Report_" . Length ) ;
44
- _CurrentProject currentProject = null ;
45
- AllObjects allForms = null ;
46
- AccessObject accessObject = null ;
47
- Reports reports = null ;
48
- try
29
+ using ( var currentProject = new SafeIDispatchWrapper < _CurrentProject > ( Application . CurrentProject ) )
30
+ using ( var allReports = new SafeIDispatchWrapper < AllObjects > ( currentProject . Target . AllReports ) )
31
+ using ( var reports = new SafeIDispatchWrapper < Reports > ( Application . Reports ) )
32
+ using ( var accessObject = new SafeIDispatchWrapper < AccessObject > ( allReports . Target [ name ] ) )
49
33
{
50
- currentProject = Application . CurrentProject ;
51
- reports = Application . Reports ;
52
- allForms = currentProject . AllForms ;
53
- accessObject = allForms [ name ] ;
54
-
55
34
return LoadHostDocument ( "Access.Report" , accessObject , reports ) ;
56
35
}
57
- finally
58
- {
59
- if ( reports != null ) Marshal . ReleaseComObject ( reports ) ;
60
- if ( accessObject != null ) Marshal . ReleaseComObject ( accessObject ) ;
61
- if ( allForms != null ) Marshal . ReleaseComObject ( allForms ) ;
62
- if ( currentProject != null ) Marshal . ReleaseComObject ( currentProject ) ;
63
- }
64
36
}
65
37
66
38
return null ;
67
39
}
68
40
69
- public override IEnumerable < IHostDocument > GetDocuments ( )
41
+ public override IEnumerable < HostDocument > GetDocuments ( )
70
42
{
71
43
var result = new List < HostDocument > ( ) ;
72
- _CurrentProject currentProject = null ;
73
- AllObjects allObjects = null ;
74
- Forms forms = null ;
75
- Reports reports = null ;
76
-
77
- try
44
+ using ( var currentProject = new SafeIDispatchWrapper < _CurrentProject > ( Application . CurrentProject ) )
45
+ using ( var allForms = new SafeIDispatchWrapper < AllObjects > ( currentProject . Target . AllForms ) )
46
+ using ( var allReports = new SafeIDispatchWrapper < AllObjects > ( currentProject . Target . AllReports ) )
47
+ using ( var forms = new SafeIDispatchWrapper < Forms > ( Application . Forms ) )
48
+ using ( var reports = new SafeIDispatchWrapper < Reports > ( Application . Reports ) )
78
49
{
79
- currentProject = Application . CurrentProject ;
80
- allObjects = currentProject . AllForms ;
81
- forms = Application . Forms ;
82
-
83
- PopulateList ( ref result , "Access.Form" , allObjects , forms ) ;
84
-
85
- Marshal . ReleaseComObject ( allObjects ) ;
86
-
87
- allObjects = currentProject . AllReports ;
88
- reports = Application . Reports ;
89
-
90
- PopulateList ( ref result , "Access.Report" , allObjects , reports ) ;
50
+ PopulateList ( ref result , "Access.Form" , allForms , forms ) ;
51
+ PopulateList ( ref result , "Access.Report" , allReports , reports ) ;
91
52
}
92
- finally
93
- {
94
- if ( allObjects != null ) Marshal . ReleaseComObject ( allObjects ) ;
95
- if ( forms != null ) Marshal . ReleaseComObject ( forms ) ;
96
- if ( reports != null ) Marshal . ReleaseComObject ( reports ) ;
97
- if ( currentProject != null ) Marshal . ReleaseComObject ( currentProject ) ;
98
- }
99
-
53
+
100
54
return result ;
101
55
}
102
56
103
- private void PopulateList ( ref List < HostDocument > result , string className , AllObjects allObjects , dynamic objects )
57
+ private void PopulateList ( ref List < HostDocument > result , string className , SafeIDispatchWrapper < AllObjects > allObjects , dynamic objects )
104
58
{
105
- foreach ( AccessObject accessObject in allObjects )
59
+ foreach ( AccessObject rawAccessObject in allObjects . Target )
60
+ using ( var accessObject = new SafeIDispatchWrapper < AccessObject > ( rawAccessObject ) )
106
61
{
107
62
var item = LoadHostDocument ( className , accessObject , objects ) ;
108
63
result . Add ( item ) ;
109
64
}
110
65
}
111
66
112
- private HostDocument LoadHostDocument ( string className , AccessObject accessObject , dynamic objects )
67
+ private HostDocument LoadHostDocument ( string className , SafeIDispatchWrapper < AccessObject > accessObject , dynamic objects )
113
68
{
114
69
var state = DocumentState . Closed ;
115
- if ( ! accessObject . IsLoaded )
70
+ if ( ! accessObject . Target . IsLoaded )
116
71
{
117
- return new HostDocument ( accessObject . Name , className , null , state ) ;
72
+ return new HostDocument ( accessObject . Target . Name , className , state , null ) ;
118
73
}
119
74
120
- object target = objects [ accessObject . Name ] ;
121
- state = accessObject . CurrentView == AcCurrentView . acCurViewDesign
75
+ state = accessObject . Target . CurrentView == AcCurrentView . acCurViewDesign
122
76
? DocumentState . DesignView
123
77
: DocumentState . ActiveView ;
124
- return new HostDocument ( accessObject . Name , className , target , state ) ;
78
+ return new HostDocument ( accessObject . Target . Name , className , state , null ) ;
125
79
}
126
80
}
127
81
}
0 commit comments