PDA

View Full Version : [SOLVED:] Nightmare with formatting



Davecogz84
09-07-2018, 08:45 AM
Hello all,

I'm having a nightmare with coding Word formatting. Here is the story:

- Our Word documents are uploaded to a custom-built software that at the moment reads the underlying "Asian text font", rather than the Font or Complex Font of all text. Once this software spits it out, the formatting is all over the place since it is based on the Asian text font, rather than the real font.
- N.B. half of the documents we're getting are coming from computers that seem to be from Chinese and Korean computers (even though the text is almost always in English or Russian).
- Since it is not possible to set Asian Text Font to Times New Roman, nor to remove the Asian text font altogether, the plan is to Clear all formatting, which seems to wipe the underlying Asian text font.

- The issue is, however, that we need to keep the bold in the original document. The solution we came up with is to search through the document for any instances of bold, strip the formatting, and then reintroduce the bold.
- The code below goes through removing the bold, but I want it also to clear the clearformatting sufficiently well to completely wipe the underlying "Asian text font". Here is what I have. This code only does it for one instance of bold at a time. How do I get it to loop through and do it for all instances? Any help is so much appreciated!!

This was my original attempt

Selection.ClearFormatting
With Selection.Find
.ClearFormatting
.Font.Bold = True
.Replacement.ClearFormatting
.Replacement.Font.Bold = False
.Forward = True
.Wrap = wdFindContinue
.Format = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

And then I tried this
Dim iCount As Integer
Selection.HomeKey Unit:=wdStory


With Selection.Find
.ClearFormatting
.Font.Bold = True
.Execute
End With


Do While Selection.Find.found = True And iCount < 1000
iCount = iCount + 1


If Selection.Find.found Then

Selection.ClearFormatting
Selection.Font.Bold = True


End If
Loop


End Sub

Kilroy
09-07-2018, 12:50 PM
This code will change the language ID and clear formatting:


Sub ChangeLanguageIdAndClearFormatting()
'
' Kilroy


Selection.WholeStory
With Selection
.LanguageID = wdEnglishUK
.NoProofing = True
Application.CheckLanguage = True
End With
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "^?"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False 'True
Selection.Find.Execute Replace:=wdReplaceAll
End With
End Sub

Davecogz84
09-07-2018, 06:16 PM
Hi Kilroy,

Thanks for the quick reply!

Unfortunately the Find and Replace function doesn't clear the Asian text font. The only way I've found to do that is to run a Selection.ClearFormatting command. But to run such a command, I need to be looping through the find and replace. I hope that makes sense!

gmaxey
09-08-2018, 04:53 AM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 9/8/2018
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Bold = True
While .Execute
oRng.Select
Selection.ClearFormatting
oRng.Font.Bold = True
oRng.Collapse wdCollapseEnd
Wend
End With
lbl_Exit:
Exit Sub
End Sub