PDA

View Full Version : add tags to file name *.jpg



dagerr
08-23-2019, 03:28 AM
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


24845

Artik
08-24-2019, 02:30 PM
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 SubThe code seems to work, provided only the file name is in the paragraph.

Artik

dagerr
08-26-2019, 03:16 AM
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.

Artik
08-26-2019, 03:54 AM
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

dagerr
08-26-2019, 04:10 AM
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...

Artik
08-26-2019, 04:26 AM
I removed Matchwildcars:=TrueDO 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. :)

dagerr
08-26-2019, 06:14 AM
Yes, but finally i'm not use searching with wildcards, in this case: " .JPG" so it is not nescessery

Artik
08-26-2019, 06:46 AM
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