none
Calling ApplyQuickStyleSet2 in Word throws COMException RRS feed

  • Question

  • Hi there,

    I'm trying to reset the Quick Styles to the one of my template in Word, but doing fails with a COMException "HRESULT E_FAIL".

    I'm going to put the details below, but before some more information:

    The funny thing is that I could successfully do it once in Visual Studio's Immediate Window, but only once, so it should work actually.

    The code I'm using:

    private void ResetWordDocument(Word._Application app)
            {
                var template = fileProvider.GetTemplatePath(TemplateType.Word);
    
                if (string.IsNullOrWhiteSpace(template))
                    throw new FileNotFoundException("No Word template file found.");
    
                var doc = app.ActiveDocument;
    
                doc.set_AttachedTemplate(template);
                
                doc.UpdateStyles();
                doc.ApplyQuickStyleSet2(Word.WdApplyQuickStyleSets.wdTemplateSet);
                doc.ApplyDocumentTheme(template);
            }

    Exception details:

    Ausnahme ausgelöst: "System.Runtime.InteropServices.COMException" in Microsoft.Office.Interop.Word.dll
    'doc.ApplyQuickStyleSet2(Word.WdApplyQuickStyleSets.wdTemplateSet)' löste eine Ausnahme des Typs 'System.Runtime.InteropServices.COMException' aus
        Data: {System.Collections.ListDictionaryInternal}
        ErrorCode: -2147467259
        HResult: -2147467259
        HelpLink: null
        IPForWatsonBuckets: 0x00000000
        InnerException: null
        IsTransient: false
        Message: "Beim Aufruf einer COM-Komponente wurde ein HRESULT E_FAIL-Fehler zurückgegeben."
        RemoteStackTrace: null
        Source: "Microsoft.Office.Interop.Word"
        StackTrace: "   bei Microsoft.Office.Interop.Word.DocumentClass.ApplyQuickStyleSet2(Object& Style)"
        TargetSite: {Void ApplyQuickStyleSet2(System.Object ByRef)}
        WatsonBuckets: null
        _HResult: -2147467259
        _className: null
        _data: {System.Collections.ListDictionaryInternal}
        _dynamicMethods: null
        _exceptionMethod: {Void ApplyQuickStyleSet2(System.Object ByRef)}
        _exceptionMethodString: null
        _helpURL: null
        _innerException: null
        _ipForWatsonBuckets: 0x00000000
        _message: "Beim Aufruf einer COM-Komponente wurde ein HRESULT E_FAIL-Fehler zurückgegeben."
        _remoteStackIndex: 0
        _remoteStackTraceString: null
        _safeSerializationManager: {System.Runtime.Serialization.SafeSerializationManager}
        _source: "Microsoft.Office.Interop.Word"
        _stackTrace: {sbyte[24]}
        _stackTraceString: null
        _watsonBuckets: null
        _xcode: -532462766
        _xptrs: 0x00000000

    Anyone got any idea why it was working once and only once with Immediate Window, but not anymore.

    Is there a way to get more details about this error?

    Cheers,
    Tom

    Thursday, March 12, 2020 12:18 PM

All replies

  • Ok, by now I've found a workaround to only show the styles from the template, but if anyone got an idea why the correct usage isn't working, I'd still appreciate any comments here.
    Thursday, March 12, 2020 1:44 PM