gpt4 book ai didi

c# - Excel 应用程序在设置宽度时引发异常

转载 作者:太空宇宙 更新时间:2023-11-03 12:27:18 28 4
gpt4 key购买 nike

我正在尝试通过 WPF 项目控制 Excel 应用程序。

var   xla = new Microsoft.Office.Interop.Excel.Application();

xla.Width = 400; // This line is throwing the below exception.

它有几次工作正常,几天后同一行出现如下所示的异常。注销并登录系统可暂时解决问题。

System.Runtime.InteropServices.COMException was unhandled
HResult=-2146827284
Message=Exception from HRESULT: 0x800A03EC
Source=Microsoft.Office.Interop.Excel
ErrorCode=-2146827284
StackTrace:
at Microsoft.Office.Interop.Excel.ApplicationClass.set_Width(Double RHS)
at EmbedExcel1.Form1.button1_Click(Object sender, EventArgs e) in d:\Test Projects\EmbedExcel1\EmbedExcel1\Form1.cs:line 27
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at EmbedExcel1.Program.Main() in d:\Test Projects\EmbedExcel1\EmbedExcel1\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

我该如何解决这个问题?

最佳答案

当 Excel 窗口最大化时,设置宽度不起作用。

在设置 Width 属性之前,您应该设置 xla.WindowState = xlNormal

关于c# - Excel 应用程序在设置宽度时引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44253615/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com