[VB] Création fichier Excel

Bonjour,

Je voudrais créer un fichier excel dans lequel je puisse écrire dans la suite du code mais je n'y arrive pas.
Voici mon code:

Set ExcelApplication = CreateObject("Excel.Application")
Set MyFile = ExcelApplication.CreateObject(PathFile, True)

Set Report = MyFile.Worksheets("Test_Failed_Defects")

Merci de me dire ce qui ne va pas.

6 réponses

  1. Private Sub Command1_Click()

    Dim xls As Excel.Application
    Dim xlsfeuille As Excel.Worksheet
    Dim xlsclasseur As Excel.Workbook
    Dim pchar As String

    Set xls = CreateObject("Excel.Application")
    Set xlsclasseur = xls.Workbooks.Add
    Set xlsfeuille = xlsclasseur.Worksheets(1)

    xlsfeuille.Cells(3, 1).Value = "MA FEUILLE EXCEL EST BELLE ET BIEN CREE"
    xlsfeuille.Cells(4, 1).Value = "C'EST FACILE NON"

    CommonDialog1.Filter = "Fichiers excel (*.xls)|*.xls"
    CommonDialog1.ShowSave
    chpath = CommonDialog1.FileName
    If chpath <> "" Then
    xlsclasseur.SaveAs (chpath)
    End If
    xls.Application.Quit
    Set xls = Nothing
    MsgBox "FIN DE L'EXPORTATION", vbOKOnly + vbInformation, "GESTION DES STATISTIQUES"

    End Sub
    4
    1. Bonjour,

      voila ton code :

      Private Sub Command1_Click()

      Dim xls As Excel.Application
      Dim xlsfeuille As Excel.Worksheet
      Dim xlsclasseur As Excel.Workbook
      Dim pchar As String

      Set xls = CreateObject("Excel.Application")
      Set xlsclasseur = xls.Workbooks.Add
      Set xlsfeuille = xlsclasseur.Worksheets(1)

      xlsfeuille.Cells(3, 1).Value = "MA FEUILLE EXCEL EST BELLE ET BIEN CREE"
      xlsfeuille.Cells(4, 1).Value = "C'EST FACILE NON"

      CommonDialog1.Filter = "Fichiers excel (*.xls)|*.xls"
      CommonDialog1.ShowSave
      chpath = CommonDialog1.FileName
      If chpath <> "" Then
      xlsclasseur.SaveAs (chpath)
      End If
      xls.Application.Quit
      Set xls = Nothing
      MsgBox "FIN DE L'EXPORTATION", vbOKOnly + vbInformation, "GESTION DES STATISTIQUES"

      J'ai mis directement quand je clique sur un bouton mais il me souligne plein de truk. il faut pa faire des imports ou autres choses ?
      0
    2. Bonjour Monsieur
      J'ai un petit soucis dans Visual basic pourriez-vous m'aidez svp?
      Dans mon fichier dans la feuille DEVIS ORMAZABAL PRIVEE il y a un boutton VALIDER et j'aimerai qu'une maccro enregistre la facture dans un fichier DEVIS 2009 en numérotant le devis dans le fichier je ne sais pas si c'est possible? Je vous remercie par avance j'ai essayé de faire un module sous vba le 1er mais ca ne marche pas.
      Voici le fichier: http://www.cijoint.fr/cjlink.php?file=cj200906/cijRFxsbTD.xls
      Micka
      0
  2. Il faut que tu importes excel sinon il ne comprend pas les code excel
    tu va dans Projet / Ajouter une référence / COM et tu sélectionne Microsoft excel ... là il te souligneras plus rien ^^
    3
    1. Bonjour,

      Dim xls As Excel.Application
      Dim xlsfeuille As Excel.Worksheet
      Dim xlsclasseur As Excel.Workbook

      Cette déclaration utilise des types prédéfinies qui se trouvent dans
      la librairie ( référence Microsoft excel ... ).

      À ce moment je dis que vous utilisé les objets d'Excel.

      Par opposition, vous pouvez déclarer vos variables ainsi :

      Dim xls As Variant
      Dim xlsfeuille As Variant
      Dim xlsclasseur As Variant

      Set xls = CreateObject("Excel.Application")
      Cette instruction instancie un objet de scripting et ne nécessite pas la référence.

      exemple de script VBS pilotant Excel sans cette référence:
      http://membre.oricom.ca/lupin/documents/excelvbs.txt

      Lupin
      2
      1. le compilateur me souligne le Type Excel.Application meme si j'ai importer le package
        Imports System.Net.Mime.MediaTypeNames
        2
        1. salut

          tu doit avant tous dans le menu objet selectionner reference puis activer microsoft excel
          1
          1. Ok c'est nickel merci à tous ^^
            1