Consulting

Results 1 to 8 of 8

Thread: add tags to file name *.jpg

  1. #1
    VBAX Regular
    Joined
    Jan 2018
    Posts
    58
    Location

    add tags to file name *.jpg

    Hi, the following macro adds tags to image names, the problem is that if there are two or more instances of image names, the InsertBefore command does not add tags exactly in the right place. There is also a case-sensitive problem that does not work with wildcards. File attached below.
    Sub JPG_xml()
    
    
                    
        Dim fRng As Range
            Set fRng = ActiveDocument.Range
        With fRng.Find
             
            Do While .Execute(findText:="^13<*>.JPG", _
            MatchWholeWord:=True, MatchWildcards:=True, _
            MatchCase:=False)
         
      
                With fRng
                               
                  .Start = fRng.Start + 1
                    .InsertAfter """></Image>" & Chr(13)
                    .InsertBefore "<Picture>" & Chr(13) & "<Image href=""file://images/"
                                      
                   .Collapse 0
                End With
                
               'Exit Do
            Loop
        End With
        
    lblx_Exit:
        Set fRng = Nothing
        End Sub
    code add tags jpg.docx
    Karol

  2. #2
    VBAX Tutor
    Joined
    Dec 2008
    Posts
    272
    Location
    The problem is the search pattern. Copy the pattern from the code and search in Word. You'll see what it finds in the next steps.


    I wrote the code a bit differently.
    Sub JPG_xml_1()
        Dim fRng        As Range
        
        Set fRng = ActiveDocument.Range
        
            Do While fRng.Find.Execute(findText:="_*.JPG", _
                              MatchWholeWord:=True, MatchWildcards:=True, _
                              MatchCase:=False)
    
    
                Set fRng = fRng.Paragraphs.Item(1).Range
                
                fRng.Select 'tylko na czas testów!!!
                
                With fRng
                    .Text = "<Picture>" & Chr(13) & "<Image href=""file://images/" & Left(.Text, Len(.Text) - 1) & """></Image>" & String(2, Chr(13))
                    .Collapse 0
                End With
    
    
            Loop
    
    
    lblx_Exit:
        Set fRng = Nothing
    End Sub
    The code seems to work, provided only the file name is in the paragraph.

    Artik

  3. #3
    VBAX Regular
    Joined
    Jan 2018
    Posts
    58
    Location
    ok, right, so what if in filenames will be e.g.: lorem.jpg, ipsum.jpg... and so on, and either where there is no underscore or the letters JPG are small caps.
    Karol

  4. #4
    VBAX Tutor
    Joined
    Dec 2008
    Posts
    272
    Location
    If only the file name will be in the paragraph AND ".JPG" will not appear in other unordered places, it seems that it is enough if you change the search to the following
            Do While fRng.Find.Execute(findText:=".JPG", _                          
                              MatchWholeWord:=False, MatchWildcards:=False, _
                              MatchCase:=False)
    Artik

  5. #5
    VBAX Regular
    Joined
    Jan 2018
    Posts
    58
    Location
    Of course... I removed Matchwildcars:=True, because it block Matchcase - it can't work at the same time with Matchwildcards... Many thanks for Your help.

    Do While fRng.Find.Execute(findText:=".JPG", _                          
                              MatchWholeWord:=False, MatchCase:=False)
    P.S. Wreszcie ktos z Polski...
    Karol

  6. #6
    VBAX Tutor
    Joined
    Dec 2008
    Posts
    272
    Location
    Quote Originally Posted by dagerr View Post
    I removed Matchwildcars:=True
    DO NOT DO THIS. The search remembers the settings from previous use. If you were looking for something (manually or with a macro) with the "Use wildcards" option enabled before using this macro, then the lack of setting in your code will cause an incorrect search.

    Artik

    PS
    Też się cieszę jak spotkam rodaka.
    Last edited by Artik; 08-26-2019 at 05:02 AM.

  7. #7
    VBAX Regular
    Joined
    Jan 2018
    Posts
    58
    Location
    Yes, but finally i'm not use searching with wildcards, in this case: " .JPG" so it is not nescessery
    Karol

  8. #8
    VBAX Tutor
    Joined
    Dec 2008
    Posts
    272
    Location
    Today - not. And tomorrow you will rummage in the code? Or you'll wonder why something that worked didn't suddenly work.
    Listen to the elders and don't combine (nie kombinuj).

    Artik

Posting Permissions

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