PDA

View Full Version : Make macros in Word



Pwyll2
11-13-2013, 09:40 AM
Hi,

I’d like to make macros in Word, in order to automatically delete things or to automatically move paragraphs (that start with the same strings of characters).

I created a macro with the recorder of Word (I don’t know how to write codes in Visual Basic, I know nothing about VB actually). The problem is that I don’t know how to make a loop so that the task is done on every paragraph until the end of the document. It stops at the end of the first paragraph.
I think I have to write « loop » somewhere, but I don’t know where, and I don’t know what else I should write in the macro.
Could someone help me please ? Thanks a lot in advance.

Here is the code I’ve got so far :
I’d like to make macros in Word, in order to automatically delete things or to automatically move paragraphs (that start with the same strings of characters).

I created a macro with the recorder of Word (I don’t know how to write codes in Visual Basic, I know nothing about VB actually). The problem is that I don’t know how to make a loop so that the task is done on every paragraph until the end of the document. It stops at the end of the first paragraph.
I think I have to write « loop » somewhere, but I don’t know where, and I don’t know what else I should write in the macro.
Could someone help me please ? Thanks a lot in advance.

Here is the code I’ve got so far :






Sub XmlToLpEffacerChampsInutiles()
'' XmlToLpEffacerChampsInutiles Macro
' Macro enregistrée le 09/11/2013 par Pwyll'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "<identifiant>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=wdExtend
Selection.TypeBackspace
Selection.Find.ClearFormatting
With Selection.Find
.Text = "<image>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=3, Extend:=wdExtend
Selection.TypeBackspace
Selection.Find.ClearFormatting
With Selection.Find
.Text = "<enqueteur>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=11, Extend:=wdExtend
Selection.TypeBackspace
Selection.Find.ClearFormatting
With Selection.Find
.Text = "<DonneesMorpho>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=2, Extend:=wdExtend
Selection.TypeBackspace
Selection.Find.ClearFormatting
With Selection.Find
.Text = "<type>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveDown Unit:=wdParagraph, Count:=9, Extend:=wdExtend
Selection.TypeBackspace
End Sub

gmaxey
11-13-2013, 10:52 AM
Here is one way:

Deletes each paragraph if first character is "T"


Sub ScratchMacro1()
'A basic Word macro coded by Greg Maxey
Dim lngIndex As Long
For lngIndex = ActiveDocument.Paragraphs.Count To 1 Step -1
If ActiveDocument.Paragraphs(lngIndex).Range.Characters(1) = "T" Then
ActiveDocument.Paragraphs(lngIndex).Range.Delete
End If
Next lngIndex
End Sub

Pwyll2
11-13-2013, 09:22 PM
Thanks for your answer, unfortunately, I don't know how to adapt your code to my macro, especially because my macro is meant to delete several paragraphs within every "block of text", so I guess there will be quite a lot of things to change... I don't know which ones nor how, I know nothing in VB :(

fumei
11-13-2013, 09:43 PM
Start by describing EXACTLY what your requirements are. No vagueness. For example "block of text" is actually a meaningless phrase. The whole document is a "block of text". The second sentence of a fifteen sentence paragraph is a "block of text". The entire paragraph is a "block of text". Four paragraphs together is a "block of text".

fumei
11-13-2013, 09:48 PM
And btw, your code posted does not appear to have much to do with your original question.

Pwyll2
11-14-2013, 07:33 AM
I'm sorry for being vague. I thought it was clear :(


Start by describing EXACTLY what your requirements are. No vagueness. For example "block of text" is actually a meaningless phrase. The whole document is a "block of text". The second sentence of a fifteen sentence paragraph is a "block of text". The entire paragraph is a "block of text". Four paragraphs together is a "block of text".

I see; actually, my document is divided in paragraphs that have the same structure. But in Word, every line of these paragraphs is also considered as a paragraph itself since there's a paragraph jump at the end of every line! That's why I thought it would be clearer if I used "block of text" to talk about the "big paragraphs".
Anyway, this is the kind of stuff I have to modify with my macro:


<identifiant>thaeyhdjehdfjč</identifiant>
<FichierSon>hsyhysrhsyhj</FichierSon>
<image>hqyhqyhsr</image>
<TitreImage>hshysyh</TitreImage>
<lieu>shysyhsrh</lieu>
<informateur>syhsyhsy</informateur>
<enqueteur>hysshsrh</enqueteur>
<decoupeur>hshysyhs</decoupeur>
<transcripteur>hyshysyhsd</transcripteur>
<relecteur>shysyshy</relecteur>
<machine>RFrgGtrqgt</machine>
<FichierSource>qgtshysrjhtdy</FichierSource>
<duree>hyshysdhshys</duree>
<DateEnreg>hysrjuukjiy</DateEnreg>
<questionnaire>hysjdutjj</questionnaire>
<QuestionPosee>judujedjdj</QuestionPosee>
<contexte>udjdjfjfu</contexte>
<BretonLocal>jdujufyk,ifk,i</BretonLocal>
<phon>djuk,iykfy,fhy</phon>
<BretonStandard>dyhsssrjedu</BretonStandard>
<TraducFr>judtjujud</TraducFr>
<TraducLitt>dujsjsusu</TraducLitt>
<DonneesMorpho>qyjqyjsqjs</DonneesMorpho>
<DonneesSynt>jsysjuu</DonnesSynt>
<nt>sujsujndu</nt>
<type>sjsujstuy</type>
<theme>jzstjuetdjudt</theme>
<MotsClesFr>jeutjedtjd</MotsClesFr>
<MotsClesLocal>jetujetrču</MotsClesLocal>
<MotsClesStand>ahryjhsfj</MotsClesStand>
<RefAutreExtr>jssuuzst</RefAutreExtr>
<commentairePerso>seujyzuu</commentairePerso>
<DateCreation>sjučus</DateCreation>
<DateMiseAJour>jshysujsujsu</DateMiseAJour>



<identifiant>ntuedkfyi</identifiant>
<FichierSon>kiyfkfyilkgfyk</FichierSon>
<image>kdikfyik;</image>
<TitreImage>skyjnstuxdnj,jst</TitreImage>
<lieu>usdjdtjdt</lieu>
<informateur>dukjduk</informateur>
<enqueteur>qsujsfhnjxf</enqueteur>
<decoupeur>justjdutk</decoupeur>
<transcripteur>sukujgxh</transcripteur>
<relecteur>usjstujd</relecteur>
<machine>ujsdtukjedut</machine>
<FichierSource>sujxtujs</FichierSource>
<duree>utkjkeduk</duree>
<DateEnreg>jdtujdtyu</DateEnreg>
<questionnaire>judtk,yif</questionnaire>
<QuestionPosee>juetjed</QuestionPosee>
<contexte>jetudtj</contexte>
<BretonLocal>ujedukjeyi</BretonLocal>
<phon>kjedyikk,</phon>
<BretonStandard>uj,dekiyt</BretonStandard>
<TraducFr>kiydi;ky,</TraducFr>
<TraducLitt>k,eydik,dy</TraducLitt>
<DonneesMorpho>kryikfl;kf</DonneesMorpho>
<DonneesSynt>ydi;r</DonnesSynt>
<nt>,eydi;k,kriy</nt>
<type>eikyi,;edy</type>
<theme>jzsujzsutj</theme>
<MotsClesFr>juedt,tug</MotsClesFr>
<MotsClesLocal>jtujdtj</MotsClesLocal>
<MotsClesStand>jsuysrju</MotsClesStand>
<RefAutreExtr>jtduj,djny,</RefAutreExtr>
<commentairePerso>jetjudetu</commentairePerso>
<DateCreation>juetdjdt</DateCreation>
<DateMiseAJour>jetujudj</DateMiseAJour>


etc with loads of paragraphs like that (always the same tags and structure, but differents things between the tags, here I've written random letters of course)

And I want to delete the lines or groups of lines that start with certain tags, so that I get this (here I wrote them with bold characters so that you see which ones I want to keep, but of course, since it's XML code, in the original document there'll be no bold characters):


<FichierSon>hsyhysrhsyhj</FichierSon>
<informateur>syhsyhsy</informateur>
<BretonLocal>jdujufyk,ifk,i</BretonLocal>
<phon>djuk,iykfy,fhy</phon>
<BretonStandard>dyhsssrjedu</BretonStandard>
<TraducFr>judtjujud</TraducFr>
<TraducLitt>dujsjsusu</TraducLitt>
<nt>sujsujndu</nt>


<FichierSon>kiyfkfyilkgfyk</FichierSon>
<informateur>dukjduk</informateur>
<BretonLocal>ujedukjeyi</BretonLocal>
<phon>kjedyikk,</phon>
<BretonStandard>uj,dekiyt</BretonStandard>
<TraducFr>kiydi;ky,</TraducFr>
<TraducLitt>k,eydik,dy</TraducLitt>
<nt>,eydi;k,kriy</nt>






I hope it helps...
Thanks a lot!

fumei
11-14-2013, 12:44 PM
"And I want to delete the lines or groups of lines "

First off, "lines" can be a difficult thing thing in Word. It is much better (because that is how Word works) to stick with paragraphs. A paragraph in Word is any block of text (no matter how many sentence are in it) between two paragraph marks. Except for the very first paragraph, which of course does not have a paragraph mark before it...being the first one. So if every line has a paragraph mark at the end, then by definition it is a paragraph. There is NO such thing as a "big" paragraph in Word. Again, it is any block of text between two paragraph marks. Five sentence or 5,000 sentences, if all the text is linked to a single paragraph mark, then it is a single paragraph.

It sounds like something like what Greg posted is basically what you want. You go through the paragraph collection with a search string, if a paragraph starts with that string delete it.

Sub DeleteParagraphs()
Dim myList()
Dim lngIndex As Long
Dim var
myList = Array("FichierSon", "informateur", "BretonLocal", _
"phon", "BretonStandard", "TraducFr", "TraducLitt", "nt", ",")
For lngIndex = ActiveDocument.Paragraphs.Count To 1 Step -1
For var = 0 To UBound(myList())
If ActiveDocument.Paragraphs(lngIndex).Range.Words(2) = myList(var) Then
ActiveDocument.Paragraphs(lngIndex).Range.Delete
Exit For
End If
Next var
Next lngIndex
End Sub Comments:
The search list is built of an array. Note that the items in the array do NOT have the brackets <>. This is because Word considers the opening brackets as Words(1), and the text as Words(2). So the code checks the SECOND word of each paragraph (nt, NOT <nt>) to see if there is an match in the array. If there is, the paragraph is deleted.

Note also that this is a rather brute force piece of code and not very efficient. EVERY paragraph runs through EVERY word in the array (until it finds one of course, if found it exits that iteration). So if a paragraph does not have a start from the array, the code must go through ALL items in the array to determine that.

If you have a lot of paragraphs this will take a bit of time, depending on your computer processor.

macropod
11-14-2013, 03:43 PM
And I want to delete the lines or groups of lines that start with certain tags
You can actually do this quite quickly without a macro, using a wildcard Find/Replace. Suppose, for example, you want to delete all instances of:
<identifiant> ... </identifiant>

For that, the wildcard Find/Replace expression would be:
Find = (\<)(identifiant\>)*\1/\2^13
Replace = nothing

So, if you want to replace a series of such tagged ranges, all you need to change in the Find expression is 'identifiant'.

Similarly, if you want to delete:
<image> ... </image>
...
<lieu> ... </lieu>

the wildcard Find/Replace expression could be:
Find = (\<)(image\>)*\1/\2*(\<)(lieu\>)*\3/\4^13
Replace = nothing

Again, if you want to replace a series of such tagged ranges, all you need to change in the Find expression is 'image' and 'lieu'.

A paragraph in Word is any block of text (no matter how many sentence are in it) between two paragraph marks. Except for the very first paragraph, which of course does not have a paragraph mark before it...being the first one.
Gerry, I think you're making this more complicated than it need be! A paragraph in Word is any range, which may or may not contain text, containing a single paragraph mark or table cell marker at its end.

fumei
11-14-2013, 05:57 PM
Hmmm. First off, yes using Wildcards will work. Second, regarding my definition of paragraph, perhaps but... (ignoring table cell markers for now) while yes, they are ranges, that range start is defined by the existence of the previous paragraph mark plus 1. NOT just the existence of the terminating one. The start of the range goes back until it finds the previous paragraph mark.

Or, if you prefer, the NEXT paragraph starts at the last terminating paragraph mark (plus 1), and continues the range until it hits the NEXT paragraph mark.

This may be such quibbling that it is not worth bothering with, 'tis true. Just saying. A paragraph range .Start is always the previous paragraph range .End + 1. And yes, it is true, whether the range contains text or not is irrelevant.

Pwyll2
11-14-2013, 06:15 PM
Thanks for your answers...

Fumei: your macro doesn't work on my document :( It says "Error 5941" "The needed member of the collection doesn't exist" (I dunno if these are the proper words since my Word is in French so I've tried to translate for u!). I don't understand...

Macropod: I didn't know there were wildcards in Word, for find/replace. However, given I have to replace all these paragraphs by nothing in my document and that I have 3 other macros to do after that (I've not worked on them yet! actually I hoped I would find out how to change my current macros so that the task is done till the end of the document), it would take forever to make Find/replace with all that, that's why I'd prefer a macro...

but I'm very happy you're helping me because I couldn't find any real help on French language forums :(

fumei
11-14-2013, 06:28 PM
Can you post a sample document, removing any sensitive information? It does not have to be a full document, just enough to work with. Bold the paragraphs that you expect to be deleted.

macropod
11-14-2013, 06:29 PM
I didn't know there were wildcards in Word, for find/replace. However, given I have to replace all these paragraphs by nothing in my document and that I have 3 other macros to do after that (I've not worked on them yet! actually I hoped I would find out how to change my current macros so that the task is done till the end of the document), it would take forever to make Find/replace with all that, that's why I'd prefer a macro...
Apparently it also hasn't occurred to you that you can use Find/Replace in a macro! For example:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Replacement.Text = ""
.Text = "(\<)(identifiant\>)*\1/\2^13"
.Execute Replace:=wdReplaceAll
.Text = "(\<)(image\>)*\1/\2*(\<)(lieu\>)*\3/\4^13"
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub

Pwyll2
11-14-2013, 06:33 PM
What I wrote above would be ok I guess (I've written with bold characters what should be deleted though, as you asked)


<identifiant>thaeyhdjehdfjč</identifiant>
<FichierSon>hsyhysrhsyhj</FichierSon>
<image>hqyhqyhsr</image>
<TitreImage>hshysyh</TitreImage>
<lieu>shysyhsrh</lieu>
<informateur>syhsyhsy</informateur>
<enqueteur>hysshsrh</enqueteur>
<decoupeur>hshysyhs</decoupeur>
<transcripteur>hyshysyhsd</transcripteur>
<relecteur>shysyshy</relecteur>
<machine>RFrgGtrqgt</machine>
<FichierSource>qgtshysrjhtdy</FichierSource>
<duree>hyshysdhshys</duree>
<DateEnreg>hysrjuukjiy</DateEnreg>
<questionnaire>hysjdutjj</questionnaire>
<QuestionPosee>judujedjdj</QuestionPosee>
<contexte>udjdjfjfu</contexte>
<BretonLocal>jdujufyk,ifk,i</BretonLocal>
<phon>djuk,iykfy,fhy</phon>
<BretonStandard>dyhsssrjedu</BretonStandard>
<TraducFr>judtjujud</TraducFr>
<TraducLitt>dujsjsusu</TraducLitt>
<DonneesMorpho>qyjqyjsqjs</DonneesMorpho>
<DonneesSynt>jsysjuu</DonnesSynt>
<nt>sujsujndu</nt>
<type>sjsujstuy</type>
<theme>jzstjuetdjudt</theme>
<MotsClesFr>jeutjedtjd</MotsClesFr>
<MotsClesLocal>jetujetrču</MotsClesLocal>
<MotsClesStand>ahryjhsfj</MotsClesStand>
<RefAutreExtr>jssuuzst</RefAutreExtr>
<commentairePerso>seujyzuu</commentairePerso>
<DateCreation>sjučus</DateCreation>
<DateMiseAJour>jshysujsujsu</DateMiseAJour>






<identifiant>thaeyhdjehdfjč</identifiant>
<FichierSon>hsyhysrhsyhj</FichierSon>
<image>hqyhqyhsr</image>
<TitreImage>hshysyh</TitreImage>
<lieu>shysyhsrh</lieu>
<informateur>syhsyhsy</informateur>
<enqueteur>hysshsrh</enqueteur>
<decoupeur>hshysyhs</decoupeur>
<transcripteur>hyshysyhsd</transcripteur>
<relecteur>shysyshy</relecteur>
<machine>RFrgGtrqgt</machine>
<FichierSource>qgtshysrjhtdy</FichierSource>
<duree>hyshysdhshys</duree>
<DateEnreg>hysrjuukjiy</DateEnreg>
<questionnaire>hysjdutjj</questionnaire>
<QuestionPosee>judujedjdj</QuestionPosee>
<contexte>udjdjfjfu</contexte>
<BretonLocal>jdujufyk,ifk,i</BretonLocal>
<phon>djuk,iykfy,fhy</phon>
<BretonStandard>dyhsssrjedu</BretonStandard>
<TraducFr>judtjujud</TraducFr>
<TraducLitt>dujsjsusu</TraducLitt>
<DonneesMorpho>qyjqyjsqjs</DonneesMorpho>
<DonneesSynt>jsysjuu</DonnesSynt>
<nt>sujsujndu</nt>
<type>sjsujstuy</type>
<theme>jzstjuetdjudt</theme>
<MotsClesFr>jeutjedtjd</MotsClesFr>
<MotsClesLocal>jetujetrču</MotsClesLocal>
<MotsClesStand>ahryjhsfj</MotsClesStand>
<RefAutreExtr>jssuuzst</RefAutreExtr>
<commentairePerso>seujyzuu</commentairePerso>
<DateCreation>sjučus</DateCreation>
<DateMiseAJour>jshysujsujsu</DateMiseAJour>




<identifiant>thaeyhdjehdfjč</identifiant>
<FichierSon>hsyhysrhsyhj</FichierSon>
<image>hqyhqyhsr</image>
<TitreImage>hshysyh</TitreImage>
<lieu>shysyhsrh</lieu>
<informateur>syhsyhsy</informateur>
<enqueteur>hysshsrh</enqueteur>
<decoupeur>hshysyhs</decoupeur>
<transcripteur>hyshysyhsd</transcripteur>
<relecteur>shysyshy</relecteur>
<machine>RFrgGtrqgt</machine>
<FichierSource>qgtshysrjhtdy</FichierSource>
<duree>hyshysdhshys</duree>
<DateEnreg>hysrjuukjiy</DateEnreg>
<questionnaire>hysjdutjj</questionnaire>
<QuestionPosee>judujedjdj</QuestionPosee>
<contexte>udjdjfjfu</contexte>
<BretonLocal>jdujufyk,ifk,i</BretonLocal>
<phon>djuk,iykfy,fhy</phon>
<BretonStandard>dyhsssrjedu</BretonStandard>
<TraducFr>judtjujud</TraducFr>
<TraducLitt>dujsjsusu</TraducLitt>
<DonneesMorpho>qyjqyjsqjs</DonneesMorpho>
<DonneesSynt>jsysjuu</DonnesSynt>
<nt>sujsujndu</nt>
<type>sjsujstuy</type>
<theme>jzstjuetdjudt</theme>
<MotsClesFr>jeutjedtjd</MotsClesFr>
<MotsClesLocal>jetujetrču</MotsClesLocal>
<MotsClesStand>ahryjhsfj</MotsClesStand>
<RefAutreExtr>jssuuzst</RefAutreExtr>
<commentairePerso>seujyzuu</commentairePerso>
<DateCreation>sjučus</DateCreation>
<DateMiseAJour>jshysujsujsu</DateMiseAJour>



<identifiant>thaeyhdjehdfjč</identifiant>
<FichierSon>hsyhysrhsyhj</FichierSon>
<image>hqyhqyhsr</image>
<TitreImage>hshysyh</TitreImage>
<lieu>shysyhsrh</lieu>
<informateur>syhsyhsy</informateur>
<enqueteur>hysshsrh</enqueteur>
<decoupeur>hshysyhs</decoupeur>
<transcripteur>hyshysyhsd</transcripteur>
<relecteur>shysyshy</relecteur>
<machine>RFrgGtrqgt</machine>
<FichierSource>qgtshysrjhtdy</FichierSource>
<duree>hyshysdhshys</duree>
<DateEnreg>hysrjuukjiy</DateEnreg>
<questionnaire>hysjdutjj</questionnaire>
<QuestionPosee>judujedjdj</QuestionPosee>
<contexte>udjdjfjfu</contexte>
<BretonLocal>jdujufyk,ifk,i</BretonLocal>
<phon>djuk,iykfy,fhy</phon>
<BretonStandard>dyhsssrjedu</BretonStandard>
<TraducFr>judtjujud</TraducFr>
<TraducLitt>dujsjsusu</TraducLitt>
<DonneesMorpho>qyjqyjsqjs</DonneesMorpho>
<DonneesSynt>jsysjuu</DonnesSynt>
<nt>sujsujndu</nt>
<type>sjsujstuy</type>
<theme>jzstjuetdjudt</theme>
<MotsClesFr>jeutjedtjd</MotsClesFr>
<MotsClesLocal>jetujetrču</MotsClesLocal>
<MotsClesStand>ahryjhsfj</MotsClesStand>
<RefAutreExtr>jssuuzst</RefAutreExtr>
<commentairePerso>seujyzuu</commentairePerso>
<DateCreation>sjučus</DateCreation>
<DateMiseAJour>jshysujsujsu</DateMiseAJour>

fumei
11-14-2013, 09:24 PM
I asked for a file! You just posted the same thing as before. Nevertheless, I created a document, copied some of the text, copied the code into it and assigned a keyboard shortcut. Ctrl-d executes the code...which I did not change, other than to add all the items to search for, to perform the deletion.

Sub DeleteParagraphs()
Dim myList()
Dim lngIndex As Long
Dim var
myList = Array("identifiant", "image", "TitreImage", "lieu", _
"enqueteur", "decoupeur", "transcripteur", "relecteur", "machine", _
"FichierSource", "duree", "DateEnreg", "questionnaire", _
"QuestionPosee", "contexte", "DonneesMorpho", "DonneesSynt", _
"type", "theme", "MotsClesFr", "MotsClesLocal", "MotsClesStand", _
"RefAutreExtr", "commentairePerso", "DateCreation", "DateMiseAJour", ",")
For lngIndex = ActiveDocument.Paragraphs.Count To 1 Step -1
For var = 0 To UBound(myList())
If ActiveDocument.Paragraphs(lngIndex).Range.Words(2) = myList(var) Then
ActiveDocument.Paragraphs(lngIndex).Range.Delete
Exit For
End If
Next var
Next lngIndex
End Sub
Document attached. Open it and press Ctrl-d. It works as I state. That is why i asked for your file. I do not know what you are ACTUALLY doing. Perhaps you should go through the wildcard route that macropod posted, as it will work. Good luck.

fumei
11-14-2013, 09:28 PM
In many ways macropod's solution is better. Using VBA is not always the best, or easiest, solution.

macropod
11-14-2013, 09:46 PM
FWIW, my macro solution would be:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Replacement.Text = ""
.Text = "(\<)(identifiant\>)*\1/\2^13"
.Execute Replace:=wdReplaceAll
.Text = "(\<)(image\>)*\1/\2*(\<)(lieu\>)*\3/\4^13"
.Execute Replace:=wdReplaceAll
.Text = "(\<)(enqueteur\>)*\1/\2*(\<)(contexte\>)*\3/\4^13"
.Execute Replace:=wdReplaceAll
.Text = "(\<)(DonneesMorpho\>)*\1/\2*(\<)(DonneesSynt\>)*\3/\4^13"
.Execute Replace:=wdReplaceAll
.Text = "(\<)(type\>)*\1/\2*(\<)(DateMiseAJour\>)*\3/\4^13"
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
Note: As coded, it won't process the lines:
<DonneesMorpho>qyjqyjsqjs</DonneesMorpho>
<DonneesSynt>jsysjuu</DonnesSynt>
because the tags on the last line are mis-matched.

fumei
11-14-2013, 09:50 PM
and there ya go

macropod
11-14-2013, 09:54 PM
I think I know which would be quicker...

fumei
11-14-2013, 10:04 PM
Pwyll2, although both macropod and I have posted radically different VBA code (although both use VBA!), macropod's should be a bit faster. That is because Find and Replace is a fast native internal Word function and does not require essentially brute force processing of the paragraph collection. Depending on processor speed and RAM, this may or may not be noticeable. Certainly for the few paragraphs being used for this sample it makes no difference at all. For a large document it could.

The main advantage of my version is that editing to add further items is (I think) easier, and more intuitive. Just add another "whatever" to the array.

fumei
11-14-2013, 10:06 PM
You read my mind and posted before I posted the above. Yup, your code should be faster, all other factors being equal.

Pwyll2
11-15-2013, 12:42 PM
Thanks
I've tested both of your macros with my sample text above...
Fumei, your macro doesn't work here, it says the same as I said earlier, Error 5941 :(

Macropod, yours doesn't work either, actually it doesn't do what is expected: if I test it on the sample document above, the macro deletes completely 3 of the 4 paragraphs, and the one that is left, keeps all its tags even those that had to be deleted.
Maybe it's because the macro deletes all the text that is between the first opening tag (in the first "paragraph") and the last one (in the last paragraph), instead of deleting what is between 2 tags in the 1st paragraph, then in the 2 same tags in the 2nd paragraph, etc... :(

(btw I have corrected the typo in the tag "DonneesSynt")

By the way, what do these mean?:

*\1/\2*


*\3/\4^13

thanks

macropod
11-15-2013, 01:45 PM
Macropod, yours doesn't work either, actually it doesn't do what is expected: if I test it on the sample document above, the macro deletes completely 3 of the 4 paragraphs, and the one that is left, keeps all its tags even those that had to be deleted. I don't understand why :(
It's probably because what I'd previously understood to be paragraphs (i.e. each line of your tagged expressions) aren't paragraphs at all, but are lines within a paragraph.
By the way, what do these mean?:

*\1/\2*


*\3/\4^13
With wildcards, when you enclose part of an expression in parentheses, you can refer to that part of the expression later on (in either the Find or Replace expressions), by its 'index' in the parenthetic sequence. Thus, as I have two such parenthetic expressions in "(\<)(identifiant\>)" I can refer to them as \1 and \2. The '/' is simply the '/' character you find in the closing tag and the '^13' is a paragraph break.

Although I could modify my previous macro to take account of your tags being lines within a paragraph, I've decided to take a different approach, drawing on Gerry's:

Sub DeleteTaggedLines()
Application.ScreenUpdating = False
Dim strTags As String, i As Long
strTags = "identifiant|identifiant,image|lieu," & _
"enqueteur|contexte,DonneesMorpho|DonneesSynt," & _
"type|DateMiseAJour"
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Replacement.Text = ""
For i = 0 To UBound(Split(strTags, ","))
.Text = "\<" & Split(Split(strTags, ",")(i), "|")(0) & "\>*\</" & Split(Split(strTags, ",")(i), "|")(1) & "\>^l"
.Execute Replace:=wdReplaceAll
.Text = "^l\<" & Split(Split(strTags, ",")(i), "|")(0) & "\>*\</" & Split(Split(strTags, ",")(i), "|")(1) & "\>"
.Execute Replace:=wdReplaceAll
Next
End With
Application.ScreenUpdating = True
End Sub
With this approach, one still uses Find/Replace, but it also works with an input list consisting of the start and end tag name pairs, each separated by a '|' and pairs separated by ','. This makes the code much simpler to maintain - you can simply add/delete tag pairs in the 'strTags' list. You'll note that there are two Find/Replace operations for each tag. That's so tags before the last line of a paragraph are handles differently to the other lines; in practice, only one of the two replacements will occur.

fumei
11-15-2013, 02:15 PM
"Fumei, your macro doesn't work here, it says the same as I said earlier, Error 5941"

Well it DOES work here. I have made two different documents and it worked on both. This is why I asked you to post an actual file! But hey, if you do not want to bother...nether will I.

Pwyll2
11-15-2013, 03:31 PM
Macropod > cool, it works !!!!

Fumei > if the same macro used on the same text works on your pc and not on mine, I don't think changing what is between the tags would change anything... there must be some paranormal reason :rotlaugh:

Anyway, thanks a million guys!
I have 2 other macros to do (they are about deleting other things and moving paragraphs), I'll try by myself with the help of your macros, maybe I'll manage on my own.
Thanks again !!! :friends:

Pwyll2
11-15-2013, 04:43 PM
Ok, I don’t manage... I’m hopeless :banghead:.
In this 2nd macro, I want to do 2 tasks :

- replacing the opening tags by other non-XML markers (note : these new markers should be followed by a space) :
<FichierSon> by \sfx
<informateur> by \xinfor
<BretonLocal> by \xbrloc
<phon> by \xfon
<BretonStandard> by \xv
<TraducFr> by \xtrad
<TraducLitt> by \lt
<nt> by \nt


- deleting the closing tags :
</FichierSon>
</informateur>
</BretonLocal>
</phon>
</BretonStandard>
</TraducFr>
</TraducLitt>
</nt>


For instance :

<FichierSon>hsyhysrhsyhj</FichierSon>
<informateur>syhsyhsy</informateur>
<BretonLocal>jdujufyk,ifk,i</BretonLocal>
<phon>djuk,iykfy,fhy</phon>
<BretonStandard>dyhsssrjedu</BretonStandard>
<TraducFr>judtjujud</TraducFr>
<TraducLitt>dujsjsusu</TraducLitt>
<nt>sujsujndu</nt>


should become :

\sfx hsyhysrhsyhj
\xinfor syhsyhsy
\xbrloc jdujufyk,ifk,i
\xfon djuk,iykfy,fhy
\xv dyhsssrjedu
\xtrad judtjujud
\lt dujsjsusu
\nt sujsujndu

I guess it's very easy to do...
Thanks a lot in advance guys!!! :friends:

macropod
11-15-2013, 05:32 PM
Try:

Sub DeleteTags()
Application.ScreenUpdating = False
Dim strTags As String, i As Long
strTags = "FichierSon|sfx,informateur|xinfor,BretonLocal|xbrloc," & _
"phon|xfon,BretonStandard|xv,TraducFr|xtrad,TraducLitt|lt,nt|nt"
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
For i = 0 To UBound(Split(strTags, ","))
.Text = "(\<)(" & Split(Split(strTags, ",")(i), "|")(0) & "\>)(*)\1/\2"
.Replacement.Text = "^92" & Split(Split(strTags, ",")(i), "|")(1) & " \3"
.Execute Replace:=wdReplaceAll
Next
End With
Application.ScreenUpdating = True
End Sub
Not very easy at all - even when one knows how to use wildcards, it takes time to analyse the problem and work how whether & how they might be applied in a given context. Having done that, the programming isn't all that difficult - as you'll see, the code's just a variation on what I posted before.

Pwyll2
11-15-2013, 09:03 PM
PERFECT! THANX!!!!

And now the 3rd and last macro for this time, will be to change the order of the paragraphs (well, lines) I've got from the preceding macro.

So this:

\sfx hsyhysrhsyhj
\xinfor syhsyhsy
\xbrloc jdujufyk,ifk,i
\xfon djuk,iykfy,fhy
\xv dyhsssrjedu
\xtrad judtjujud
\lt dujsjsusu
\nt sujsujndu


should become this:

\xv dyhsssrjedu
\sfx hsyhysrhsyhj
\xfon djuk,iykfy,fhy
\xinfor syhsyhsy
\xbrloc jdujufyk,ifk,i
\xtrad judtjujud
\lt dujsjsusu
\nt sujsujndu

Thanks a million in advance :friends:

macropod
11-15-2013, 09:52 PM
Again, the bulk of the work goes into figuring out the logic of what has to be moved. In this case, only two moves are needed. The following two macros show how it can be done (a) with a list and (b) without a list:

Sub MoveTags1()
Application.ScreenUpdating = False
Dim strTags As String, i As Long
strTags = "sfx|xv,xinfor|xfon"
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Text = "^13"
.Replacement.Text = "^l^p"
.Execute Replace:=wdReplaceAll
For i = 0 To UBound(Split(strTags, ","))
.Text = "(\\" & Split(Split(strTags, ",")(i), "|")(0) & "*)(\\" & Split(Split(strTags, ",")(i), "|")(1) & "*^l)"
.Replacement.Text = "\2\1"
.Execute Replace:=wdReplaceAll
Next
.Text = "^l^13"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub

Sub MoveTags2()
Application.ScreenUpdating = False
Dim strTags As String, i As Long
strTags = "sfx|xv,xinfor|xfon"
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Text = "^13"
.Replacement.Text = "^l^p"
.Execute Replace:=wdReplaceAll
.Text = "(\^92sfx*)(\^92xv*^l)"
.Replacement.Text = "\2\1"
.Execute Replace:=wdReplaceAll
.Text = "(\^92xinfor*)(\^92xfon*^l)"
.Execute Replace:=wdReplaceAll
.Text = "^l^13"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
Although there's only two moves, four F/R operations are used. The first makes sure there's a line-break before each paragraph-break, so the two moves need only consider line-breaks; the last one undoes that.

PS: The board software insisted on converting some of the macro code in the second sub to links, so I've had to use a different form of the same expression for that.

fumei
11-15-2013, 11:46 PM
Fumei > if the same macro used on the same text works on your pc and not on mine, I don't think changing what is between the tags would change anything... there must be some paranormal reason
1. there is nothing paranormal
2. who said anything about changing what is between the tags. Not me. I have no idea what you are talking abpout.
3. who says that it IS the same text. I do not know that as you did not supply what YOU have - even after asking three times. All I can see is what you posted, and the FACT that it did not indicate (as per macropod's post) the lines were NOT paragraphs is evidence that what you have is NOT the same text.

In fact, the same issue would cause an issue with my code, but I am sure not inclined to help in any way at all now.

fumei
11-16-2013, 12:00 AM
"if the same macro used on the same text works on your pc and not on mine"

Are you saying my macro in MY file did not work on your machine?

Pwyll2
11-16-2013, 06:33 AM
Fumei: I'm very sorry, I didn't wanted to make you angry at all... Your code didn't work on my pc and I just didn't understand why... Actually even when it works I don't understand why either, I'm not a computer scientist, I'm a linguist...
I didn't understand properly what you asked - you asked me to "supply what I have"... what did you mean? my code? my real text to convert? my sample paragraphs? (which follow the same model as the real text, only what is between the tags is different)...


All I can see is what you posted, and the FACT that it did not indicate (as per macropod's post) the lines were NOT paragraphs is evidence that what you have is NOT the same text.

they are paragraphs, there's a paragraph jumps at the end of each line... I think we didn't understand each other for vocabulary reasons (sorry my first language is not English and I don't know well the vocabulary used by Word - my Word is in French).
And I'm sorry for having made you waste your time... :(

Macropod: it works perfectly :-) THANKS a million! :bow:

macropod
11-16-2013, 07:02 AM
Pwyll2: Gerry (fumei) is understandably disgruntled because you assured in in post #6 that every line was a paragraph:

my document is divided in paragraphs that have the same structure. But in Word, every line of these paragraphs is also considered as a paragraph itself since there's a paragraph jump at the end of every line!
The fact is every line is NOT a paragraph - they're simply lines separated by manual line breaks within the same paragraph. This isn't a language issue; it's a question of you giving a quite inaccurate description of what you were asking us to work with. Gerry is quite justifiably annoyed too that, having asked you to supply a document containing the actual content, all you did was to post more of the same stuff that doesn't allow us to see what you're really working with. Sure, I figured it out, but I should have needed to. I should have been able to rely on what you said and you should have supplied a document as you were asked. The most likely reason that Gerry's macro gave the '5941' error is that, in addition to the misleading advice you did give, you also didn't tell us your document also contains empty paragraphs - or, at least, paragraphs with less than two 'words'. So, yes, you did waste Gerry's time - and some of mine.

fumei
11-16-2013, 07:22 PM
I didn't understand properly what you asked - you asked me to "supply what I have"...
Actually I did NOT ask you that. I asked:

Can you post a sample document, removing any sensitive information?
Document. I do not think I could be more clear. Document.

As you did not, I could hardly see that you did not have what you said were paragraphs, nor empty paragraphs. That is why I wanted to see what you were actually working with. Heck you did not even say if the document I posted worked for you. You did not have even download it, just open the posted file and press Ctrl-d. You can see that it works doing what you asked for. BUT with full paragraphs.

I could have answered the 5941 error immediately. Please, in the future try to answer questions asked of you. I also asked

Are you saying my macro in MY file did not work on your machine?
and you have not answered either. And yes I am annoyed.

Pwyll2
11-27-2013, 06:39 PM
Hello guys

Sorry again.


The fact is every line is NOT a paragraph - they're simply lines separated by manual line breaks within the same paragraph.

to me, a manual line break is Shift+Enter, which can be seen in Word as an arrow that goes down and then left. And a paragraph break is what happens when you just press Enter... and in my document there's no Shift+Enter line breaks, only "paragraph breaks"...


Gerry is quite justifiably annoyed too that, having asked you to supply a document containing the actual content, all you did was to post more of the same stuff that doesn't allow us to see what you're really working with.

Actually I thought sending a sample text in a word document and posting the same text directly in my message wouldn't make any difference. That's why I didn't understood why you asked for a word document attached to my post...


The most likely reason that Gerry's macro gave the '5941' error is that, in addition to the misleading advice you did give, you also didn't tell us your document also contains empty paragraphs - or, at least, paragraphs with less than two 'words'.

in my first sample text there were 2 paragraphs with a few empty lines (paragraph jumps) between them... (if it's what you call empty paragraphs)


So, yes, you did waste Gerry's time - and some of mine.

So sorry again. Well you didn't really waste your time since you found a solution to my problem... thanks again for that.






Actually I did NOT ask you that. I asked:


Can you post a sample document, removing any sensitive information?


Document. I do not think I could be more clear. Document.

I thought my few sample paragraphs could be called "a document"... since in a Word document there would be exactly the same thing. To me if you copy-and-paste a text from a word document into a post on a forum, it is still a document. Language problem, as I said. Sorry for that, but it's not my fault... Dealing with a subject I know nothing about in a language that isn't mine, is far from being easy for me. If you had to ask questions about a subject you don't know in a French forum (if you don't really master French), I guess you might not understand everything properly either...



I could have answered the 5941 error immediately. Please, in the future try to answer questions asked of you. I also asked
Are you saying my macro in MY file did not work on your machine?

It worked in your word document and I couldn't understand why the very same text would be transformed in your document and not when I used the very same text in my document. That is beyond me, I still don't understand where is the difference. So I thought your macro wouldn't work in my other files, that's why I didn't answer... :doh:

Sorry again and thanks...

fumei
11-28-2013, 03:26 PM
It worked in your word document and I couldn't understand why the very same text would be transformed in your document and not when I used the very same text in my document."

That is precisely WHY I wanted to see one of you documents. TEXT copied into a forum window is NOT the same as an actual document file. Sample text (which did not even show whether they are real paragraphs or not) is not the same as a real document


Actually I thought sending a sample text in a word document and posting the same text directly in my message wouldn't make any difference. That's why I didn't understood why you asked for a word document attached to my post...

You did not need to understand. It was a request. You made an assumption (incorrectly) that it would make no difference, and another assumption (incorrectly) that I made the request for some trivial reason. I asked because it DOES make a difference.

fumei
11-28-2013, 03:40 PM
Look, I am not trying to trash you. I asked for a document in order to try and help you, Hopefully in the future you will see that someone asking for information is asking for a reason.

Pwyll2
11-28-2013, 04:23 PM
That is precisely WHY I wanted to see one of you documents. TEXT copied into a forum window is NOT the same as an actual document file. Sample text (which did not even show whether they are real paragraphs or not) is not the same as a real document

Yeah, you know that because you know Visual Basic and how it works... :)
Actually I have copied-and-pasted my sample paragraphs from Word to the forum window, so I thought you would get exactly the same document if you copied-and-pasted the same stuff on Word on your pc... :)

fumei
11-28-2013, 11:06 PM
It has nothing (in itself) at all to do with VBA. It has to do with something that I stated clearly worked in my document (and it DID, and does ), and YOU stating that it was not working in YOUR document. Logically, that means there is something different in your DOCUMENT. Therefore, I wanted to see....your document. The problem was - in your own words - there, in your document. Not with what is on the forum. The document.

What we see in a browser, here in a forum, is not a document. It is text filtered through the graphical lens of HTML (or PHP or whatever). This may be a good learning moment for you, and others. A web page is not a Word document. At BEST, copying here shows only the text, never the environment of Word. And as was shown by the 5941 error, the text pasted did NOT indicate the reality of what was in the actual...wait for it...document.

Anyway, in the end, I am glad it worked out for you.

Pwyll2
12-16-2013, 07:59 AM
Hello again!
Sorry to bother you again!
There is a slight change in my XML documents (I had to change that so that it is similar to my colleagues' ones).

Instead of having
<identifiant></identifiant>

at the very beginning of every paragraph, I now have:
<item id="whatever">
("whatever" here is a reference number that changes everytime) and the closing tag </item> at the end of each paragraph, ie.


<item id="PYK-56264-RB-0002">
<FichierSon>PYK-56264-RB-0002.mp3</FichierSon>
<image></image>
<TitreImage></TitreImage>
<lieu>Kernascléden, Meslan, Priziac, Le Croisty</lieu>
<informateur>RB</informateur>
<enqueteur>PYK, LC</enqueteur>
<decoupeur>PYK</decoupeur>
<transcripteur>LC</transcripteur>
<relecteur>PYK</relecteur>
<machine>Marantz</machine>
<FichierSource></FichierSource>
<duree></duree>
<DateEnreg></DateEnreg>
<questionnaire></questionnaire>
<QuestionPosee></QuestionPosee>
<contexte></contexte>
<BretonLocal>Mén ‘pes hoń kaůet ?</BretonLocal>
<phon></phon>
<BretonStandard>Men ho peus eń kavet ?</BretonStandard>
<TraducFr>Oů l’avez-vous trouvé ?</TraducFr>
<TraducLitt></TraducLitt>
<DonneesMorpho></DonneesMorpho>
<DonneesSynt></DonneesSynt>
<nt></nt>
<type></type>
<theme></theme>
<MotsClesFr></MotsClesFr>
<MotsClesLocal></MotsClesLocal>
<MotsClesStand></MotsClesStand>
<RefAutreExtr></RefAutreExtr>
<commentairePerso></commentairePerso>
<DateCreation>13-12-2013</DateCreation>
<DateMiseAJour></DateMiseAJour>
</item>


So I need to change the first Macro, which was the following, so that the opening tag <item....> and the closing one at the very end </item> are deleted. My macro was this:


Sub XmlToLp1SupprimerChampsInutiles()
Application.ScreenUpdating = False
Dim strTags As String, i As Long
strTags = "identifiant|identifiant,image|lieu," & _
"enqueteur|contexte,DonneesMorpho|DonneesSynt," & _
"type|DateMiseAJour"
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Replacement.Text = ""
For i = 0 To UBound(Split(strTags, ","))
.Text = "\<" & Split(Split(strTags, ",")(i), "|")(0) & "\>*\</" & Split(Split(strTags, ",")(i), "|")(1) & "\>^l"
.Execute Replace:=wdReplaceAll
.Text = "^l\<" & Split(Split(strTags, ",")(i), "|")(0) & "\>*\</" & Split(Split(strTags, ",")(i), "|")(1) & "\>"
.Execute Replace:=wdReplaceAll
Next
End With
Application.ScreenUpdating = True
End Sub



what should we change in the macro so that the "item" tags are deleted? (see the attachment with the real document)

Thanks a lot in advance! :)

macropod
12-16-2013, 02:09 PM
After 'Next' insert:


.Text = "\<item id=[!\>]@\>^l"
.Execute Replace:=wdReplaceAll
.Text = "^l\</item\>"
.Execute Replace:=wdReplaceAll

Pwyll2
12-16-2013, 05:13 PM
It doesn't work :dunno
should I do something else, apart from copy and paste that code after "Next" ?

thanks

macropod
12-16-2013, 10:47 PM
Well, given that you reckoned the macro you quoted worked OK for what it was coded to do, I didn't bother looking at your attachment, since all that was needed was to add two more F/R operations of the same kind to the code. What I now find is that your attachment has a separate paragraph for each line, rather than manual line breaks. This goes back to what I said before. Remember how you said my code "works perfectly"? That was for code that looked for line breaks, not paragraph breaks. And that's what one gets in Word if one copies & pastes the strings you'd posted here into a document. Remember too how, when I said "The fact is every line is NOT a paragraph - they're simply lines separated by manual line breaks within the same paragraph", you came back with:

in my document there's no Shift+Enter line breaks, only "paragraph breaks"..."
Well, if that was true the code that you said "works perfectly" wouldn't have worked then and won't work now, even without the new code. So, either the code worked before, and you had line breaks then, or it never worked at all. There is no in-between.

Now that I have looked at your attachment, I can see that making the macro work with what you now have means replacing every instance of ^l in the code with ^13. Then and only then will it work.

Pwyll2
12-17-2013, 06:30 AM
Thanks! only one problem left: when I run the 3 macros one after another, at the end it's ok except that there's a paragraph break (ie. a blank line) just before \trad... (a normal paragraph break at the end of the preceding line of code, plus another one below, which makes like a blank line in the document) do you know why and how to change that?
Here are the 3 macros:
Thanks a lot!


Sub XmlToLp1SupprimerChampsInutiles()
Application.ScreenUpdating = False
Dim strTags As String, i As Long
strTags = "identifiant|identifiant,image|lieu," & _
"enqueteur|contexte,DonneesMorpho|DonneesSynt," & _
"type|DateMiseAJour"
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Replacement.Text = ""
For i = 0 To UBound(Split(strTags, ","))
.Text = "\<" & Split(Split(strTags, ",")(i), "|")(0) & "\>*\</" & Split(Split(strTags, ",")(i), "|")(1) & "\>^13"
.Execute Replace:=wdReplaceAll
.Text = "^13\<" & Split(Split(strTags, ",")(i), "|")(0) & "\>*\</" & Split(Split(strTags, ",")(i), "|")(1) & "\>"
.Execute Replace:=wdReplaceAll

Next

.Text = "\<item id=[!\>]@\>"
.Execute Replace:=wdReplaceAll
.Text = "\</item\>"
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub


---------------------------


Sub XmlToLp2SupprimerTagsFermantsEtConvertirMarqueurs()
Application.ScreenUpdating = False
Dim strTags As String, i As Long
strTags = "FichierSon|sfx,informateur|xinfor,BretonLocal|xbrloc," & _
"phon|xfon,BretonStandard|xv,TraducFr|xtrad,TraducLitt|lt,nt|nt"
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
For i = 0 To UBound(Split(strTags, ","))
.Text = "(\<)(" & Split(Split(strTags, ",")(i), "|")(0) & "\>)(*)\1/\2"
.Replacement.Text = "^92" & Split(Split(strTags, ",")(i), "|")(1) & " \3"
.Execute Replace:=wdReplaceAll
Next
End With
Application.ScreenUpdating = True
End Sub

-----------------------------------------------------

Sub XmlToLp3MettreMarqueursDansOrdreLp()
Application.ScreenUpdating = False
Dim strTags As String, i As Long
strTags = "sfx|xv,xinfor|xfon"
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = True
.Text = "^13"
.Replacement.Text = "^13^p"
.Execute Replace:=wdReplaceAll
For i = 0 To UBound(Split(strTags, ","))
.Text = "(\\" & Split(Split(strTags, ",")(i), "|")(0) & "*)(\\" & Split(Split(strTags, ",")(i), "|")(1) & "*^13)"
.Replacement.Text = "\2\1"
.Execute Replace:=wdReplaceAll
Next
.Text = "^13^13"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub

macropod
12-17-2013, 04:19 PM
I can see that you're using different code to what I've advised, for both XmlToLp1SupprimerChampsInutiles and XmlToLp3MettreMarqueursDansOrdreLp. I'll leave it to you to figure out which sub is causing the problem, why and how to fix it.

Pwyll2
12-17-2013, 04:50 PM
Where is it different??? I've changed ^l to ^13 as you told, I've copied and pasted your code after "Next" as you told...
apart from that, what is different? :dunno

macropod
12-17-2013, 05:32 PM
Well, I did not suggest the changes you made to the XmlToLp3MettreMarqueursDansOrdreLp macro - I suggested changing only the XmlToLp1SupprimerChampsInutiles macro. And, where I suggested changing the XmlToLp1SupprimerChampsInutiles macro, you've deleted two instances of ^l from the additional code instead of changing them to ^13.

You make it very hard for anyone to help you solve your problems!!!

Pwyll2
12-17-2013, 10:09 PM
Well, I did not suggest the changes you made to the XmlToLp3MettreMarqueursDansOrdreLp macro - I suggested changing only the XmlToLp1SupprimerChampsInutiles macro.

sh*te... I did "find/replace" in the Macros' page in Word, I didn't know it would change all occurrences at the same time, I thought it would only change them in the Macro I was working on :( I thought the separation lines between the macros would stop it :(


And, where I suggested changing the XmlToLp1SupprimerChampsInutiles macro, you've deleted two instances of ^l from the additional code instead of changing them to ^13.

Yes because I noticed my XML code, when copied and pasted in Word, had no line jumps but only paragraph jumps!
I see the problem now... looks like sometimes when I copy and paste my XML document to a word document, each line ends with a paragraph jump ; and other times, they end with a line jump. I didn't notice that before. That's why we had problems with the line jumps and paragraph jumps earlier. The problem is that I open my XML stuff with different software to make different tasks: Notepad, Notepad++, XML Notepad 2007 and Word. I guess some of them make a line jump when I push Enter, and others make a paragraph jump, and I didn't verify that ; that's why your macros sometimes worked and other times they didn't work :(

Thanks again and sorry again...

macropod
12-17-2013, 11:11 PM
OK, so you're importing text that could be using line breaks, or it could be using paragraph breaks. That's no big deal - it's quite easy to cater for both. But why are you using three separate macros instead of doing the whole process with just one? Unless you're getting data that comes in the different stages for what each macro does, the only reason I can see for that is that you never gave a single specification of what you were starting with and what you were trying to achieve.