Select Text to End of Footnote

Joined
Aug 12, 2022
Messages
6
Reaction score
0
What I am trying to do: I tried to write a macro that pastes a string then selects that string and applies a style to it.

So Record Macro
select desired style
paste
type comma and space
select Normal style

No. The desired style does not apply to the pasted text. F8 to extend before applying style does not work.

So I next wrote the macro to:
type \
paste text
Find \
Click on found item (there is only one match)
Close Find tab
delete \
F8
Find Paragraph ^p
apply style
Type , blank
Apply Normal

The VBA is:
Sub newspaperTitle()
'
' newspaperTitle Macro
'
'
Selection.TypeText Text:="\"
Selection.PasteAndFormat (wdFormatOriginalFormatting)
With Selection.Find
.Text = ""
CommandBars("Navigation").Visible = False
Selection.Delete Unit:=wdCharacter, Count:=1
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
End Sub

What am I doing wrong? I see no ApplyStyle in the VBA.
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
578
Reaction score
50
Why would you want to apply the Normal Style to footnotes? Word uses the for that.

Assuming your selection is actually within a footnote when you do this, it seems you need something like:
Code:
Sub NewsPaperTitle()
Application.ScreenUpdating = False
With Selection
  .Text = "\"
  .PasteAndFormat (wdFormatOriginalFormatting)
  With .Footnotes(1).Range
    .Style = wdStyleFootnoteText
    Do While .Characters.Last.Previous Like "[ " & vbCr & "]"
      .Characters.Last.Previous.Delete
    Loop
  End With
End With
Application.ScreenUpdating = True
End Sub
If you're wedded to using Word's Normal Style, you could replace wdStyleFootnoteText with wdStyleNormal
 
Last edited:
Joined
Aug 12, 2022
Messages
6
Reaction score
0
So far, thanks and good, but the style I want applied is journalTitle ; FootnoteText should apply to the comma and space that follows. The \ was part of my hopeless attempt to select a range of multiple words.

Sub NewsPaperTitle()
Application.ScreenUpdating = False
With Selection
.PasteAndFormat (wdFormatOriginalFormatting)
With .Footnotes(1).Range
.Style = wdStylejournalTitle
Do While .Characters.Last.Previous Like "[ " & vbCr & "]"
.Characters.Last.Previous.Delete
Loop
End With
End With
Application.ScreenUpdating = True
End Sub

When I step through, .Style = wdStylejournalTitle complains "One of the values passed to this method or property is out of range." Do I have to use a built=-in style?
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
578
Reaction score
50
The problem with your wdStylejournalTitle code is that you're treating a custom Style as if it was a Word constant. You need to use:
.Style = "journalTitle"
 
Joined
Aug 12, 2022
Messages
6
Reaction score
0
One more question:

With .Footnotes(1).Range
.Style = "journalTitle"
Do While .Characters.Last.Previous Like "[ " & vbCr & "]"
.Characters.Last.Previous.Delete

applies the style to the entire footnote. How do I apply it to just the text I pasted?
 
Joined
Aug 12, 2022
Messages
6
Reaction score
0
And
Do While .Characters.Last.Previous Like "[ " & vbCr & "]"

does what? Is it going backward until it hits brackets?
 
Joined
Aug 12, 2022
Messages
6
Reaction score
0
I think that I am close. It seems as though pasting text in Footnote style requires

Selection.Style = ActiveDocument.Styles("Footnote")
Selection.PasteAndFormat (wdFormatFootnote)
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
578
Reaction score
50
One more question:

With .Footnotes(1).Range
.Style = "journalTitle"
Do While .Characters.Last.Previous Like "[ " & vbCr & "]"
.Characters.Last.Previous.Delete

applies the style to the entire footnote. How do I apply it to just the text I pasted?
Try:
Code:
Sub NewsPaperTitle()
Application.ScreenUpdating = False
With Selection
  .PasteAndFormat (wdFormatOriginalFormatting)
  .Range.End = .Footnotes(1).Range.End
  .Style = "journalTitle"
  Do While .Characters.Last.Previous Like "[ " & vbCr & "]"
    .Characters.Last.Previous.Delete
  Loop
End With
Application.ScreenUpdating = True
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top