Bonjour

Dans mon code je remarque un problème de selection de feuille et de cellule, je m'explique

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
Private Sub BtSymbole_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtSymbole.Click
        xlApp = New Excel.Application
        xlWorkbook = xlApp.Workbooks.Open("C:\Users\Mario\Documents\VBA\Excel\Bonjour.xls")
        xlWorksheet = CType(xlApp.Worksheets("Feuil1"), Worksheet)
        XlRange = xlWorksheet.UsedRange
        CType(XlRange.Cells(2, 4), Excel.Range).Select()
        xlWorkbook.Save()
        xlApp.Quit()
    End Sub
Avec ce code si ma cellule active ce trouve sur la feuil1 pas de problème pour déplacer ma selection sur cells(2, 4). mais si ma cellule active se trouve sur une autre feuille, j'ai un message d'erreur au niveau de CType(XlRange.Cells(2, 4), Excel.Range).Select()

L'exception System.Runtime.InteropServices.COMException n'a pas été gérée
ErrorCode=-2146827284
HelpLink="C:\Program Files\Microsoft Office\Office\1036\xlmain9.chm"
Message="La méthode Select de la classe Range a échoué."
Source="Microsoft Excel"
StackTrace:
à System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData) à Excel.Range.Select() à AppExcel.BtSelection.BtSymbole_Click(Object sender, EventArgs e) dans C:\Users\Mario\Documents\Visual Studio 2008\Projects\AppExcel\AppExcel\Tableau.vb:ligne 46 à System.Windows.Forms.Control.OnClick(EventArgs e) à System.Windows.Forms.Button.OnClick(EventArgs e) à System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) à System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) à System.Windows.Forms.Control.WndProc(Message& m) à System.Windows.Forms.ButtonBase.WndProc(Message& m) à System.Windows.Forms.Button.WndProc(Message& m) à System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) à System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) à System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) à System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) à System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) à System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) à System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) à System.Windows.Forms.Application.Run(ApplicationContext context) à Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() à Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() à Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) à AppExcel.My.MyApplication.Main(String[] Args) dans 17d14f5c-a337-4978-8281-53493378c1071.vb:ligne 81 à System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) à System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) à Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() à System.Threading.ThreadHelper.ThreadStart_Context(Object state) à System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) à System.Threading.ThreadHelper.ThreadStart()
InnerException:
Quelqu'un sait pourquoi?
merci