Consulting

Page 1 of 3 1 2 3 LastLast
Results 1 to 20 of 48

Thread: Make macros in Word

  1. #1
    VBAX Regular
    Joined
    Nov 2013
    Posts
    30
    Location

    Make macros in Word

    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

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,339
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Nov 2013
    Posts
    30
    Location
    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

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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".

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    And btw, your code posted does not appear to have much to do with your original question.

  6. #6
    VBAX Regular
    Joined
    Nov 2013
    Posts
    30
    Location
    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!

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "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.

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Pwyll2 View Post
    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'.
    Quote Originally Posted by fumei View Post
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #9
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  10. #10
    VBAX Regular
    Joined
    Nov 2013
    Posts
    30
    Location
    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

  11. #11
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  12. #12
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by Pwyll2 View Post
    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
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  13. #13
    VBAX Regular
    Joined
    Nov 2013
    Posts
    30
    Location
    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>

  14. #14
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.
    Attached Files Attached Files

  15. #15
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    In many ways macropod's solution is better. Using VBA is not always the best, or easiest, solution.

  16. #16
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  17. #17
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    and there ya go

  18. #18
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    I think I know which would be quicker...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  19. #19
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    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.

  20. #20
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    You read my mind and posted before I posted the above. Yup, your code should be faster, all other factors being equal.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •