![]() |
|
#1
|
|||
|
|||
![]()
I cannot for the life of me figure out how to do this.
I need to be able to figure out what the last character of each line in a document is. If the last character is a colon ( ![]() I have no idea how to even access a single line as some sort of object. Any help would be much appreciated. |
#2
|
||||
|
||||
![]()
Word does not work with lines, as such - it works with paragraphs (which may also contain manual line breaks). Which are you working with?
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
Thank you for responding. Sorry for my delayed reply, my life has been crazy lately.
I'm on Windows 10, working on Microsoft 365's Word. Here's an example of what I'm trying to do: ![]() See how some lines end with ":"? I want to make sure that any time a line ends with a ":" (and a few other symbols) that the ":" and the word preceding it is moved down to the next line. Right now, I'm having to move things down to the next line manually over hundreds of pages, which takes a long time and ends in me missing some. I felt like there has to be some way to Macro that process. I've gotten Macros to work for nearly everything else that I need to do, like changing all quotation marks and apostrophes to curly instead of straight, but this one has me completely stumped. Granted, I'm not a programmer, so my knowledge is pretty limited. My only experience with VBA is a Visual Basic course I took in high school 15 years ago and a whole lot of Googling...but I couldn't find the answer to this one through Google. I did, however, find this forum through Googling, and your posts have been incredibly helpful for figuring out how to do a lot of other things through VBA, so you've already been a huge help! |
#4
|
||||
|
||||
![]()
You don't need a macro for that. All you need is a Find/Replace to change the space after a colon to a non-breaking space. If you do that, only the ones at the line ends will be visibly affected.
Likewise, you don't need macro to change plain quotes to smart quotes or vice-versa.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]() Quote:
...welp, I feel like an idiot now. I didn't think about using a non-breaking space. And I had been wracking my brain about this for weeks! My only follow-up concern is that a non-breaking space will work for colons, but not for em-dashes (—) and en-dashes (–), which wouldn't be followed by a space. Instead, those are followed by words—like this—so I'm not entirely sure how to fix that. My thought was that I'd do a search for the em-dash/en-dash, then move forward a word and replace that word with itself plus a non-breaking space, but I can't quite get the code to work. Dim r As Range Set r = ActiveDocument.Range With r.Find Do While .Execute(findtext:="—", Forward:=True) = True With r .Next(unit:=wdWord, Count:=1).Select With Selection.Find .ClearFormatting .Replacement.ClearFormatting .Text = "—" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False With Selection Dim thatword As String thatword = Selection.Text Selection.Find.Replacement.Text = thatword + "^160" End With End With Selection.Find.Execute Selection.Next(unit:=wdWord, Count:=1).Select End With r.Collapse direction:=wdCollapseEnd Loop End With There's probably a simple error in that code...I just don't know what it is. I know that I don't need to use a macro to change all the quotes because I could just do a find and replace, but since I have to do about a million find and replaces for a ton of different formatting things, I thought it might be easier to just make one big macro to do all of them at once. The only other line-related thing that I'm completely stumped on is how I can detect when there are one-word lines (like in the example, where the word "utrices" is a single word on a line of its own), so that I can take some words from the previous line to move to that line to make sure no words are alone. I know there are ways to detect one-word paragraphs, but I don't know about figuring that out for lines. Is it even possible? |
#6
|
||||
|
||||
![]()
Word doesn't have non-breaking versons of en dashes (–) and em dashes (—), and it doesn't seem that inserting either a normal non-breaking space or a zero-width non-breaking space either side of them has any effect.
The only way around that would be to insert a manual line-break before the word followed by the first in each sequence of en dashes and em dashes and line ends, but that can't be relied on, since: • any subsequent edit to the paragraphs concerned; and • simply attaching a different printer to the the document (whether on the same PC or another one), is liable to mess up the paragraph layout.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
![]()
What I need this for is formatting for an academic journal. It seems super meticulous, but it's part of formatting conventions for academic journals going to publication. There's a rule that states that, because a reader's attention would be distracted from having certain punctuation at the end of a line, you can never leave that punctuation at the end of a line. (I don't make the rules...I'm also not allowed to break them.) Unfortunately, there aren't a lot of resources for optimizing Word for precise journal formatting rules (probably because literally nobody else on the planet other than academic journals does it...or even notices it.)
This process comes at the very end of editing (no further edits occur) and the word document is then exported to PDF before being sent to a professional printer. I feel like there has to be some way around having to do this manually. Right now, I have to manually scroll through hundreds of pages, look at the last character of each line, and place a manual line-break before any word that precedes an em-dash or en-dash. It usually takes me about 6 hours for each article that I have to process. Six very long hours. Just brainstorming, but I wouldn't be able to programmatically just add a soft enter before each word that precedes an em-dash or en-dash because that would impact words that are in the middle of lines, too. Word does have an option to ensure hyphens are non-breaking, so it seems like theoretically, whatever code is running in the background to support that option could also be applied to other characters. Probably. (Or maybe not. Macros are in VBA, but the backend language of Word is probably something like C#, and the code just might not translate over. Or something. I don't know, there's a reason I didn't go into computer science, lol.) My other ideas are that, because I believe Visual Basic (but not VBA) has a line object, maybe there's a way to do that work in a separate VB application. But then that would probably not keep all the other formatting necessary, like Styles applied to the text. What do you think? |
#8
|
||||
|
||||
![]()
There is a way of doing this, but its progress won't exactly be stellar on long documents:
Code:
Sub Demo() Application.ScreenUpdating = False Dim wdPage As Page, wdRct As Rectangle, wdRng As Range, wdLine As Line For Each wdPage In ActiveDocument.ActiveWindow.Panes(1).Pages For Each wdRct In wdPage.Rectangles For Each wdLine In wdRct.Lines Set wdRng = wdLine.Range With wdRng If .Characters.Last.Text Like "[–—]" Then .MoveEndUntil Cset:=" ", Count:=wdBackward .InsertAfter Chr(11) End If End With Next Next Next Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
![]()
If the journal can tell the difference between 0 point and 1 point next to em/en dashes, then this won't work. Otherwise, the following code finds all em and en dashes, then puts 1-point nonbreaking spaces on either side. Seems to work fine on my end.
I wasn't able to figure out how to get the font size from the found em/en dash, so you're prompted to provide it at the beginning of the macro. This is because the macro temporarily shrinks the em/en dashes to 1 point, along with the nonbreaking spaces, then returns just the dashes to their former size. I'm assuming that all instances of dashes will be the same size. Code:
Sub StickyEmAndEnDashes() ' 03/28/2022 Dim oRange As Range Dim strFontSize As String Dim strDashtype() As String Dim strReplaceString As String Dim i As Long ' Prompt user for body text font size: strFontSize = InputBox("Enter the body text font size:") ' Exit sub if user cancels: If strFontSize = vbNullString Then Exit Sub End If Set oRange = ActiveDocument.Range ' Populate an array with em- and en-dash wildcards: strDashtype = Split("^+,^=", ",") For i = LBound(strDashtype) To UBound(strDashtype) ' Concatenate a replace string variable, as it's not possible to ' mix strings and variables for the replacement string: strReplaceString = "^s" & strDashtype(i) & "^s" With oRange.Find .ClearFormatting .Replacement.ClearFormatting .MatchWildcards = True ' Find all em or en dashes: .Text = strDashtype(i) ' Replace with nonbreak-em/en-dash-nonbreak, 1-pt font: .Replacement.Text = strReplaceString .Replacement.Font.Size = 1 .Execute Replace:=wdReplaceAll ' Find all em/en dashes again: .Text = strDashtype(i) .Replacement.Text = strDashtype(i) ' Replace with the normal font size: .Replacement.Font.Size = strFontSize .Execute Replace:=wdReplaceAll End With Next i ' Clean up: With oRange.Find .ClearFormatting .Replacement.ClearFormatting .MatchWildcards = False End With Set oRange = Nothing End Sub |
![]() |
Tags |
find character, line |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Header line only in text box using a different character set | anne4651 | Publisher | 0 | 07-03-2018 03:45 PM |
![]() |
Deferolles | Word | 11 | 04-26-2018 12:18 AM |
![]() |
Tye30 | Word VBA | 8 | 04-20-2017 08:40 PM |
![]() |
glennhardy | Word | 6 | 02-05-2016 04:05 PM |
![]() |
dlowrey | Word Tables | 6 | 03-09-2015 11:29 AM |