![]() |
#1
|
|||
|
|||
![]() I am trying to figure out how to create two different code that modifies text that I have copied. As background, the copied text will always have this format: "This is the text" Citation Now sometimes there will be other quotation marks within the outer quotation marks. For instance: "This is "the" text" Citation There will be different character spacing between the closing quotation mark and the citation. Sometimes, it will be paragraph symbols, other times braking spaces, other times only one space, etc. For the first code, I am trying to do a few things:
For the second code, I am trying do everything that the first code does, but just flip how it all fits together, i.e., Citation ("This is the text"). I have a starting code, but I am having a lot of difficulties figuring out how to do the more specific things in this code. For the second code, I can't even figure out how to get it to paste correctly. Code 1: Code:
Sub Cite1() Application.ScreenUpdating = False Dim Rng As range Set Rng = Selection.range Rng.PasteAndFormat (wdFormatSurroundingFormattingWithEmphasis) With Rng .MoveStartUntil Chr(11), wdForward .End = .Start + 2 .Text = " " End With Selection.MoveStartUntil ")", wdForward Application.ScreenUpdating = True End Sub Code:
Sub Cite2() Application.ScreenUpdating = False Dim RngA As range, RngB As range, StrTmp As String Set RngA = Selection.range With RngA .Paste Do While .Characters.Last Like "[ " & Chr(11) & vbCr & "]" .End = .End - 1 Loop .InsertBefore " (" & Chr(147) Set RngB = .Duplicate With RngB .MoveStartUntil Chr(11), wdForward .End = .Start + 2 .Text = "." & Chr(148) & ") " .Collapse wdCollapseEnd .End = RngA.End End With .Collapse wdCollapseStart .FormattedText = RngB.FormattedText RngB.Text = vbNullString End With Application.ScreenUpdating = True End Sub |
#2
|
||||
|
||||
![]()
Perhaps:
Code:
Sub Demo() Application.ScreenUpdating = False Dim Rng As Word.Range, StrTxt As String, StrRef As String, StrCite As String Set Rng = Selection.Range With Rng StrTxt = Trim(.Text) StrRef = Trim(Split(StrTxt, """")(UBound(Split(StrTxt, """")))) StrCite = Trim(Left(StrTxt, Len(StrTxt) - Len(StrRef))) StrCite = "(" & Chr(147) & Mid(StrCite, 2, Len(StrCite) - 2) & Chr(148) & ")" End With Selection.Text = StrRef & " " & StrCite Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
![]()
Thank you so much for the starting point! So the code is failing at the "StrRef" stage and I can't figure out why.
I made a few modifications so that if I can figure out what is wrong with the "StrRef" stage it will do most of what I want it to do. I am having trouble figuring out the right way to add in an if statement to only add in (quotations marks and alterations omitted) if those characters are actually omitted. Also, did I add in the past and format function correct? Code:
Sub Demo() Application.ScreenUpdating = False Dim Rng As Word.range, StrTxt As String, StrRef As String, StrCite As String Set Rng = Selection.range With Rng StrTxt = Trim(.Text) StrRef = Trim(Split(StrTxt, """")(UBound(Split(StrTxt, """")))) StrRef = Replace(StrRef, "[", "") StrRef = Replace(StrRef, "]", "") StrRef = Replace(StrRef, ". . .", "") StrRef = Replace(StrRef, "...", "") StrCite = Trim(Left(StrTxt, Len(StrTxt) - Len(StrRef))) StrCite = "(" & Chr(147) & Mid(StrCite, 2, Len(StrCite) - 2) & Chr(148) & ")" End With Selection.Text = StrRef & " " & StrCite & "(quoations marks and alterations removed)" PasteAndFormat (wdFormatSurroundingFormattingWithEmphasis) Application.ScreenUpdating = True End Sub |
#4
|
||||
|
||||
![]() Quote:
It would be helpful if you attached an actual document to a post containing representative examples of what you're working with and what you want the end result to look like.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
![]()
I am so sorry, it would have been easier for me to post a word document to show what I am trying to do.
|
#6
|
||||
|
||||
![]()
I don't see any conversion of:
"This is the text" Citation to: Citation ("This is the text") as described in your first post. For what is shown in the attached document: Code:
Sub Demo() Application.ScreenUpdating = False With Selection.Paragraphs.First.Range.Find .ClearFormatting .Replacement.ClearFormatting .Format = False .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Text = "[“”\(\[\)\]]" .Replacement.Text = "" .Execute Replace:=wdReplaceAll .Text = Chr(34) .Replacement.Text = """" .Execute Replace:=wdReplaceAll .Text = ". . ." .Replacement.Text = "…" .Execute Replace:=wdReplaceAll .Text = "^s" .Replacement.Text = " " .Execute Replace:=wdReplaceAll .Text = "[ ]{2,}" .Replacement.Text = " " .Execute Replace:=wdReplaceAll .Text = "[^l^13]{1,}" .Replacement.Text = " " .Execute Replace:=wdReplaceAll End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
![]()
This code is amazing! I cannot thank you enough for the help! I made a few modifications so that it will paste in the information instead of just change the format of selected text. I also attached a modified example to show what I was trying to do for the second code (this is example 3). I had a few questions. First, what am I doing incorrectly in my modification to move the cursor to the end of what I just pasted in? Second, how can I add in an the quotation marks omitted and alterations removed? Third, when I modified the code to paste in items, it changes everything in the paragraph, not just the pasted in items (meaning if the previous already existed sentence has a bracket, it removes that bracket after it pastes in the new stuff). Currently, the modified code 1 is:
Code:
Sub Demo1() Application.ScreenUpdating = False Set Rng = Selection.range Rng.PasteAndFormat (wdFormatSurroundingFormattingWithEmphasis) With Selection.Paragraphs.First.range.Find .ClearFormatting .Replacement.ClearFormatting .Format = False .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Text = "[“”\(\[\)\]]" .Replacement.Text = "" .Execute Replace:=wdReplaceAll .Text = Chr(34) .Replacement.Text = """" .Execute Replace:=wdReplaceAll .Text = ". . ." .Replacement.Text = "…" .Execute Replace:=wdReplaceAll .Text = "^s" .Replacement.Text = " " .Execute Replace:=wdReplaceAll .Text = "[ ]{2,}" .Replacement.Text = " " .Execute Replace:=wdReplaceAll .Text = "[^l^13]{1,}" .Replacement.Text = " " .Execute Replace:=wdReplaceAll End With Selection.Move wdCharacter, Len(Rng) Application.ScreenUpdating = True End Sub |
#8
|
||||
|
||||
![]() Quote:
Code:
Sub Demo() Application.ScreenUpdating = False Dim Rng As Range Set Rng = Selection.Range With Rng .PasteAndFormat (wdFormatSurroundingFormattingWithEmphasis) With .Duplicate .End = .Paragraphs.First.Range.End With .Find .ClearFormatting .Replacement.ClearFormatting .Format = False .Forward = True .Wrap = wdFindStop .MatchWildcards = True .Text = "[“”\(\[\)\]]" .Replacement.Text = "" .Execute Replace:=wdReplaceAll .Text = Chr(34) .Replacement.Text = """" .Execute Replace:=wdReplaceAll .Text = ". . ." .Replacement.Text = "…" .Execute Replace:=wdReplaceAll .Text = "^s" .Replacement.Text = " " .Execute Replace:=wdReplaceAll .Text = "[ ]{2,}" .Replacement.Text = " " .Execute Replace:=wdReplaceAll .Text = "[^l^13]{1,}" .Replacement.Text = "^p" .Execute Replace:=wdReplaceAll End With End With If MsgBox("Swap Citation & Reference?", vbYesNo) = vbYes Then With .Find .Text = "(*)^13(*^13)" .Replacement.Text = "\2(\1)^p" .Wrap = wdFindStop .MatchWildcards = True .Execute Replace:=wdReplaceAll End With End If .Paragraphs.First.Range.Characters.Last = " " .Start = .Paragraphs.First.Range.End .Select End With Application.ScreenUpdating = True End Sub Quote:
Quote:
The code was written on the premise that what is being worked on is being pasted into a new paragraph. Your examples didn't indicate otherwise. I have revised the code to allow pasting into an existing paragraph.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
leeqiang | Excel Programming | 10 | 04-17-2022 03:30 AM |
![]() |
tarikov2006 | Excel Programming | 1 | 11-18-2016 04:10 AM |
Modify right-click context menu to only allow pasting text | derajlance | Word VBA | 0 | 05-24-2016 02:25 PM |
![]() |
Steve_B | PowerPoint | 3 | 01-08-2014 01:06 PM |
Modify vba code to print based on name in the InputBox | OTPM | Project | 0 | 05-25-2011 02:03 AM |