PDA

View Full Version : Need help with If Then in my macro!



janaboo13
01-02-2014, 09:56 AM
Happy New Year Everyone!In reading this, you might find that it's similar in a request made last August. However, I've come at it from a different angle and hope that I can solve this issue with your help. I had asked for help with a macro to determine whether is the last line of a paragraph has only 1 character and if so, move left a couple of characters and insert a non-breaking zero-width character.

Macropod helped me understand that this is a really difficult approach. I've taken a different different approach, but I need help with If Then logic in my macro! I work with Chinese and Japanese translated docs that require more than one character (not including punctuation) to appear in the last line of any given paragraph.

I first wrote a macro that loops through all paragraphs, finds the paragraph mark and then moves left 3 characters to insert the non-breaking character. That worked great except for paragraphs in tables, of course, since there are no paragraph markers and you can't search for end of table cell markers.

I've finally come up with a macro that searches for a styles and when found, collapse the selection to the end, move left 3 characters and insert a zero width, non-breaking character. But, I need more help. This works great on most paragraphs. However, if the macro encounters a paragraph with less than 3 characters, it hangs up or goes into a continuous loop in the paragraph before it (which may not be one of the styles that I need it to work on).

So I'm thinking I need code that tests to make sure that the paragraph has less than 3 characters (not including the punctuation marks) and if so, skip it. I'm stumped on how to do this in my code and need some help.In addition to this, I also need to ignore punctuation marks (parenthesis, colons, semi-colons).

Here's code that I've gotten to work for Table Text - 9 pt style. Since it's a style that is only used in our tables, it works:
Option Explicit
Sub InsertZeroWidthTableText - 9 pt()
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="2"
With Selection.Find
.ClearFormatting
.Style = "Table Text - 9 pt"
Do While .Execute(Forward:=True, Format:=True) = True
With Selection
.Collapse Direction:=wdCollapseEnd
.MoveLeft Unit:=wdCharacter, Count:=2
.InsertSymbol CharacterNumber:=8205, Unicode:=True, Bias:=0
End With
Loop
End With
End Sub
The style that I'm having trouble with is "Body Text". It is used in the majority of the content, but we also use it for explanatory text in 2-column tables.

Option Explicit
Sub InsertZeroWidthBodyText()
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="2"
With Selection.Find
.ClearFormatting
.Style = "Table Text - 9 pt"
Do While .Execute(Forward:=True, Format:=True) = True
With Selection
.Collapse Direction:=wdCollapseEnd
.MoveLeft Unit:=wdCharacter, Count:=2
.InsertSymbol CharacterNumber:=8205, Unicode:=True, Bias:=0
End With
Loop
End With
End SubI would really appreciate any help on this and I thank you in advance!

janaboo13
01-02-2014, 11:10 AM
Not sure that I was clear enough about the punctuation issue. I'm including macro code that works for styles that are never used in tables, hence have paragraph marks. What I need is a modification to the macro below to take care of moving the cursor only 2 characters left when there is NO punctuation, but if there is punctuation, move it 3 characters.Here's the code that works with no punctuation:

Option Explicit
Sub InsertZeroWidthSpace()
Dim rng As Range
Dim i As Long
Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:="2"
For i = 1 To 3
Set rng = ActiveDocument.Range
With rng.Find
.ClearFormatting
.Text = "^p"
.Style = Choose(i, "Legend List", "Legend List Bullet Indent", "Legend List Continue")
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
While rng.Find.Execute
rng.Select
Selection.Collapse Direction:=wdCollapseEnd
Selection.MoveLeft Unit:=wdCharacter, Count:=2
Selection.InsertSymbol CharacterNumber:=8205, Unicode:=True, Bias:=0
Wend
End With
Next i
End SubAgain, thanks in advance for your help!