Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-29-2022, 01:55 AM
Wrox23 Wrox23 is offline Replace umlauts in fields Windows 11 Replace umlauts in fields Office 2021
Novice
Replace umlauts in fields
 
Join Date: Sep 2022
Posts: 12
Wrox23 is on a distinguished road
Wink 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
Reply With Quote
  #2  
Old 09-29-2022, 05:13 PM
Guessed's Avatar
Guessed Guessed is offline Replace umlauts in fields Windows 10 Replace umlauts in fields Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #3  
Old 09-30-2022, 12:29 AM
Wrox23 Wrox23 is offline Replace umlauts in fields Windows 11 Replace umlauts in fields Office 2021
Novice
Replace umlauts in fields
 
Join Date: Sep 2022
Posts: 12
Wrox23 is on a distinguished road
Default 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 }
Maybe you can help me, that would be great.

Kind regards - Lothar
Reply With Quote
  #4  
Old 09-30-2022, 01:30 AM
Guessed's Avatar
Guessed Guessed is offline Replace umlauts in fields Windows 10 Replace umlauts in fields Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #5  
Old 09-30-2022, 02:25 AM
Wrox23 Wrox23 is offline Replace umlauts in fields Windows 11 Replace umlauts in fields Office 2021
Novice
Replace umlauts in fields
 
Join Date: Sep 2022
Posts: 12
Wrox23 is on a distinguished road
Default 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.
Depending on the text it comes already immediately or after some passes. In some texts the procedure does everything correctly until about the middle and only then exits. The rest of the text then remains as it is. In other texts it exits immediately.

I will try to find a solution.

Many greetings - Lothar
Reply With Quote
  #6  
Old 09-30-2022, 02:33 AM
Guessed's Avatar
Guessed Guessed is offline Replace umlauts in fields Windows 10 Replace umlauts in fields Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #7  
Old 09-30-2022, 02:41 AM
Wrox23 Wrox23 is offline Replace umlauts in fields Windows 11 Replace umlauts in fields Office 2021
Novice
Replace umlauts in fields
 
Join Date: Sep 2022
Posts: 12
Wrox23 is on a distinguished road
Default

Yes, IncludePicture are the only field type and I deleted any filepath in the fields.
Reply With Quote
  #8  
Old 09-30-2022, 02:58 AM
Guessed's Avatar
Guessed Guessed is offline Replace umlauts in fields Windows 10 Replace umlauts in fields Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #9  
Old 10-02-2022, 02:00 AM
Wrox23 Wrox23 is offline Replace umlauts in fields Windows 11 Replace umlauts in fields Office 2021
Novice
Replace umlauts in fields
 
Join Date: Sep 2022
Posts: 12
Wrox23 is on a distinguished road
Default

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
Surely only a small thing.

Best regards - Lothar
Reply With Quote
  #10  
Old 10-02-2022, 03:25 PM
Guessed's Avatar
Guessed Guessed is offline Replace umlauts in fields Windows 10 Replace umlauts in fields Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #11  
Old 10-05-2022, 12:59 AM
Wrox23 Wrox23 is offline Replace umlauts in fields Windows 11 Replace umlauts in fields Office 2021
Novice
Replace umlauts in fields
 
Join Date: Sep 2022
Posts: 12
Wrox23 is on a distinguished road
Default

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
Reply With Quote
  #12  
Old 10-05-2022, 03:48 AM
Guessed's Avatar
Guessed Guessed is offline Replace umlauts in fields Windows 10 Replace umlauts in fields Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,969
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Have you updated the values of the sFind and sRep strings with all your pairs of replacements?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #13  
Old 10-05-2022, 04:20 AM
Wrox23 Wrox23 is offline Replace umlauts in fields Windows 11 Replace umlauts in fields Office 2021
Novice
Replace umlauts in fields
 
Join Date: Sep 2022
Posts: 12
Wrox23 is on a distinguished road
Default

OK, that will have been it. Now everything runs smoothly.

Thank you very much!
Reply With Quote
Reply



Other Forums: Access Forums

All times are GMT -7. The time now is 05:35 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft