Wildcard "special" searches

  • Thread starter Thread starter Peter T. Daniels
  • Start date Start date
P

Peter T. Daniels

(Word2007)

My authors are European, so they love to put their periods and commas
after their footnote references, but that's not how we Americans do
it. I'm getting tired of manually moving the punctuation around (and
the Transpose macro results in a superscript period before the
footnote reference, which isn't an improvement).

Word tells me that I cannot use both ^f and Wildcards in a
(Find-)Replace, so how can I automate this procedure? (Is it possible
to modify the Transpose macro to not affect the Superscript status of
the characters involved, since it's unlikely in the extreme that a
typo would involve superscripting!)

(Here's the text of the macro that was provided here years ago; it was
modified from the original because it was taking longer and longer to
work as a file got bigger, so a checking operation was removed.)

Sub Transpose()
Dim oRng As Range
Dim sText As String
Dim Msg1 As String
Dim Msg2 As String
Dim Msg3 As String
Dim MsgTitle As String
Msg1 = "You must place the cursor between " & _
"the 2 characters to be transposed!"
Msg2 = "There are no characters to transpose?"
Msg3 = "There is no document open!"
MsgTitle = "Transpose Characters"
On Error GoTo ErrorHandler
Set oRng = Selection.Range
Select Case Len(oRng)
Case Is = 0
If oRng.Start = oRng.Paragraphs(1).Range.Start Then
MsgBox Msg1, vbCritical, MsgTitle
Exit Sub
End If
If oRng.End = oRng.Paragraphs(1).Range.End - 1 Then
MsgBox Msg1, vbCritical, MsgTitle
Exit Sub
End If
With oRng
.Start = .Start - 1
.End = .End + 1
.Select
sText = .Text
End With
Case Is = 1
MsgBox Msg1, vbCritical, MsgTitle
Exit Sub
Case Is = 2
sText = Selection.Range.Text
Case Else
MsgBox Msg1, vbCritical, MsgTitle
Exit Sub
End Select
With Selection
If .Range.Characters(1).Case = 1 _
And .Range.Characters(2).Case = 0 Then
.TypeText UCase(Mid(sText, 2, 1)) & _
LCase(Mid(sText, 1, 1))
Else
.TypeText Mid(sText, 2, 1) & _
Mid(sText, 1, 1)
End If
.MoveLeft wdCharacter
End With
End
ErrorHandler:
If Err.Number = 4248 Then
MsgBox Msg3, vbCritical, MsgTitle
End If
End Sub
 
Thank you, and did you test it? It doesn't appear to refer to the
special nature of a footnote reference, as opposed to simply dealing
with Superscript format.

Can you point to exactly what the modifications are, so that I can
make just those changes, as opposed to replacing the whole thing and
then maybe having to go back to the earlier version? (I see 7 lines, 2
+ 5, set off near the end.)
 
Why would I want to cut and paste the footnote, instead of cutting and
pasting the punctuation mark? (If Track Changes were on, it would
create tremendous havoc in the notes, because it gives a number to
both a deleted and an inserted copy of the same note.)

And it would be better to do them individually rather than all at
once.

The reason for the wild card is so that I can put the cursor between
the note and the following period, comma, etc., and press Ctrl-T (the
shortcut I assigned to the Transpose macro). Is there some other way
to accomplish that?

If it has to be a separate macro, I can give it Ctrl-Shift-T. If that
comes assigned to something, it's not something I use! (Just as I
assigned Ctrl-\ to Accept Change and Ctrl-Shift-\ to Reject Change --
can't imagine why any of these three shortcuts aren't built in.)

(Normally a space would not intervene, so "next character" is fine.)

Thank you for your efforts!

Sorry about that. You are right. The code I gave you only transposes
superscripts. It does not work with footnote references.

The ^f cannot be used with wildcards. But why would you want a wildcard?
If the aim is to locate all footnotes, then ^f does that. So I have used
the ^f in the Word Search and Replace using code and after selecting the
footnote reference mark, it is just a matter of cutting and pasting it
after the next character.

The code below will work if there are no spaces between the footnote
reference mark and the next character ("." or ","). If there may or may
not be spaces, then the code has to be modified. But it can be done.

Also, this code searches the whole document and transposes all the
footnote reference marks it finds. If some footnote references are in
the correct position and some are not, then this code will have to be
modified further.

I have tested it and it works, but with the above conditions. If you
want it to be more foolproof, please let me know. Or you may work it out
yourself.

Sub TransposeFootnote()
'

'Move to top of document

Selection.HomeKey Unit:=wdStory, Extend:=wdMove

'search whole document

Do Until ActiveDocument.Bookmarks("\Sel").Range.End = _
ActiveDocument.Bookmarks("\EndOfDoc").Range.End

'search for footnote
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
Text = "^f"
Replacement.Text = ""
Forward = True
Wrap = wdFindStop
End With

Selection.Find.Execute

'transpose footnote
If Selection.Find.Found = True Then

With Selection
'cuts the footnote and pastes it after the next character.
'This assumes that there are no spaces between footnote reference
mark and the next character
Cut
Collapse
MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdMove
PasteAndFormat wdFormatOriginalFormatting
'delete the extra space produced by cut and paste
MoveLeft Unit:=wdCharacter, Count:=1
MoveStart Unit:=wdCharacter, Count:=-1
Delete
'move cursor beyond the transposed footnote so as to search for
the next
MoveRight Unit:=wdCharacter, Count:=2

End With
Else
Exit Sub
End If

Loop

End Sub
Thank you, and did you test it? It doesn't appear to refer to the
special nature of a footnote reference, as opposed to simply dealing
with Superscript format.
Can you point to exactly what the modifications are, so that I can
make just those changes, as opposed to replacing the whole thing and
then maybe having to go back to the earlier version? (I see 7 lines, 2
+ 5, set off near the end.)
[/i]
[/QUOTE]
[/QUOTE]
 
Great! Thanks! I'll try it shortly.

(I don't suppose you can handle XML? The Bibliography Tool has some
pretty annoying mistakes in it, and using Yves Dhondt's page I've been
able to correct only one of them.)

Okay. Here is the modified code. This macro can be assigned to any
shortcut key combination and used. Place the cursor anywhere before the
first footnote reference you want to transpose and hit the shortcut
keys. It will transpose the footnote reference. Hit the shortcut key
again and it will transpose the next footnote reference in your text.
And so on. It will change only one footnote at a time.

As suggested by you, this macro does not cut and paste the footnote
reference but cuts and pastes the first character after the reference
which is not a space. So it doesn't matter if the character is a "." or
"," or anything else. This macro will work even if there are spaces
between the footnote reference and the next character. If there are
extra spaces, they will moved beyond the footnote reference.

Sub TransposeFootnote2()

'declare variables
Dim intpos1, intpos2 As Integer

'search for footnote
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

With Selection.Find

Text = "^f"
Replacement.Text = ""
Forward = True
Wrap = wdFindStop
MatchWildcards = False

End With

Selection.Find.Execute

If Selection.Find.Found = True Then

'assign the character position of selection to variable
intpos1 = Selection.Characters.Last. _
Information(wdFirstCharacterColumnNumber)

'search for any character except space
Selection.Collapse wdCollapseEnd
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

With Selection.Find

Text = "[!"" ""]"
Replacement.Text = ""
Forward = True
Wrap = wdFindStop
MatchWildcards = True

End With

Selection.Find.Execute

'assign selected character's position to variable
intpos2 = Selection.Characters.Last. _
Information(wdFirstCharacterColumnNumber)

'transpose footnote
With Selection

'cuts the character next to footnote and pastes it before
the footnote reference.
'This will work even if there are spaces between footnote
reference mark and the next character
Cut
Collapse
MoveLeft Unit:=wdCharacter, Count:=intpos2 - intpos1
PasteAndFormat wdFormatOriginalFormatting
'move curson beyond the transposed footnote so as to search
for the next
MoveRight Unit:=wdCharacter, Count:=2

End With
Else
Exit Sub
End If

End Sub

Peter T. Daniels;492901 Wrote:




Why would I want to cut and paste the footnote, instead of cutting and
pasting the punctuation mark? (If Track Changes were on, it would
create tremendous havoc in the notes, because it gives a number to
both a deleted and an inserted copy of the same note.)
And it would be better to do them individually rather than all at
once.
The reason for the wild card is so that I can put the cursor between
the note and the following period, comma, etc., and press Ctrl-T (the
shortcut I assigned to the Transpose macro). Is there some other way
to accomplish that?
If it has to be a separate macro, I can give it Ctrl-Shift-T. If that
comes assigned to something, it's not something I use! (Just as I
assigned Ctrl-\ to Accept Change and Ctrl-Shift-\ to Reject Change --
can't imagine why any of these three shortcuts aren't built in.)
(Normally a space would not intervene, so "next character" is fine.)
Thank you for your efforts!
 
I'm afraid it doesn't work ... I put an icon for it on my QAT and when I clicked it, it immediately opened the macro listing with the line "Cut" highlighted, apparently claiming that it didn't tell it what to Cut.

(I had to use the icon because I assigned Crtl-Shift-T to the macro but when I tried that in my text, it seemed to think I wanted to apply a digital signature, or something like that.)

(Any possibility that, in the fixing, you could make it work not by searching for the next footnote, but simply by placing the cursor between the footnote reference and the following character, the things that need to be transposed?)

Thank you!

[I have just been forced back into New Google Groups again, and it is now insisting on adding a blank line after every quoted line.]

Okay. Here is the modified code. This macro can be assigned to any

shortcut key combination and used. Place the cursor anywhere before the

first footnote reference you want to transpose and hit the shortcut

keys. It will transpose the footnote reference. Hit the shortcut key

again and it will transpose the next footnote reference in your text.

And so on. It will change only one footnote at a time.



As suggested by you, this macro does not cut and paste the footnote

reference but cuts and pastes the first character after the reference

which is not a space. So it doesn't matter if the character is a "." or

"," or anything else. This macro will work even if there are spaces

between the footnote reference and the next character. If there are

extra spaces, they will moved beyond the footnote reference.







Sub TransposeFootnote2()



'declare variables

Dim intpos1, intpos2 As Integer



'search for footnote

Selection.Find.ClearFormatting

Selection.Find.Replacement.ClearFormatting



With Selection.Find



.Text = "^f"

.Replacement.Text = ""

.Forward = True

.Wrap = wdFindStop

.MatchWildcards = False



End With



Selection.Find.Execute



If Selection.Find.Found = True Then



'assign the character position of selection to variable

intpos1 = Selection.Characters.Last. _

Information(wdFirstCharacterColumnNumber)





'search for any character except space

Selection.Collapse wdCollapseEnd

Selection.Find.ClearFormatting

Selection.Find.Replacement.ClearFormatting



With Selection.Find



.Text = "[!"" ""]"

.Replacement.Text = ""

.Forward = True

.Wrap = wdFindStop

.MatchWildcards = True



End With



Selection.Find.Execute



'assign selected character's position to variable

intpos2 = Selection.Characters.Last. _

Information(wdFirstCharacterColumnNumber)



'transpose footnote

With Selection



'cuts the character next to footnote and pastes it before

the footnote reference.

'This will work even if there are spaces between footnote

reference mark and the next character

.Cut

.Collapse

.MoveLeft Unit:=wdCharacter, Count:=intpos2 - intpos1

.PasteAndFormat wdFormatOriginalFormatting

'move curson beyond the transposed footnote so as to search

for the next

.MoveRight Unit:=wdCharacter, Count:=2



End With

Else

Exit Sub

End If



End Sub




Why would I want to cut and paste the footnote, instead of cutting and
pasting the punctuation mark? (If Track Changes were on, it would
create tremendous havoc in the notes, because it gives a number to
both a deleted and an inserted copy of the same note.)

And it would be better to do them individually rather than all at


The reason for the wild card is so that I can put the cursor between
the note and the following period, comma, etc., and press Ctrl-T (the
shortcut I assigned to the Transpose macro). Is there some other way
to accomplish that?

