format rtf sous excel

michir

XLDnaute Occasionnel
bonjour
j'ai des fichiers au format rtf qu je souhaite travailler sous excel
auriez vous un solution ?
Merci
 

Staple1600

XLDnaute Barbatruc
Re

Les fichiers RTF s'ouvrent (entre autre avec Word)
Donc je répète, tu n'es pas à la bonne porte.
Ici c'est là section dédiée à Excel.
Le bon endroit, c'est ici ;)

Mais si tu persistes dans cette conversion "contre-nature" :rolleyes:
Voir ici, par exemple
(et bon courage ;))
 

Staple1600

XLDnaute Barbatruc
Re

Pour gagner un peu de temps (mais comme je le disais précédemment, on utilise Word)
Essaie cette macro (en changeant le path du RTF au préalable)
VB:
Sub RTF_To_XL()
    Dim WordApp As Object
    Set WordApp = CreateObject("Word.Application")
    With WordApp
        .Documents.Open Filename:="C:\Users\Staple1600\Documents\2019\Test.rtf"
        .ActiveDocument.Select
        .Selection.Copy
    End With
    ActiveSheet.Range("A1").Select
    ActiveSheet.Paste
    WordApp.Quit
    Set WordApp = Nothing
End Sub
PS: Code testé avec Excel/Word 2013
Le résultat n'est pas forcément des plus probant ;)
 

Staple1600

XLDnaute Barbatruc
Re

Cette version donne un meilleur résultat
(enfin sur mon PC et avec mon RTF de test ;))
VB:
Sub RTF_Vers_XL3()
'ajouter la référence dans VBE à Microsoft Word Library
Dim wApp As Word.Application
Dim oDoc As Word.Document
Dim wdFileName, txtFile$
Set wApp = New Word.Application
wdFileName = Application.GetOpenFilename("Format RTF, *.rtf")
txtFile = Replace(wdFileName, "rtf", "txt")
If wdFileName = False Then Exit Sub
'conversion du *.RTF en *.txt
Set oDoc = wApp.Documents.Open(wdFileName)
oDoc.SaveAs2 Filename:=txtFile, FileFormat:=wdFormatText, Encoding:=1252, InsertLineBreaks:=True, LineEnding:=wdCRLF
oDoc.Close SaveChanges:=wdDoNotSaveChanges
wApp.Quit
Set wApp = Nothing
'Ouverture du *.txt dans Excel
    With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & txtFile, Destination:=Range("$A$1"))
    .Name = "test3"
    .FieldNames = True: .PreserveFormatting = True
    .RefreshStyle = xlInsertDeleteCells: .SaveData = True
    .AdjustColumnWidth = True: .RefreshPeriod = 0
    .TextFilePlatform = 1252: .TextFileStartRow = 1: .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileColumnDataTypes = Array(1): .Refresh BackgroundQuery:=False
End With
End Sub
 

Discussions similaires

Statistiques des forums

Discussions
312 237
Messages
2 086 488
Membres
103 233
dernier inscrit
Ange.wil