Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
972to976
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
972to976
972to976
Aktuelles Verzeichnis
Verzeichnis Index
Verzeichnis Index
Übersicht Verzeichnisse
Inhaltsverzeichnis

Code Problem

Code Problem
24.04.2008 16:29:00
Juan
Hallo an alle,
Der folgende Code dient mir dazu, einen Datenaustausch mit Ms Project zu machen.
Erstens wird die .mpp Datei ausgewählt. Anschließend wird festgestellt ob die Datei schon offen ist oder nicht, um sie am Ende der Prozedur demnach zu schließen oder offen zu lassen.
Der Code funktioniert einigermassen gut, bis zu einem kleinen Punkt, das ich jetzt erkläre:
- Ich führe den Code mit der .mpp Datei offen. Funktioniert. Daten werden rausgenommen und .mpp Datei bleibt am Ende offen.
- Jetzt schliesse ich die .mpp Datei, und führe den Code wieder aus, ohne offene .mpp Datei im Hintergrund, also MS Project Applikation läuft nicht. Und nun taucht der Fehler auf, und zwar: "Laufzeitfehler '462': Der remote-server-computer existiert nicht oder steht nicht zur Verfügung" . Debuggen: die Zeile Set mpAPP = MSProject.Application wird gezeigt.
- Wenn ich stattdessen Beenden drücke und den Code wieder ausführe, funktioniert es halt wieder, das heiß, die .mpp Datei wird aufgemacht und am Ende wieder geschlossen. (?) Es sieht so aus als ob es in diesem Fall nötig wäre den Code 2mal laufen zu lassen.
Ich habe keine Ahnung was da los ist. Ich hoffe dass jmd mir dabei helfen kann.
Ich bedanke mich für jeden Hinweis im Voraus.
Beste Grüße,
Juan.
Anbei der Code:

Private Sub CmdImportAP_Click()
Dim Datei As String
Dim pfad
Dim stabe As String
Dim i As Integer
Dim mpApp As MSProject.Application
Dim proj As MSProject.Project
Dim T As Task
Dim xlcell As Range
Dim Spalte As Long
Dim X As String
Dim Y As String
Dim Gesucht As Variant
Dim objSheet As Worksheet
Dim found As Boolean
pfad = Application.GetOpenFilename("Microsoft Project Datei (*.mpp), *.mpp")
If pfad = False Then
MsgBox "Keine Datei wurde ausgewählt."
Exit Sub
Else
i = 1
Do
stabe = Left(Right(pfad, i), 1)
i = i + 1
Datei = stabe & Datei
Loop While stabe  "\" And i Set mpApp = MSProject.Application 
If mpApp.Visible = True Then
MsgBox "visible", vbInformation
If mpApp.Projects.Count > 0 Then
MsgBox "> 0", vbInformation
For Each proj In mpApp.Projects
MsgBox proj.Name, vbInformation
If proj.Name = Datei Then
found = True
proj.Activate
MsgBox "activated", vbInformation
End If
Next
End If
End If
Application.ScreenUpdating = True
If found = True Then
MsgBox "works...", vbInformation
Set objSheet = Worksheets(4)
With objSheet
Gesucht = Range("RangeDatum").Value
Set xlcell = .Range("RangeLaufzeit").Find(what:=Gesucht, _
LookAt:=xlWhole, LookIn:=xlValues, MatchCase:=True)
If Not xlcell Is Nothing Then
Spalte = xlcell.Column
Else
MsgBox "Es wurde kein passendes Datum gefunden.", vbInformation
Exit Sub
End If
Set xlcell = .Cells(63, Spalte) 'Set xlCell =
xlCell.Offset(1, 0)
For Each T In mpApp.ActiveProject.Tasks
If Not T Is Nothing Then
If Not T.Summary Then
xlcell.Value = T.PercentComplete
xlcell.NumberFormat = "General\%"
Set xlcell = xlcell.Offset(1, 0)
End If
End If
Next T
Set xlcell = xlcell.Offset(-1, 0)
Y = xlcell.Address
X = Cells(63, Spalte).Address
.Range(X & ":" & Y).Select
End With
ElseIf found = False Then
Set mpApp = New MSProject.Application
mpApp.FileOpen pfad
mpApp.Visible = False
Set objSheet = Worksheets(4)
With objSheet
Gesucht = Range("RangeDatum").Value
Set xlcell = .Range("RangeLaufzeit").Find(what:=Gesucht, _
LookAt:=xlWhole, LookIn:=xlValues, MatchCase:=True)
If Not xlcell Is Nothing Then
Spalte = xlcell.Column
Else
MsgBox "Es wurde kein passendes Datum gefunden.", vbInformation
Exit Sub
End If
Set xlcell = .Cells(63, Spalte) 'Set xlCell =
xlCell.Offset(1, 0)
For Each T In mpApp.ActiveProject.Tasks
If Not T Is Nothing Then
If Not T.Summary Then
xlcell.Value = T.PercentComplete
xlcell.NumberFormat = "General\%"
Set xlcell = xlcell.Offset(1, 0)
End If
End If
Next T
Set xlcell = xlcell.Offset(-1, 0)
Y = xlcell.Address
X = Cells(63, Spalte).Address
.Range(X & ":" & Y).Select
End With
mpApp.FileClose pjDoNotSave
End If
' mpApp.Quit
Set mpApp = Nothing
Set objSheet = Nothing
Set xlcell = Nothing
AppActivate "Microsoft Excel"
Application.ActiveWorkbook.Worksheets(3).Activate
Range("CB21").Select
'Sheets(3).Range("CB20").Select
End Sub


5
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Code Problem
24.04.2008 17:27:36
Luschi
HalloJuan,
so sollte es gehen: Set mpApp = New MSProject.Application
Begründung siehe: http://www.code-vb.com/fragments/Automation.htm
Stichpunkt: Automate MSProject
Gruß von Luschi
aus klein

AW: Code Problem
24.04.2008 20:43:28
Juan
Gruß Dich Luschi,
zunächst einmal Danke für die Mühe!
Ich habe das probiert und es funktioniert ganz prima!
Es ist mir auch ein paar Mal passiert, dass trotz bereits offener Datei, der Code es anscheinend nicht merkt, und die Prozedur nicht durch die If found = True Schleife durchgeht. Komisch...
Darüber mache ich mir keine Gedanke mehr.
Hauptsache es läuft.
Nochmal Danke und schönen Abend noch.
Beste Grüße,
Juan

Anzeige
Sorry. Zweig ist nicht mehr offen :-)
24.04.2008 20:47:00
Juan
.

AW: Code Problem
24.04.2008 17:29:38
Nepumuk
Hallo Juan,
kann ich nicht nachvollziehen. Ich hab allerdings gleich mal den Code geändert, bevor ich ihn getestet habe, da ich ohne Verweise arbeite. Auch ein bisschen Unsinn (Schleife zum einblenden und zum ermitteln des Dateinamens) habe ich umgebaut. Versuch es mal so:
' **********************************************************************
' Modul: Modul1 Typ: Allgemeines Modul
' **********************************************************************

Option Explicit

Private Sub CmdImportAP_Click()
    Dim Datei As String
    Dim pfad
    Dim stabe As String
    Dim i As Integer
    Dim mpApp As Object
    Dim proj As Object
    Dim T As Object
    Dim xlcell As Range, Zelle As Range
    Dim Spalte As Long
    Dim X As String
    Dim Y As String
    Dim Gesucht As Variant
    Dim objSheet As Worksheet
    Dim found As Boolean
    pfad = Application.GetOpenFilename("Microsoft Project Datei (*.mpp), *.mpp")
    If pfad = False Then
        MsgBox "Keine Datei wurde ausgewählt."
        Exit Sub
    Else
        ' i = 1
        ' Do
        ' stabe = Left(Right(pfad, i), 1)
        ' i = i + 1
        ' Datei = stabe & Datei
        ' Loop While stabe <> "\" And i < Len(pfad)
        '
        ' If Left(Datei, 1) = "\" Then
        ' Datei = Right(Datei, Len(Datei) - 1)
        ' End If
        Datei = Mid$(pfad, InStrRev(pfad, "\") + 1) '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        MsgBox Datei & vbCrLf & pfad, vbInformation
    End If
    Worksheets(4).Visible = xlSheetVisible
    Worksheets(4).Select
    Application.ScreenUpdating = False
    ' For Each Zelle In Worksheets(4).Range("A62:A561").Rows
    ' If Zelle.Hidden = True Then
    ' Zelle.Hidden = False
    ' End If
    ' Next
    Worksheets(4).Range("A62:A561").EntireRow.Hidden = False '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    found = False
    Set mpApp = CreateObject(Class:="MSProject.Application")
    If mpApp.Visible = True Then
        MsgBox "visible", vbInformation
        If mpApp.Projects.Count > 0 Then
            MsgBox "> 0", vbInformation
            For Each proj In mpApp.Projects
                MsgBox proj.Name, vbInformation
                If proj.Name = Datei Then
                    found = True
                    proj.Activate
                    MsgBox "activated", vbInformation
                End If
            Next
        End If
    End If
    Application.ScreenUpdating = True
    If found = True Then
        MsgBox "works...", vbInformation
        Set objSheet = Worksheets(4)
        With objSheet
            Gesucht = Range("RangeDatum").Value
            Set xlcell = .Range("RangeLaufzeit").Find(what:=Gesucht, _
                LookAt:=xlWhole, LookIn:=xlValues, MatchCase:=True)
            If Not xlcell Is Nothing Then
                Spalte = xlcell.Column
            Else
                MsgBox "Es wurde kein passendes Datum gefunden.", vbInformation
                Exit Sub
            End If
            Set xlcell = .Cells(63, Spalte) 'Set xlCell = xlCell.Offset(1, 0)
            For Each T In mpApp.ActiveProject.Tasks
                If Not T Is Nothing Then
                    If Not T.Summary Then
                        xlcell.Value = T.PercentComplete
                        xlcell.NumberFormat = "General\%"
                        Set xlcell = xlcell.Offset(1, 0)
                    End If
                End If
            Next T
            Set xlcell = xlcell.Offset(-1, 0)
            Y = xlcell.Address
            X = Cells(63, Spalte).Address
            .Range(X & ":" & Y).Select
        End With
    ElseIf found = False Then
        Set mpApp = CreateObject(Class:="MSProject.Application")
        mpApp.FileOpen pfad
        mpApp.Visible = False
        Set objSheet = Worksheets(4)
        With objSheet
            Gesucht = Range("RangeDatum").Value
            Set xlcell = .Range("RangeLaufzeit").Find(what:=Gesucht, _
                LookAt:=xlWhole, LookIn:=xlValues, MatchCase:=True)
            If Not xlcell Is Nothing Then
                Spalte = xlcell.Column
            Else
                MsgBox "Es wurde kein passendes Datum gefunden.", vbInformation
                Exit Sub
            End If
            Set xlcell = .Cells(63, Spalte) 'Set xlCell = xlCell.Offset(1, 0)
            For Each T In mpApp.ActiveProject.Tasks
                If Not T Is Nothing Then
                    If Not T.Summary Then
                        xlcell.Value = T.PercentComplete
                        xlcell.NumberFormat = "General\%"
                        Set xlcell = xlcell.Offset(1, 0)
                    End If
                End If
            Next T
            Set xlcell = xlcell.Offset(-1, 0)
            Y = xlcell.Address
            X = Cells(63, Spalte).Address
            .Range(X & ":" & Y).Select
        End With
        mpApp.FileClose 0
    End If
    ' mpApp.Quit
    Set mpApp = Nothing
    Set objSheet = Nothing
    Set xlcell = Nothing
    AppActivate "Microsoft Excel"
    Application.ActiveWorkbook.Worksheets(3).Activate
    Range("CB21").Select
    'Sheets(3).Range("CB20").Select
End Sub

Wenn kein Datum gefunden wurde, bleibt eine vorher nicht geöffnete Datei auch offen, da du mit Exit Sub die Routine verlässt. Ist die MS-Project nicht offen, dann bleibt am Ende das Programm ausgeblendet im Taskmanager liegen, auch nicht ganz sauber. Das musst du auf alle Fälle mit Quit beenden.
Gruß
Nepumuk

Anzeige
AW: Code Problem
24.04.2008 21:05:17
Juan
Hallo Nepumuk,
vielen Dank für Deine nette Ratschläge. Die werde ich alle in Betracht ziehen.
Auf solche Sachen komme ich nicht auf.
Dann also:
...
Else
MsgBox "Es wurde kein passendes Datum gefunden.", vbInformation
mpApp.Quit
Set mpApp = Nothing
Exit Sub
Bedanke mich nochmal.
LG,
Juan

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige