We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Copying style from one document gives System.NullReferenceException:

I try to onverwrite styles in a Document with styles from another Document. I open both Documents and then call " Snippet

Document2.CopyStylesFromTemplate(Document1)

That gives me the error NullReferenceException.
This is the Stacktrace:

   bei Aspose.Words.StyleCollection.(Style , Style )
   bei Aspose.Words.StyleCollection.(Style , Style )
   bei Aspose.Words.StyleCollection.(Style ,     )
   bei Aspose.Words.StyleCollection.(Style ,     )
   bei Aspose.Words.StyleCollection.(StyleCollection ,     )
   bei Aspose.Words.StyleCollection.(StyleCollection )
   bei Aspose.Words.Document.CopyStylesFromTemplate(Document template)
   bei AsposeTools.ViewModels.MainViewModel.HandleButtonPressed(Object item) in C:\Users\schulzeh\Documents\Visual Studio 2017\Projects\AsposeTools\AsposeTools\ViewModels\MainViewModel.cs:Zeile 102.
   bei AsposeTools.ViewModels.MainViewModel.<get_CmdParameterButton>b__15_0(Object param) in C:\Users\schulzeh\Documents\Visual Studio 2017\Projects\AsposeTools\AsposeTools\ViewModels\MainViewModel.cs:Zeile 76.
   bei AsposeTools.Framework.RelayCommand`1.Execute(Object parameter) in C:\Users\schulzeh\Documents\Visual Studio 2017\Projects\AsposeTools\AsposeTools\Framework\RelayCommand.cs:Zeile 75.
   bei MS.Internal.Commands.CommandHelpers.CriticalExecuteCommandSource(ICommandSource commandSource, Boolean userInitiated)
   bei System.Windows.Controls.Primitives.ButtonBase.OnClick()
   bei System.Windows.Controls.Button.OnClick()
   bei System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
   bei System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
   bei System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   bei System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   bei System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   bei System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   bei System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
   bei System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
   bei System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   bei System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   bei System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   bei System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   bei System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   bei System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
   bei System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
   bei System.Windows.Input.InputManager.ProcessStagingArea()
   bei System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
   bei System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
   bei System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
   bei System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   bei System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   bei MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   bei MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   bei System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   bei System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   bei System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   bei MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   bei MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   bei System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   bei System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   bei System.Windows.Application.RunDispatcher(Object ignore)
   bei System.Windows.Application.RunInternal(Window window)
   bei System.Windows.Application.Run(Window window)
   bei System.Windows.Application.Run()
   bei AsposeTools.App.Main()

TIA

@Holger_Schulze Could you please attach your documents here for testing? We will check the issue and provide you more information.

I cannot give you the documents, as I am not allowed to give the Data away. I will try to build the problem with other data.

NeuLeer.docx (3.8 MB)
OrgLeer.docx (1.3 MB)
I want to copy the styles from NeuLeer.docx to OrgLeer,docx.
Document2.CopyStylesFromTemplate(Document1);
Document2 is OrgLeer.docx and Document1 is NeuLeer.docx. Both are opened.

@Holger_Schulze Thank you for additional information. I cannot reproduce the problem on my side using the latest 21.11.0 version of Aspose.Words. The following code was used:

Document Document1 = new Document(@"C:\Temp\NeuLeer.docx");
Document Document2 = new Document(@"C:\Temp\OrgLeer.docx");
Document2.CopyStylesFromTemplate(Document1);

Please try using the latest version of Aspose.Words and let me know if the problem still persists on your side.

Thanks a lot. In a newer Version the error does not occur.