Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-23-2023, 03:38 AM
RobiNew RobiNew is offline VBA Quotes Before Punctuation Windows 10 VBA Quotes Before Punctuation Office 2016
Competent Performer
VBA Quotes Before Punctuation
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Question VBA Quotes Before Punctuation

I am trying to move quotes before punctuation with a Word macro, but apart from an error in the code here below, I cannot find a way to swap characters without using 'Selection.' Can someone help? Thanks! Here's the code:


Quote:
'VBA Quotes Before Punctuation
Application.ScreenUpdating = False
Dim myRng As Word.Range, myChr


For Each myRng In ActiveDocument.StoryRanges 'Text and Footnotes
Do
For Each myChr In myRng.Characters
If myChr.Text Like "[,;.:?]" _
And myChr.First.Next.Text Like "[«‹›»" & Chr(39) & Chr(34) & Chr(146) & Chr(148) & "]" Then
'Revert without using "Selection."
End If
Next
Set myRange = myRange.NextStoryRange
Loop Until myRange Is Nothing
Next

Last edited by RobiNew; 09-23-2023 at 07:24 AM. Reason: typo
Reply With Quote
  #2  
Old 09-23-2023, 07:25 AM
vivka vivka is offline VBA Quotes Before Punctuation Windows 7 64bit VBA Quotes Before Punctuation Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

Hi, if I correctly understand your objective, try this (move quotes to be just before the punctuations):
For Each myChr In myRng.Characters
If myChr Like "[,;.:?]" _
And myChr.Next Like "[«‹›»" & Chr(39) & Chr(34) & Chr(146) & Chr(148) & "]" Then
myChr.InsertBefore myChr.Next
myChr.Next.Delete
End If


Note that the code is quite slow because it checks each char in the range.
Reply With Quote
  #3  
Old 09-23-2023, 07:57 AM
RobiNew RobiNew is offline VBA Quotes Before Punctuation Windows 10 VBA Quotes Before Punctuation Office 2016
Competent Performer
VBA Quotes Before Punctuation
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Smile

Thanks a lot for your help! Now the macro does its job correctly, but before closing it shows a message of the type "Object variable or variable of the With block not defined" and highlights the following lines of the code:


Quote:
If myChr Like "[,;.:]" _
And myChr.Next Like "[«‹›»" & Chr(39) & Chr(34) & Chr(146) & Chr(148) & "]" Then
Reply With Quote
  #4  
Old 09-23-2023, 09:19 AM
vivka vivka is offline VBA Quotes Before Punctuation Windows 7 64bit VBA Quotes Before Punctuation Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

I didn't check this on footnotes, but it's much faster:

For Each myRng In ActiveDocument.StoryRanges
Do
With myRng
Do
With .Find
.MatchWildcards = True
.text = "[,;.:?]" & "[«‹›»" & Chr(39) & Chr(34) & Chr(146) & Chr(148) & "]"
.Replacement.text = ""
.Wrap = wdFindStop
.Execute
End With
If .Find.found Then
myRng.InsertBefore myRng.Characters.Last
myRng.Characters.Last.Delete
Else: Exit Do
End If
myRng.Collapse 0
Loop
End With
Set myRng = myRng.NextStoryRange
Loop Until myRng Is Nothing
Next myRng
Reply With Quote
  #5  
Old 09-23-2023, 09:35 AM
RobiNew RobiNew is offline VBA Quotes Before Punctuation Windows 10 VBA Quotes Before Punctuation Office 2016
Competent Performer
VBA Quotes Before Punctuation
 
Join Date: Sep 2023
Posts: 183
RobiNew is on a distinguished road
Smile

It is indeed much faster. And it works also in the footnotes. I think it's perfect. Many, many thanks!
Reply With Quote
  #6  
Old 09-23-2023, 09:48 AM
vivka vivka is offline VBA Quotes Before Punctuation Windows 7 64bit VBA Quotes Before Punctuation Office 2016
Competent Performer
 
Join Date: Jul 2023
Posts: 227
vivka is on a distinguished road
Default

I am glad I managed to help you!
Reply With Quote
Reply

Tags
characters, quotes, swap



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem when changing single-quotes to double-quotes Lup Word 14 02-14-2020 02:46 AM
Curly Smart Quotes Switch to Straight Smart Quotes in New Chapters TheWriterBloke Word 3 03-29-2019 03:11 AM
VBA Quotes Before Punctuation How to replace straight quotes with smart quotes in existing document PABwriter Word 4 05-27-2016 03:36 PM
Changing dumb quotes to smart quotes Reisende Word 2 05-02-2016 08:56 PM
VBA Quotes Before Punctuation Changing single-quotes to double-quotes Bobosmite Word 5 04-15-2013 06:40 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:07 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