If it has to be a separate macro, I can give it Ctrl-Shift-T. If that
comes assigned to something, it's not something I use! (Just as I
assigned Ctrl-\ to Accept Change and Ctrl-Shift-\ to Reject Change --
can't imagine why any of these three shortcuts aren't built in.)

(Normally a space would not intervene, so "next character" is fine.)

Thank you for your efforts!
 
Where are you placing the cursor ? Please try the macro with the cursor
anywhere on the line where the footnote reference is located but before
the footnote reference. It should work.

I placed the cursor a few characters before the footnote reference, on
the same line.

I just tried it again, and the Visual Basic window opened, with the
line "Cut" highlighted and a little box reading "Compile error: Sub or
Function not defined."
Please let me know if it doesn't. If not, I will code for placing the
cursor between the footnote reference and the next character. If you can
get this macro to work, it should be even more convenient for you.

Indeed.
 
I can't figure it out. Maybe something was left out while copying my
code? Is there a period before the cut statement? Like ".cut" ?

Nope. It's directly below two lines in green.
Could you upload your document so that I could see where the error is
creeping in?

You want me to upload my normal.dotm template somewhere? Where?

I installed the macro simply by copying your text from the newsgroup
to the clipboard, then to a Notepad file; then copied it from the
Notepad file to the template (following Greg Mayor's instructions,
which have worked all the times people have posted useful macros here
before).
 
It works. Thank you. (I compared the two texts -- the main difference
is that you added periods to start a number of lines.)

But not when the cursor is placed between note and following
character, only when it precedes a note.

And Ctrl-Shift-T still brings up a box saying that if I want to add a
digital signature, blah-di-blah-di-blah. (I've never had trouble
assigning a shortcut before! The default assignment for Ctrl-Shift-T
is "Unhang," something I have no use for, not "digital signature"!)

Maybe Ctrl-Alt-T will do it ...
 
And Ctrl-Shift-T still brings up a box saying that if I want to add a
digital signature, blah-di-blah-di-blah. (I've never had trouble
assigning a shortcut before! The default assignment for Ctrl-Shift-T
is "Unhang," something I have no use for, not "digital signature"!)

Maybe Ctrl-Alt-T will do it ...

D'oh! I failed to notice that when you click "Customize" to add a
shortcut, it doesn't open to the item that's selected in the list
above, but to the first item in the first list of commands, viz.,
AddDigitalSignature!

And when I tested the new shortcut, I learned that if the period is
already before the next footnote, it'll grab the first letter of the
next sentence. Not really ideal. Takes two Undo's to set it right.
 
Why do you want to make it constantly _more_ complicated? I asked for
a button to do one thing (a "transpose" function that works when a
footnote reference is involved) and you've given it a multitude of
functions whose utility escapes me.

It didn't occur to me to ask about this earlier (because I hate
endnotes) -- I don't suppose this will work on endnote references as
well? (Not really a problem, because if any author had for some reason
used endnotes instead of footnotes, I'd simply use Convert to fix
them, and then when done Convert them back. A problem might arise,
though, in the rare event that a document has both footnotes and
endnotes.)

Okay. That means your document can have a mixture or correctly placed
footnotes and incorrect ones.

That would be its state when editing was partly finished.
Also, there may be footnotes in the middle
of a sentence, not only at the end.

If there's a comma after it (an individual item in a list is
footnoted, say), then it needs to be changed; if there isn't (a note
on the subject of a sentence that would be inappropriate at the end of
the sentence, for instance), then the question would not even arise.
This code takes care of that. It searches for a footnote and asks if you
want it to be transposed. If you click "Yes" then it will do so, else it
will move on. Once it reaches end of document, it will ask you if you
would like to do the search again from the beginning of document. If you
say "Yes", then it will take you to the beginning and you can start
again; if "No" then it does nothing.

That sounds like at least as much, if not more, work than no macro at
all. The point is to _simplify_ editing, not to add complications.
If you still want to do it by placing the cursor between the footnote
ref. and the next character, that can be done too. But this is more
useful.

I suspect you've never been a copyeditor ...
 
As I said, you've clearly never done copyediting. I don't "hunt" for
footnotes so as to get all the punctuation of them right, and then go
back and do some other task (though I imagine a computer scientist
could suppose that's a practical way of proceeding).

I'm reading the entire document, beginning to end, fixing everything
as I come to it, and that includes footnote placement; that's why it
makes sense to have a Transpose command for this one special case that
works just like the one that fixes ordinary tpyos.

BTW I had a series of items ending with parenthesis-note-comma, and
the macro flipped the comma and note properly, but inserted a space
before the comma (it doesn't do that with ordinary letters). I suppose
that has to do with Word's attempt to understand when to supply spaces
when moving a "word" (something selected by a double-click) vs. a
series of letters (something selected by dragging).

Have you even tried it? Or are you feeling it is more complicated? Since

I read your description.
you have so many different scenarios, footnote before character, after
character, and none,

There's only one relevant scenario: when I put the cursor between two
characters and want them transposed. It's only a quirk of Word that
causes the older macro not to work with note references.
the only way to deal with all of them is to give
you the choice whether to transpose a footnote or not. I don't see how
that is more complicated than hunting each footnote in the text, placing
the cursor exactly between the 2 characters and then running the macro.
So try it out and see. Of course, if you have only one footnote to
transpose in a whole document, then this may be more work. But then you
may as well do it manually. Why request a macro?

The current article I'm doing has 130 notes.
 
Yes, you are right. I never could have imagined what you actually wanted
if you had not explained it. So you need to go over each word of the
text, and correct as you go. Got it. I was laughing at this whole
exchange of messages. A classic example of the programmer not
understanding what the client needs. Your nagging is helping me, because
this is how a client is going to be. So, here goes. Another code! This
is as simple as it can get.

Just copy the code, and assign a keyboard shortcut to it. When you want
to transpose a footnote, place the cursor anywhere on the line where the
footnote reference is, but BEFORE the footnote reference (NOT between
footnote reference and next character). Press the keyboard shortcut.
Presto! Your desire will be fulfilled!

Um, no. The one you gave me already does that (plus some other stuff
that I can simply ignore). I find that as I used it yesterday, I would
usually do the transpose with the cursor directly before the note
reference. (I haven't mentioned that I'm translating from German as I
go, because that's not relevant to the problem, but it does mean that
the cursor often happens to come out in that spot.)

The _only_ thing it should do is transpose the two characters the
cursor is between (just like the one I already have, except it should
work on note refs as well as simple characters). It might be ok for it
to work on "fields" generally, but ones other than footnotes (or
endnotes) would rarely if ever arise, because authors don't insert
their own figure or cross reference markers. Thus ideally it would do
nothing but add note-reference capability to what I already had, so
that I wouldn't have to have separate commands (to remember) for the
two kinds of Transpose that exist only because of Word's quirk (and
because after all these years Word has never simply included a
Transpose command!).
 
It works, and I thank you for providing exactly what I asked for
originally! Thank you very much.

I was nonplussed by the line "option explicit" at the top, and also by
the fact that it appears to be two separate macros, but somehow the
single keyboard shortcut works with both of them.
 
Phew! I feel happy that it suits your requirement.

The "Option Explicit" statement is part of VBA options. It simply means
that the code has to declare all variables. It prevents error in
programming.

Does that mean I'm supposed to include it somewhere within the text?
Yes, when I at last realized what you were really looking for, it was

What was unclear about the original formulation?
 
Got it. Thanks!

No, you don't need to include the Option Explicit statement to make the
code work. If you have copied the code without that statement, then
fine.

What I did not understand was how a copy editor worked. I was trying to
create a macro that took care of all the footnote references in one go -
to use the full power of VBA programming - not realizing that that was
not what you wanted to do.
 
(I don't suppose you can handle XML? The Bibliography Tool has some
pretty annoying mistakes in it, and using Yves Dhondt's page I've been
able to correct only one of them.)

This past week, I was able finally to understand Yves Dhondt's system
-- the problem was that it does NOT work with any of Word's built-in
styles, since they reference things in the code for Word itself, but
only with styles that Yves himself made available on his website
(www.codeplex.com/bibliography).

I was able to adapt one of his styles to my needs.
 
Back
Top