Page 2 of 3 FirstFirst 1 2 3 LastLast
Results 21 to 40 of 48

Thread: Make macros in Word

  1. #21
    VBAX Regular
    Joined
    Nov 2013
    Posts
    30
    Location
    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
    Last edited by Pwyll2; 11-15-2013 at 01:24 PM.

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

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

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

    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 !!!
    Last edited by Pwyll2; 11-15-2013 at 04:43 PM.

  5. #25
    VBAX Regular
    Joined
    Nov 2013
    Posts
    30
    Location
    Ok, I don’t manage... I’m hopeless .
    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!!!

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

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

  8. #28
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    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.
    Last edited by macropod; 11-15-2013 at 10:04 PM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

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

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

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

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

  13. #33
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,710
    Location
    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.

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

    Sorry again and thanks...




  15. #35
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,710
    Location
    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.

  16. #36
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,710
    Location
    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.

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

  18. #38
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,710
    Location
    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.

  19. #39
    VBAX Regular
    Joined
    Nov 2013
    Posts
    30
    Location
    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!
    Attached Files Attached Files

  20. #40
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    After 'Next' insert:
            .Text = "\<item id=[!\>]@\>^l"
            .Execute Replace:=wdReplaceAll
            .Text = "^l\</item\>"
            .Execute Replace:=wdReplaceAll
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

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
  •