![]() |
#1
|
|||
|
|||
![]()
I am trying to learn VBA - after previous attempts that defeated me. (I was OK with WordPerfect. Not WordPerfect VBA)
As a starting attempt, I have documents with superscripts in the form "(X)" where "X" may be up to 3 characters depending on the number of total footnotes. I want a macro that will remove all those superscript references. I assume that this would have already been done, if anybody can point me to appropriate code. If I can get some code that might be a good way of starting to learn VBA. Co-Pilot wrote some code for me but it didn't work. |
#2
|
||||
|
||||
![]()
The footnote references are linked to the footnotes so if you want to remove one, you also remove the other.
Code:
Sub KillAllFootnotes() Do While ActiveDocument.Footnotes.Count > 0 ActiveDocument.Footnotes(1).Delete Loop End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#3
|
||||
|
||||
![]()
You don't even need a macro for that. A basic wildcard Find/Replace will do:
Find = \(*\) Replace = nothing where the Find format is specified as superscript.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#4
|
|||
|
|||
![]()
Thanks Paul. I couldn't make that work as "Replace = nothing
where the Find format is specified as superscript" is beyond my abilities. In any event, my aim is to learn to write VBA macros. |
#5
|
|||
|
|||
![]()
Thanks Paul (in Canberra). I selected "Find what: \(*\)". "Options: Use Wildcards". "Format: Superscript" but the result was "All done. We made 0 replacements"
In any event, my aim is to learn to write VBA macros so this task is to get me started. I'd be interested to know why the replace did not work, but I'm more interested in trying to learn VBA. gcp in Sydney |
#6
|
|||
|
|||
![]()
This is the code that Co-Pilot wrote for me, that doesn't work. It has elements of Paul's replace that, as a novice, I can follow. As I sat, I am trying to get my head around VBA and I hope that making this work will give me some good tuition.
Sub DeleteSuperscriptText() Dim rng As Range Dim startPos As Long Dim endPos As Long Set rng = ActiveDocument.Content With rng.Find .ClearFormatting .Font.Superscript = True .Text = "\(*\)" .Forward = True .Wrap = wdFindStop .Format = True .MatchWildcards = True Do While .Execute If rng.Font.Superscript = True Then startPos = rng.Start endPos = rng.End ' Adjust the range to select the characters between "(" and ")" rng.SetRange startPos + 1, endPos - 1 ' Delete the characters rng.Delete ' Move the cursor to the next non-superscript position Set rng = ActiveDocument.Range(rng.End, ActiveDocument.Content.End) If rng.Find.Execute(FindText:="", Forward:=True, Wrap:=wdFindStop) Then If rng.Font.Superscript = False Then rng.Collapse wdCollapseStart Exit Sub End If End If End If Loop End With End Sub |
#7
|
||||
|
||||
![]() Quote:
As a macro, the Find/Replace becomes: Code:
Sub DemoA() Application.ScreenUpdating = False With ActiveDocument.Range.Find .ClearFormatting .Replacement.ClearFormatting .Font.Superscript = True .Text = "\(*\)" .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = False .MatchWildcards = True .Execute Replace:=wdReplaceAll End With Application.ScreenUpdating = True End Sub Code:
Sub DemoB() Application.ScreenUpdating = False With ActiveDocument.Range.Find .ClearFormatting .Replacement.ClearFormatting .Font.Superscript = True .Execute FindText:="\(*\)", ReplaceWith:="", MatchWildcards:=True, Format:=False, Forward:=True, Wrap:=wdFindContinue, Replace:=wdReplaceAll End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
![]()
Thanks Paul. DemoB() works. Some of the footnotes had square brackets, so I added that to the macro. Thanks for your help. Little by little I'm getting there.
|
#9
|
||||
|
||||
![]()
To cater for both, you could use a single Find expression with:
"[\(\[]*[\)\]]"
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
![]()
Sorry to bother you. I lost my macros and couldn't find them. Normal.dotm was in templates but it apparently didn't have any macros in it. I have now re-entered your DemoB() but it won't run as it did before. I have made sure that it was copied exactly from your code below. Any hints?
|
#11
|
||||
|
||||
![]()
It's possible the problem isn't with your Normal.dotm but that you've managed to change your system's VBA security settings so as to disable all macros. See under File|Options|Trust Center>Trust Center Settings. The 'Disable all macros with notification' option should be checked.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#12
|
|||
|
|||
![]()
Thanks Paul. I had checked that and just checked it again. The macros are enabled as is "Trust access to the VBA project object model". I didn't think it was the security settings as my other macro works fine on the same document.
|
#13
|
|||
|
|||
![]()
Unless you really know what you are doing and are using VBA code to manipulate other VBA code, "Trust access to the VBA project object model" should be disabled.
|
#14
|
|||
|
|||
![]()
The footnotes were hyperlinked, so I added
Selection.WholeStory Selection.Fields.Unlink And it now works. Thanks for the help |
#15
|
||||
|
||||
![]()
Instead of
Code:
Selection.WholeStory Selection.Fields.Unlink Code:
ActiveDocument.Fields.Unlink
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
LLLCa947 | Word VBA | 6 | 11-21-2024 01:44 AM |
![]() |
ra_beale | Word VBA | 4 | 09-05-2022 07:09 AM |
![]() |
Swarup | Word | 2 | 07-09-2022 07:42 PM |
![]() |
TheBigBoss | Word VBA | 5 | 06-10-2022 06:14 AM |
![]() |
Swarup | Word | 4 | 07-18-2019 05:51 PM |