J
Juan
Hi everybody,
I'm trying to run a macro from Excel which imports some data from MS Project.
I'm having some problems with the code and it’s getting me to lose
perspective.
I want the macro to look around and check whether the wanted .mpp is already
opened or not. In case it’s opened, the macro has to leave it opened at the
end. In case it wasn't opened, it will have to close it afterwards (only the
file in question).
I've got the impression that the more I work on the code, the more I'm
moving away from the right solution.
Lines 30 and/or 46 seem not to be working properly.
I’ve tried to solve it with Set XXX = GetObject( path ,
“MSProject.Applicationâ€), but unsuccessfully.
I post here the code and hope that someone can help me with it.
As I said, I may be trying to solve it wrongly. I'll accept any suggestions.
Thanks in advance,
Juan.
Private Sub CmdImportAP_Click()
Dim file As String
Dim path
Dim stabe As String
Dim i As Integer
Dim mpApp As MSProject.Application
Dim T As Task
Dim xlcell As Range
Dim Zelle As Range
Dim Spalte As Long
Dim X As String
Dim Y As String
Dim wanted As Variant
Dim objSheet As Worksheet
Dim p As Integer
Dim found As Boolean
1 path = Application.GetOpenFilename("Microsoft Project file
(*.mpp),*.mpp")
2 If path = False Then
3 MsgBox "no file chosen"
4 Exit Sub
5 Else
6 i = 1
7 Do
8 stabe = Left(Right(path, i), 1)
9 i = i + 1
10 file = stabe & file
11 Loop While stabe <> "\" And i < Len(path)
12 If Left(file, 1) = "\" Then
13 file = Right(file, Len(file) - 1)
14 End If
15 End If
16 Worksheets(4).Visible = True
17 Worksheets(4).Select
18 Application.ScreenUpdating = False
19 For Each Zelle In Worksheets(4).Range("A60:A560").Rows
20 If Zelle.Hidden = True Then
21 Zelle.Hidden = False
22 End If
23 Next
24 Application.ScreenUpdating = True
25 p = 0
26 found = False
27 For p = 0 To MSProject.Application.Projects.Count
28 If MSProject.Application.Projects.Item(p).Name = Datei Then
29 found = True
30 MSProject.Application.Projects.Item(p).Activate
31 End If
32 Next
33 If found = True Then
34 Set objSheet = Worksheets(4)
35 With objSheet
36 wanted = Range("RangeDatum").Value
37 Set xlcell = .Range("RangeLaufzeit").Find(what:=wanted, _
38 LookAt:=xlWhole, LookIn:=xlValues, MatchCase:=True)
39 If Not xlcell Is Nothing Then
40 Spalte = xlcell.Column
41 Else
42 MsgBox "no matching date", vbInformation
43 Exit Sub
44 End If
45 Set xlcell = .Cells(62, Spalte)
46 For Each T In MSProject.Application.ActiveProject.Tasks
47 If Not T Is Nothing Then
48 If Not T.Summary Then
49 xlcell.Value = T.PercentComplete
50 xlcell.NumberFormat = "General\%"
51 Set xlcell = xlcell.Offset(1, 0)
52 End If
53 End If
54 Next T
55 Set xlcell = xlcell.Offset(-1, 0)
56 Y = xlcell.Address
57 X = Cells(61, Spalte).Address
58 .Range(X & ":" & Y).Select
59 End With
60 ElseIf found = False Then
61 Set mpApp = New MSProject.Application
62 mpApp.FileOpen path
63 mpApp.Visible = False
64 Set objSheet = Worksheets(4)
65 With objSheet
66 wanted = Range("RangeDatum").Value
67 Set xlcell = .Range("RangeLaufzeit").Find(what:=wanted, _
68 LookAt:=xlWhole, LookIn:=xlValues, MatchCase:=True)
69 If Not xlcell Is Nothing Then
70 Spalte = xlcell.Column
71 Else
72 MsgBox "no matching date", vbInformation
73 Exit Sub
74 End If
75 Set xlcell = .Cells(62, Spalte)
76 For Each T In mpApp.ActiveProject.Tasks
77 If Not T Is Nothing Then
78 If Not T.Summary Then
79 xlcell.Value = T.PercentComplete
80 xlcell.NumberFormat = "General\%"
81 Set xlcell = xlcell.Offset(1, 0)
82 End If
83 End If
84 Next T
85 Set xlcell = xlcell.Offset(-1, 0)
86 Y = xlcell.Address
87 X = Cells(61, Spalte).Address
88 .Range(X & ":" & Y).Select
89 End With
90 mpApp.FileClose pjDoNotSave
91 Set mpApp = Nothing
92 End If
93 Set objSheet = Nothing
94 Set xlcell = Nothing
95 AppActivate "Microsoft Excel"
96 Application.ActiveWorkbook.Worksheets(3).Activate
97 Range("CB20").Select
End Sub
I'm trying to run a macro from Excel which imports some data from MS Project.
I'm having some problems with the code and it’s getting me to lose
perspective.
I want the macro to look around and check whether the wanted .mpp is already
opened or not. In case it’s opened, the macro has to leave it opened at the
end. In case it wasn't opened, it will have to close it afterwards (only the
file in question).
I've got the impression that the more I work on the code, the more I'm
moving away from the right solution.
Lines 30 and/or 46 seem not to be working properly.
I’ve tried to solve it with Set XXX = GetObject( path ,
“MSProject.Applicationâ€), but unsuccessfully.
I post here the code and hope that someone can help me with it.
As I said, I may be trying to solve it wrongly. I'll accept any suggestions.
Thanks in advance,
Juan.
Private Sub CmdImportAP_Click()
Dim file As String
Dim path
Dim stabe As String
Dim i As Integer
Dim mpApp As MSProject.Application
Dim T As Task
Dim xlcell As Range
Dim Zelle As Range
Dim Spalte As Long
Dim X As String
Dim Y As String
Dim wanted As Variant
Dim objSheet As Worksheet
Dim p As Integer
Dim found As Boolean
1 path = Application.GetOpenFilename("Microsoft Project file
(*.mpp),*.mpp")
2 If path = False Then
3 MsgBox "no file chosen"
4 Exit Sub
5 Else
6 i = 1
7 Do
8 stabe = Left(Right(path, i), 1)
9 i = i + 1
10 file = stabe & file
11 Loop While stabe <> "\" And i < Len(path)
12 If Left(file, 1) = "\" Then
13 file = Right(file, Len(file) - 1)
14 End If
15 End If
16 Worksheets(4).Visible = True
17 Worksheets(4).Select
18 Application.ScreenUpdating = False
19 For Each Zelle In Worksheets(4).Range("A60:A560").Rows
20 If Zelle.Hidden = True Then
21 Zelle.Hidden = False
22 End If
23 Next
24 Application.ScreenUpdating = True
25 p = 0
26 found = False
27 For p = 0 To MSProject.Application.Projects.Count
28 If MSProject.Application.Projects.Item(p).Name = Datei Then
29 found = True
30 MSProject.Application.Projects.Item(p).Activate
31 End If
32 Next
33 If found = True Then
34 Set objSheet = Worksheets(4)
35 With objSheet
36 wanted = Range("RangeDatum").Value
37 Set xlcell = .Range("RangeLaufzeit").Find(what:=wanted, _
38 LookAt:=xlWhole, LookIn:=xlValues, MatchCase:=True)
39 If Not xlcell Is Nothing Then
40 Spalte = xlcell.Column
41 Else
42 MsgBox "no matching date", vbInformation
43 Exit Sub
44 End If
45 Set xlcell = .Cells(62, Spalte)
46 For Each T In MSProject.Application.ActiveProject.Tasks
47 If Not T Is Nothing Then
48 If Not T.Summary Then
49 xlcell.Value = T.PercentComplete
50 xlcell.NumberFormat = "General\%"
51 Set xlcell = xlcell.Offset(1, 0)
52 End If
53 End If
54 Next T
55 Set xlcell = xlcell.Offset(-1, 0)
56 Y = xlcell.Address
57 X = Cells(61, Spalte).Address
58 .Range(X & ":" & Y).Select
59 End With
60 ElseIf found = False Then
61 Set mpApp = New MSProject.Application
62 mpApp.FileOpen path
63 mpApp.Visible = False
64 Set objSheet = Worksheets(4)
65 With objSheet
66 wanted = Range("RangeDatum").Value
67 Set xlcell = .Range("RangeLaufzeit").Find(what:=wanted, _
68 LookAt:=xlWhole, LookIn:=xlValues, MatchCase:=True)
69 If Not xlcell Is Nothing Then
70 Spalte = xlcell.Column
71 Else
72 MsgBox "no matching date", vbInformation
73 Exit Sub
74 End If
75 Set xlcell = .Cells(62, Spalte)
76 For Each T In mpApp.ActiveProject.Tasks
77 If Not T Is Nothing Then
78 If Not T.Summary Then
79 xlcell.Value = T.PercentComplete
80 xlcell.NumberFormat = "General\%"
81 Set xlcell = xlcell.Offset(1, 0)
82 End If
83 End If
84 Next T
85 Set xlcell = xlcell.Offset(-1, 0)
86 Y = xlcell.Address
87 X = Cells(61, Spalte).Address
88 .Range(X & ":" & Y).Select
89 End With
90 mpApp.FileClose pjDoNotSave
91 Set mpApp = Nothing
92 End If
93 Set objSheet = Nothing
94 Set xlcell = Nothing
95 AppActivate "Microsoft Excel"
96 Application.ActiveWorkbook.Worksheets(3).Activate
97 Range("CB20").Select
End Sub