EXPORTER MFLEXGRID DANS DOCUMENT WORD

Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 - 13 mai 2009 à 11:38
320C Messages postés 93 Date d'inscription dimanche 24 avril 2011 Statut Membre Dernière intervention 15 janvier 2014 - 9 janv. 2013 à 11:22
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.

https://codes-sources.commentcamarche.net/source/50006-exporter-mflexgrid-dans-document-word

320C Messages postés 93 Date d'inscription dimanche 24 avril 2011 Statut Membre Dernière intervention 15 janvier 2014
9 janv. 2013 à 11:22
Merci RENFIELD
Salut à Tous.
Merci pour la solution (je n'ai testé que celle de garoui pour le moment).
J'ai un petit problème et j'espère avoir votre aide : en fait j'ai les cellules de la première ligne fusionnées par triplets dans mon MSFlexGrid (C'est à dire que chaque trois cellules forment une seule). Qu'est ce que je dois ajouter au code pour avoir le même résultat en Word ?
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
10 janv. 2012 à 07:24
menu Projet > Reference > Microsoft Word
320C Messages postés 93 Date d'inscription dimanche 24 avril 2011 Statut Membre Dernière intervention 15 janvier 2014
9 janv. 2012 à 23:51
Salut à tous!
SVP, aidez moi, ... j'utilise VB 6.0 édition professionnel, mais pour essayer de declarer le variable word???; cela ne marche pas

Càd exemple:
dim oWord as Word.application

Ceci ne mais donne pas l'oportunite de regenere Word dans la liste de proprété qui sorte et ci je vais faire la mailin à copier, le programme affiche une erreur sur cette déclaration.
Merci à tous pour votre contrbibution...
cres12 Messages postés 5 Date d'inscription lundi 24 octobre 2005 Statut Membre Dernière intervention 20 juillet 2012
28 oct. 2011 à 11:06
Excellent pour le code, cependant j'aimerais ajouter 2 tableaux dans le fichhier word comment faire?
cs_goc Messages postés 19 Date d'inscription samedi 25 octobre 2003 Statut Membre Dernière intervention 21 avril 2011
20 oct. 2010 à 13:45
Renfield
peu etre un peu rapide ta modif du prog

voila une version qui "fonctionne" :-))

Public Sub AppendGridToWordDoc(ByRef voGrid As MSFlexGrid, Optional ByRef voDoc As Word.Document)
Dim oWord As Word.Application
Dim nRow As Integer
Dim nCol As Integer
Dim sBuffer As String
Dim stemp As String

If Not Nothing Is voGrid Then
For nRow = 0 To voGrid.Rows - 1
For nCol = 0 To voGrid.Cols - 1
stemp = voGrid.TextMatrix(nRow, nCol)
If nCol = voGrid.Cols - 1 Then
stemp = stemp & vbCrLf
Else
stemp = stemp & vbTab
End If
sBuffer = sBuffer + stemp
Next nCol
Next nRow

If Nothing Is voDoc Then
On Error Resume Next
Set oWord = GetObject(, "Word.Application")
If Nothing Is oWord Then
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
End If

Set voDoc = oWord.Documents.Add
End If

With voDoc.Bookmarks("\EndOfDoc").Range
.text = sBuffer
.ConvertToTable vbTab, Format:=wdTableFormatColorful2 '9
End With
End If
End Sub
cs_drissou Messages postés 160 Date d'inscription dimanche 7 décembre 2003 Statut Membre Dernière intervention 14 janvier 2009
15 mai 2009 à 10:49
Merci garoui et Renfield (pour l'optimisation)
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
13 mai 2009 à 11:47
un peu de nettoyage plus tard:

Private Sub AppendGridToWordDoc(ByRef voGrid As MsFlexgrid, Optional ByRef voDoc As Word.Document)
Dim oWord As Word.Application
Dim nRow As Integer
Dim nCol As Integer
Dim sBuffer As String
If Not Nothing Is voGrid Then
For nRow = 0 To voGrid.Rows - 1
For nCol = 0 To voGrid.Cols - 1
sBuffer = sTemp & voGrid.TextMatrix(row, col)
If nCol = voGrid.Cols - 1 Then
sBuffer = sBuffer & vbCrLf
Else
sBuffer = sBuffer & vbTab
End If
Next nCol
Next nRow

If Nothing Is voDoc Then
On Error Resume Next
Set oWord = GetObject(, "Word.Application")
If Nothing Is oWord Then
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
End If

Set voDoc = oWord.Documents.Add
End If

With voDoc.Bookmarks("\EndOfDoc").Range
.Text = sBuffer
.ConvertToTable vbTab, Format:=wdTableFormatColorful2 '9
End With
End If
End Sub

ici, on peux fournir une grille en parametre, ainsi qu'un document word auquel on souhaite ajouter notre tableau (l'ajout se fait systématiquement à la fin du document)

on utilise également une petite astuce (GetObject) permettant de se raccrocher à une instance Word existante (plus rapide: pas besoin de CreateObject)
Renfield Messages postés 17287 Date d'inscription mercredi 2 janvier 2002 Statut Modérateur Dernière intervention 27 septembre 2021 74
13 mai 2009 à 11:38
pourquoi passes tu par la création du tableau arr ?

plus utile sous la forme d'une fonction réutilisable.
Rejoignez-nous