#1
|
|||
|
|||
Replace umlauts in fields
Hello all,
I often get large Word documents that contain umlauts and spaces in field names. When importing them later into InDesign, this always results in problems. I have now tried to formulate a regex search to replace the umlauts and spaces only in the IncludePicture fields. It worked for the umlauts, but not for the spaces. Maybe there is a more elegant solution in VBA to do everything in one go. My knowledge of VBA is quite rudimentary. If someone could show me an approach, that would be a great help for me. "äöü " => "aeoeue_" Many greetings - Wrox23 |
#2
|
||||
|
||||
When working with fields in Word you have two different parts to consider working with: either the field code or the field result. You can change either of these via VBA but the contents of the result will be replaced whenever the field is refreshed.
When you import the this into InDesign I expect you only get the field result and it will no longer be a field in InDesign. Are you trying to replace the characters in the code or the result? Does the import to InDesign go smoother if the field is replaced by static text prior to import?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
|||
|
|||
Only the image name ...
Thank you for your inquiry. I am not quite sure if I understand your question correctly. This is only about editing in Word, everything else works. The fields must be edited in the field code. The umlauts and spaces should be replaced, just like you would do it by hand. Only the image name between the quotation marks should be changed in the field. Nothing should change in the field itself. So e.g. as follows:
Code:
{ INCLUDEPICTURE "Menü 0265.jpg" \* MERGEFORMAT \d } becomes { INCLUDEPICTURE "Menu_0265.jpg" \* MERGEFORMAT \d } Kind regards - Lothar |
#4
|
||||
|
||||
This is tricky because you don't want to replace every space in the code, just the ones between the double quotes. This code should get you started but it will need some checks in there to not destroy other fields.
Code:
Sub ReplaceUM() Dim aFld As Field, sCode As String, sFind As String, sRep As String Dim sInner As String, i As Integer, i1 As Integer, i2 As Integer Dim sPre As String, sPost As String sFind = "äöü " sRep = "aou_" For Each aFld In ActiveDocument.Fields sCode = aFld.Code.Text i1 = InStr(sCode, """") sInner = Mid(sCode, i1 + 1) i2 = InStr(sInner, """") sInner = Mid(sInner, 1, i2 - 1) sPre = Left(sCode, i1) sPost = Mid(sCode, i1 + i2) Debug.Print sCode, sInner, sPre, sPost For i = 1 To Len(sFind) sInner = Replace(sInner, Mid(sFind, i, 1), Mid(sRep, i, 1)) Next i aFld.Code.Text = sPre & sInner & sPost Next aFld End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
Invalid procedure call or argument
Hi Andrew,
Thank you very much for the code. Seems to be a good approach. However, on first quick tests, calling the procedure resulted in an error: Code:
Invalid procedure call or argument. I will try to find a solution. Many greetings - Lothar |
#6
|
||||
|
||||
Yes, the code isn't dealing with the majority of field types. The space is the particular issue because there are lots of spaces in fields that shouldn't be replaced by underscores.
Is IncludePicture the only field type that the macro should hit? Do they always have the filepath enclosed in "double quotes"?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#7
|
|||
|
|||
Yes, IncludePicture are the only field type and I deleted any filepath in the fields.
|
#8
|
||||
|
||||
OK, this version is limited to only IncludePicture fields with at least two double quotes in it
Code:
Sub ReplaceUM() Dim aFld As Field, sCode As String, sFind As String, sRep As String Dim sInner As String, i As Integer, i1 As Integer, i2 As Integer Dim sPre As String, sPost As String sFind = "äöü " sRep = "aou_" For Each aFld In ActiveDocument.Fields If aFld.Type = wdFieldIncludePicture Then sCode = aFld.Code.Text If Len(sCode) - Replace(sCode, """", "") > 1 Then i1 = InStr(sCode, """") sInner = Mid(sCode, i1 + 1) i2 = InStr(sInner, """") sInner = Mid(sInner, 1, i2 - 1) sPre = Left(sCode, i1) sPost = Mid(sCode, i1 + i2) Debug.Print sCode, sInner, sPre, sPost For i = 1 To Len(sFind) sInner = Replace(sInner, Mid(sFind, i, 1), Mid(sRep, i, 1)) Next i aFld.Code.Text = sPre & sInner & sPost aFld.Update End If End If Next aFld End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#9
|
|||
|
|||
Hello Andrew,
I'm just getting around to trying out your new variation - thanks so much for that! In this variant I get a type error, after the line: Code:
If Len(sCode) - Replace(sCode, """", "") > 1 Then Best regards - Lothar |
#10
|
||||
|
||||
Yes, I forgot to add a Len() around the second part of that code. Try changing that line to
Code:
Sub ReplaceUM() Dim aFld As Field, sCode As String, sFind As String, sRep As String Dim sInner As String, i As Integer, i1 As Integer, i2 As Integer Dim sPre As String, sPost As String sFind = "äöü " sRep = "aou_" For Each aFld In ActiveDocument.Fields If aFld.Type = wdFieldIncludePicture Then sCode = aFld.Code.Text If Len(sCode) - Len(Replace(sCode, """", "")) > 1 Then i1 = InStr(sCode, """") sInner = Mid(sCode, i1 + 1) i2 = InStr(sInner, """") sInner = Mid(sInner, 1, i2 - 1) sPre = Left(sCode, i1) sPost = Mid(sCode, i1 + i2) Debug.Print sCode, sInner, sPre, sPost For i = 1 To Len(sFind) sInner = Replace(sInner, Mid(sFind, i, 1), Mid(sRep, i, 1)) Next i aFld.Code.Text = sPre & sInner & sPost aFld.Update End If End If Next aFld End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#11
|
|||
|
|||
Hello Andrew,
it's like jinxed. Now the program runs through without errors, but it only replaces the spaces with underscores. The umlauts remain. Thanks again for your effort. Lothar |
#12
|
||||
|
||||
Have you updated the values of the sFind and sRep strings with all your pairs of replacements?
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#13
|
|||
|
|||
OK, that will have been it. Now everything runs smoothly.
Thank you very much! |
|