Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-25-2025, 01:22 AM
RRB's Avatar
RRB RRB is offline Margin captions Windows 11 Margin captions Office 2021
Susan Flamingo
Margin captions
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 312
RRB is on a distinguished road
Default Margin captions

Hi, Dear Friends!

I apologize if this has been hashed before. I tried to find it with no success

Word should scan the open doc and wherever it finds a character style named "sty1", it gets the text formatted with that style, cuts it, and pastes it into a text box (frame?) in the margin exactly at the vertical position of the cut text. The text frame should flow so as to keep its connection if any editing is done afterward.

ChatGPT has failed on this

Thank you

Thank you, and have a good day!


Susan Flamingo
Reply With Quote
  #2  
Old 12-25-2025, 02:36 AM
Italophile Italophile is offline Margin captions Windows 11 Margin captions Office 2021
Expert
 
Join Date: Mar 2022
Posts: 578
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

See https://www.msofficeforums.com/word/...-document.html
Reply With Quote
  #3  
Old 12-25-2025, 04:49 AM
RRB's Avatar
RRB RRB is offline Margin captions Windows 11 Margin captions Office 2021
Susan Flamingo
Margin captions
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 312
RRB is on a distinguished road
Default Margin Quotes

Yes, I remember that. Thank you.
But I was looking for a macro that would automate the whole process...
Reply With Quote
  #4  
Old 12-25-2025, 05:37 AM
Italophile Italophile is offline Margin captions Windows 11 Margin captions Office 2021
Expert
 
Join Date: Mar 2022
Posts: 578
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Why use a macro? All you need to do is convert your character style into a paragraph style with a frame, but do a find and replace first to add a paragraph mark after the text formatted with the character style.
Reply With Quote
  #5  
Old 12-25-2025, 07:42 AM
gmaxey gmaxey is offline Margin captions Windows 10 Margin captions Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,652
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Quote:
Originally Posted by RRB View Post
Yes, I remember that. Thank you.
But I was looking for a macro that would automate the whole process...

First you need to create an appropriate paragraph style for the margin text. You can do that with:

Code:
Sub MarginTextStyle()
Dim styleName As String
Dim oStyle As Style
  styleName = "Margin Text"
  'Create/Setup the style
  For Each oStyle In ActiveDocument.Styles
    If oStyle.NameLocal = styleName Then GoTo Setup
  Next oStyle
  ActiveDocument.Styles.Add Name:=styleName, Type:=wdStyleTypeParagraph
Setup:
  With ActiveDocument.Styles(styleName)
    .AutomaticallyUpdate = False
    .BaseStyle = ""
    .NextParagraphStyle = "Normal"
  End With
  With ActiveDocument.Styles(styleName).Font
    .Size = 8
    .ColorIndex = wdAuto
  End With
  With ActiveDocument.Styles(styleName).ParagraphFormat
    .LineSpacingRule = wdLineSpaceSingle
    .SpaceAfter = 0
    .WidowControl = False
    .KeepWithNext = True
    .KeepTogether = True
    .OutlineLevel = wdOutlineLevelBodyText
    .Shading.BackgroundPatternColor = wdColorLightYellow
    With .Borders
      .OutsideLineStyle = wdLineStyleSingle
      .OutsideLineWidth = wdLineWidth050pt
      .OutsideColor = wdColorAutomatic
      .DistanceFromTop = 1
      .DistanceFromLeft = 1
      .DistanceFromBottom = 1
      .DistanceFromRight = 1
      .Shadow = False
    End With
  End With
  With ActiveDocument.Styles(styleName).Frame
    .TextWrap = True
    .WidthRule = wdFrameExact
    'Accomodate 1" margin.   Adjust to suit,
    .Width = 55
    .HeightRule = wdFrameAuto
    .RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
    .RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
    .HorizontalDistanceFromText = InchesToPoints(0.08)
    .VerticalDistanceFromText = InchesToPoints(0)
    .LockAnchor = False
  End With
lbl_Exit:
  Exit Sub
End Sub

Then perhaps you could achieve your goal with:

Code:
Sub ApplyMarginTextToSty1()
Dim oRng As Range
Dim oRngDef As Range
Dim strDef As String
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Style = "Sty1"
    While .Execute
      If oRng.Start = oRng.Paragraphs(1).Range.Start And oRng.End = oRng.Paragraphs(1).Range.End Then
        oRng.Style = "Margin Text"
      Else
        strDef = oRng.Text
        oRng.Delete
        Set oRngDef = oRng.Paragraphs(1).Range
        oRngDef.Start = oRngDef.Paragraphs(1).Range.Start
        oRngDef.InsertBefore vbCr
        oRngDef.Paragraphs.First.Style = "Margin Text"
        oRngDef.Paragraphs.First.Range.Text = strDef
      End If
    Wend
  End With
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #6  
Old 12-25-2025, 08:33 AM
RRB's Avatar
RRB RRB is offline Margin captions Windows 11 Margin captions Office 2021
Susan Flamingo
Margin captions
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 312
RRB is on a distinguished road
Default Margin captions

Of course, doing this via a macro would not give us any advantage over simply formatting the desired text with the correct style, as in the first answer. But I share the knowledge I learn here with other friends, and some of them couldn't even create a style if their dear life depended on it, so I thought I would use a macro and assign a quick button to it, and walla.
Thank you all
And hats off to Greg for again showing there is apparently nothing he cannot do

Thank you, and have a good day!
Susan Flamingo
Reply With Quote
  #7  
Old 12-25-2025, 03:03 PM
Charles Kenyon Charles Kenyon is offline Margin captions Windows 11 Margin captions Office 2021
Moderator
 
Join Date: Mar 2012
Location: Sun Prairie, Wisconsin
Posts: 9,637
Charles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant futureCharles Kenyon has a brilliant future
Default

Create a marginal frame. In that, insert a StyleRef Field to your sty1 style.
Then, select your frame and use it to create an AutoText entry (Alt+F3).
You can attach a keyboard shortcut to it if you want.
AutoComplete will work as well.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Margin captions (again:( RRB Word 3 01-31-2024 03:21 PM
Margin captions VBA to insert captions without appending to existing captions Marrick13 Word VBA 17 03-21-2023 07:51 PM
Table captions changing to Figure captions and vice versa alicatsmom Word Tables 0 06-11-2019 08:51 AM
Margin captions Some captions getting screwed up when fixing other captions oliboi Word 2 08-30-2016 02:55 PM
Margin captions Captions: Changing captions in Appendix update all captions carnestw Word 3 10-27-2015 12:34 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:21 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2026 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft