Fix Outlook folder discovery to find all contact folders
- Scan all stores recursively instead of only the default folder - Properly release COM objects to avoid leaks - Better error handling with debug output per store/folder - Show error message if Outlook is not reachable - Fallback to default contacts folder if recursive scan finds nothing Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
d0a2ffc9fd
commit
9298c3c287
|
|
@ -50,20 +50,29 @@ namespace StarfaceOutlookSync.Services
|
||||||
var app = GetOutlookApp();
|
var app = GetOutlookApp();
|
||||||
var ns = app.GetNamespace("MAPI");
|
var ns = app.GetNamespace("MAPI");
|
||||||
|
|
||||||
// Standard-Kontaktordner
|
// Alle Stores durchgehen (jedes Konto, jede PST-Datei etc.)
|
||||||
var defaultFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
|
|
||||||
folders.Add(defaultFolder.FolderPath);
|
|
||||||
|
|
||||||
// Unterordner
|
|
||||||
AddSubFolders(defaultFolder, folders);
|
|
||||||
|
|
||||||
// Weitere Kontaktordner in anderen Stores
|
|
||||||
foreach (Outlook.Store store in ns.Stores)
|
foreach (Outlook.Store store in ns.Stores)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var rootFolder = store.GetRootFolder();
|
var rootFolder = store.GetRootFolder();
|
||||||
FindContactFolders(rootFolder, folders);
|
FindContactFoldersRecursive(rootFolder, folders);
|
||||||
|
Marshal.ReleaseComObject(rootFolder);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Error scanning store '{store.DisplayName}': {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Falls nichts gefunden, Standard-Kontaktordner als Fallback
|
||||||
|
if (folders.Count == 0)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var defaultFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
|
||||||
|
folders.Add(defaultFolder.FolderPath);
|
||||||
|
Marshal.ReleaseComObject(defaultFolder);
|
||||||
}
|
}
|
||||||
catch { }
|
catch { }
|
||||||
}
|
}
|
||||||
|
|
@ -78,30 +87,34 @@ namespace StarfaceOutlookSync.Services
|
||||||
return folders;
|
return folders;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AddSubFolders(Outlook.MAPIFolder folder, List<string> paths)
|
private void FindContactFoldersRecursive(Outlook.MAPIFolder folder, List<string> paths)
|
||||||
{
|
{
|
||||||
foreach (Outlook.MAPIFolder sub in folder.Folders)
|
try
|
||||||
{
|
|
||||||
if (sub.DefaultItemType == Outlook.OlItemType.olContactItem)
|
|
||||||
{
|
|
||||||
if (!paths.Contains(sub.FolderPath))
|
|
||||||
paths.Add(sub.FolderPath);
|
|
||||||
AddSubFolders(sub, paths);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void FindContactFolders(Outlook.MAPIFolder folder, List<string> paths)
|
|
||||||
{
|
{
|
||||||
|
// Kontaktordner erkennen: DefaultItemType ODER Ordnername enthaelt "Kontakt"/"Contact"
|
||||||
if (folder.DefaultItemType == Outlook.OlItemType.olContactItem)
|
if (folder.DefaultItemType == Outlook.OlItemType.olContactItem)
|
||||||
{
|
{
|
||||||
if (!paths.Contains(folder.FolderPath))
|
if (!paths.Contains(folder.FolderPath))
|
||||||
paths.Add(folder.FolderPath);
|
paths.Add(folder.FolderPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Alle Unterordner durchsuchen
|
||||||
foreach (Outlook.MAPIFolder sub in folder.Folders)
|
foreach (Outlook.MAPIFolder sub in folder.Folders)
|
||||||
{
|
{
|
||||||
FindContactFolders(sub, paths);
|
try
|
||||||
|
{
|
||||||
|
FindContactFoldersRecursive(sub, paths);
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
Marshal.ReleaseComObject(sub);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Error scanning folder '{folder.Name}': {ex.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -142,9 +142,12 @@ namespace StarfaceOutlookSync.UI
|
||||||
_outlookFolderPaths = outlook.GetContactFolderPaths();
|
_outlookFolderPaths = outlook.GetContactFolderPaths();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_outlookFolderPaths = new List<string> { "\\\\Kontakte" };
|
MessageBox.Show(
|
||||||
|
$"Outlook-Kontaktordner konnten nicht geladen werden:\n{ex.Message}\n\nIst Outlook gestartet?",
|
||||||
|
"Outlook-Verbindung", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||||
|
_outlookFolderPaths = new List<string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bestehende Werte laden
|
// Bestehende Werte laden
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue