Bonjour,
J'ai regardé un bon nombre d'exemples sur le Net et je ne comprends pas pourquoi mon code ne fonctionne pas en ouverture d'un fichier Excel existant (ouvert ou pas).
L'exception est alors: System.Runtime.InteropServices.COMException: 'Excel cannot open the file 'Book1.xlsm' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.'. Le fichier concerné peut être ouvert sans problème par Excel.

Si je remplace Open(...) par Add() le code fonctionne pourtant.
Je ne sais pas si tous ces "Imports" sont nécessaires.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Runtime.InteropServices
Imports System.IO
Imports System
Imports System.Diagnostics
Imports System.ComponentModel
 
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim sDT As String
        Dim today As Date = Now()
        sDT = CStr(today)
        xlApp = CType(GetObject(, "Excel.Application"), Excel.Application)
        If sDT <> "" And FileName.text <> "" Then
            xlWorkBook = xlApp.Workbooks.Open(FileName.Text) ' FileName is set by a OpenFileDialog control
            xlWorkSheet = xlWorkBook.Worksheets(1)
            xlApp.Visible = True
            xlWorkSheet.Range("A1").Value = "dummy value"
            xlWorkBook.SaveAs(FileName.Text + " " + sDT)
            xlWorkBook.Close()
        End If
        xlApp.Quit()
    End Sub
Merci par avance pour votre aide.