Private Sub CommandButton2_Click() Dim Balise1, Balise1a, Balise2, Balise2a, Balise3, Balise3a As String Dim Str, St, Result, Liste As String Str = "[OBJECTIVE] Texte 1 dans objective /OBJECTIVE [REMINDER] Texte dans reminder /REMINDER [TRACE] Texte 1 dans trace /TRACE [OBJECTIVE] Texte 2 dans objective /OBJECTIVE [TRACE] Texte 2 dans trace /TRACE" Balise1 = "[OBJECTIVE]" Balise1a = "/OBJECTIVE" Balise2 = "[REMINDER]" Balise2a = "/REMINDER" Balise3 = "[TRACE]" Balise3a = "/TRACE" St = Str Liste = Balise1 & vbCrLf Do While InStr(1, St, Balise1) > 0 Result = Split(Split(St, Balise1, 2)(1), Balise1a)(0) If Len(Result) > 0 Then Liste = Liste & Result & vbCrLf St = Right(St, Len(St) - (InStr(1, St, Balise1))) Loop MsgBox Liste St = Str Liste = Balise2 & vbCrLf Do While InStr(1, St, Balise2) > 0 Result = Split(Split(St, Balise2, 2)(1), Balise2a)(0) If Len(Result) > 0 Then Liste = Liste & Result & vbCrLf St = Right(St, Len(St) - (InStr(1, St, Balise2))) Loop MsgBox Liste St = Str Liste = Balise3 & vbCrLf Do While InStr(1, St, Balise3) > 0 Result = Split(Split(St, Balise3, 2)(1), Balise3a)(0) If Len(Result) > 0 Then Liste = Liste & Result & vbCrLf St = Right(St, Len(St) - (InStr(1, St, Balise3))) Loop MsgBox Liste End Sub
For i = 1 To WordDoc.Fields.Count If WordDoc.Fields(i).Result <> "" Then MsgBox WordDoc.Fields(i).Result.Text End If Next
'Nécessite d'activer la référence "Microsoft Word xx.x Object Library"
'(Outils ==> réferences ==> Microsoft Word xx.x Object Library)
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionFor i = 1 To WordDoc.Fields.Count If WordDoc.Fields(i).Result <> "" Then MsgBox WordDoc.Fields(i).Result.Text End If Next
Sub Import_Word() Dim Wrd As Object NomFich = "c:\users\username\desktop\fichier.doc" Feuil2.Range("A1") = NomFich Set Wrd = CreateObject("word.Application") Wrd.Documents.Open (NomFich) Wrd.Selection.WholeStory Wrd.Selection.Copy ThisWorkbook.Activate Feuil2.Activate Feuil2.Range("A2").Select ActiveSheet.Paste ' Ferme Word en appliquant la méthode Quit sur l'objet Application. Wrd.Application.Quit Set Wrd = Nothing End Sub
Sub test() Dim Paragraphe As Object, WordApp As Object, WordDoc As Object Dim Txt As String, Deb As Integer, Fin As Integer, Ligne As Integer Dim Col As Integer, Bal As String 'le document Word est supposé fermé avant le lancement de la macro With Sheets("Feuil1") Fichier = "C:\Users\Daniel\Documents\Donnees\Daniel\mpfe\djamat\djamat fichier_soft2.doc" 'creation session Word Set WordApp = CreateObject("Word.Application") 'pour que word reste masqué pendant l'opération WordApp.Visible = False 'ouverture du fichier Word Set WordDoc = WordApp.Documents.Open(Fichier) For Each Paragraphe In WordDoc.Paragraphs Txt = Paragraphe.Range.Text Deb = InStr(1, Txt, "[") Fin = InStr(1, Txt, "]") If Deb > 0 And Fin > 0 Then Bal = Mid(Txt, Deb + 1, Fin - 2) If InStr(1, Txt, "& Bal & "") > 0 Then Deb = InStr(1, Txt, "[" & Bal & "]") + Len("[" & Bal & "]") Fin = InStr(1, Txt, "& Bal & "") - Len("& Bal & "") Txt = Mid(Txt, Deb, Fin) Set c = .Rows(1).Find(Bal, , , xlWhole) If c Is Nothing Then If .Cells(1, 1) = "" Then Col = 1 Else Col = .Cells(1, .Columns.Count).End(xlToLeft).Column + 1 End If .Cells(1, Col) = Bal Else Col = c.Column End If .Cells(2, Col) = .Cells(2, Col) & Txt End If End If Next Paragraphe WordDoc.Close WordApp.Quit Set WordDoc = Nothing Set WordApp = Nothing End With End Sub