Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-29-2024, 04:11 AM
RRB's Avatar
RRB RRB is offline Looking for Macro Windows 11 Looking for Macro Office 2021
Susan Flamingo
Looking for Macro
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 263
RRB is on a distinguished road
Default Looking for Macro

Hi Friends!



My VBA programming days are long gone (about 25 years ago) so I am turning here for assistance

I need a macro that will scan a long Word doc (including the Footnotes area) and get the text of a certain style, and copy that text to a different page with the pg# that it is on.

I am sure someone has already written a macro to do this.

Thanks to all IA

Susan
Reply With Quote
  #2  
Old 01-29-2024, 02:45 PM
Guessed's Avatar
Guessed Guessed is offline Looking for Macro Windows 10 Looking for Macro Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

A Table of Contents is specifically set up to do this but it won't show anything from the Footnote or Endnote stories.

You might be able to run a macro to inject a {Page} field at the end of the footnotes you want and then extract a copy of that text to do what you want. The presence of the additional page number would likely have an effect on pagination too. The result of that would be two separate chunks of text and it would require some fiddling to get the footnote instances interleaved with the TOC instances from the body text.

If you want a macro to do this, you will need to post a sample document with the styles and footnotes you are interested in.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 01-30-2024, 12:11 AM
RRB's Avatar
RRB RRB is offline Looking for Macro Windows 11 Looking for Macro Office 2021
Susan Flamingo
Looking for Macro
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 263
RRB is on a distinguished road
Default Looking for Macro

Thank you for your attention.

The file is attached as instructed.

The style we want to get the text of is named "MyStyle".

At the end of the doc is how we would like the results displayed.

Thank you again

Susan
Attached Files
File Type: docx FindStyle.docx (16.9 KB, 3 views)
Reply With Quote
  #4  
Old 01-30-2024, 08:39 PM
Guessed's Avatar
Guessed Guessed is offline Looking for Macro Windows 10 Looking for Macro Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

This macro works for the instances of MyStyle in the footnotes.
Code:
Sub aaa()
  Dim aFN As Footnote, aRng As Range, aRngRef As Range, sCode As String
  For Each aFN In ActiveDocument.Footnotes
    Set aRng = aFN.Range
    With aRng.Find
      .ClearFormatting
      .Text = ""
      .Style = "MyStyle"
      If .Execute Then
        Set aRngRef = aFN.Reference
        aRngRef.Collapse wdCollapseEnd
        sCode = "TC """ & aRng.Text & """ \l 1 "
        ActiveDocument.Fields.Add Range:=aRngRef, Text:=sCode
      End If
    End With
  Next aFN
  
  Set aRng = ActiveDocument.Range
  aRng.InsertParagraphAfter
  aRng.Collapse wdCollapseEnd
  sCode = "TOC \f "
  ActiveDocument.Fields.Add Range:=aRng, Text:=sCode
End Sub
However, we can't ALSO use the instances of MyStyle in the body of the document because it appears that only Paragraph Styles can be used to build a TOC with styles.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 01-31-2024, 01:51 AM
RRB's Avatar
RRB RRB is offline Looking for Macro Windows 11 Looking for Macro Office 2021
Susan Flamingo
Looking for Macro
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 263
RRB is on a distinguished road
Default Wroks great

Thank you so much
Susan
Reply With Quote
  #6  
Old 02-01-2024, 11:58 AM
RRB's Avatar
RRB RRB is offline Looking for Macro Windows 11 Looking for Macro Office 2021
Susan Flamingo
Looking for Macro
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 263
RRB is on a distinguished road
Default

Sorry for bothering you, but is there a way to tell this macro to only execute till the end of the current section but no more than that?
TIA
Susan
Reply With Quote
  #7  
Old 02-01-2024, 03:26 PM
Guessed's Avatar
Guessed Guessed is offline Looking for Macro Windows 10 Looking for Macro Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Haven't you moved away from the Footnotes in your other threads?
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #8  
Old 02-01-2024, 05:19 PM
RRB's Avatar
RRB RRB is offline Looking for Macro Windows 11 Looking for Macro Office 2021
Susan Flamingo
Looking for Macro
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 263
RRB is on a distinguished road
Default

Yes you are correct. Thank you for thinking about me more than myself. So I need this exact macro, not in FN area, but in main text area but only to to the end of the current section
Susan
Reply With Quote
  #9  
Old 02-01-2024, 07:22 PM
Guessed's Avatar
Guessed Guessed is offline Looking for Macro Windows 10 Looking for Macro Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Here is a quick and dirty method to add the TC fields. It is somewhat flawed in using Selection objects because I can't immediately see why range won't work and couldn't be bothered figuring it out. You will have problems if there are instances of MyStyle spanning multiple paragraphs.
Code:
Sub aaa()
  Dim aFN As Footnote, aRng As Range, aRngRef As Range, sCode As String, aFld As Field
  Set aRng = Selection.Range.Sections(1).Range
  With aRng.Find
    .ClearFormatting
    .Text = ""
    .Style = "MyStyle"
    Do While .Execute
      sCode = "TC """ & aRng.Text & """ \l 1 "
      aRng.Collapse wdCollapseEnd
      Set aFld = ActiveDocument.Fields.Add(Range:=aRng, Text:=sCode)
      aFld.Select
      Selection.Font.Reset
      aRng.Start = Selection.Range.End
      aRng.End = aRng.Sections(1).Range.End
    Loop
  End With
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #10  
Old 02-01-2024, 08:12 PM
RRB's Avatar
RRB RRB is offline Looking for Macro Windows 11 Looking for Macro Office 2021
Susan Flamingo
Looking for Macro
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 263
RRB is on a distinguished road
Default

Works great!
Forever thankful!
Reply With Quote
  #11  
Old 02-06-2024, 08:17 AM
RRB's Avatar
RRB RRB is offline Looking for Macro Windows 11 Looking for Macro Office 2021
Susan Flamingo
Looking for Macro
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 263
RRB is on a distinguished road
Default

I feel guilty for bothering you so much...
Could we get a macro that does simply two things:
1. format selected text with the style "MyStyle" ''I am gonna need this for later
2. Takes that text and inserts it into a TC field as level 3 ad {tc (text of selection) l 3} formatted with my myStyle and the original text is erased.

Thank you so much

Susan
Reply With Quote
  #12  
Old 02-06-2024, 02:42 PM
Guessed's Avatar
Guessed Guessed is offline Looking for Macro Windows 10 Looking for Macro Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Why do you need step one when the last part of step two is remove that text?

Why does it matter what character style is applied to the TC field? It is hidden text and a TC field so the style won't be visible anyway.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #13  
Old 02-06-2024, 10:23 PM
RRB's Avatar
RRB RRB is offline Looking for Macro Windows 11 Looking for Macro Office 2021
Susan Flamingo
Looking for Macro
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 263
RRB is on a distinguished road
Default

I need to apply this style because, in the final book, I want all these texts that are formatted with this style to become captions in the margin. I have a friend who has programmed an add-in for Word to scan the doc for a given style and to take the texts of those instances and create margin captions exactly where they're positioned.

But I also need them for my TOC for each chapter.

So hence the two steps.

Thank you

Susan
Reply With Quote
  #14  
Old 02-07-2024, 01:34 AM
RRB's Avatar
RRB RRB is offline Looking for Macro Windows 11 Looking for Macro Office 2021
Susan Flamingo
Looking for Macro
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 263
RRB is on a distinguished road
Default

I was actually able to do this myself, with a little assistance of recording a macro:

Dim MyText As String
MyText = Selection.Text
Selection.Style = ActiveDocument.Styles("MyStyle")

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
Text:="TC """ & MyText & """ \l 3 ", PreserveFormatting:=False

Would you believe it actually works?!

I guess I am on my way to becoming the world's biggest expert in Word macros.

I have in the past actually written thousands of lines of code (Pascal, remember that?) but my meds have totally zapped my memory

Thank You again!

Susan Flamingo
Reply With Quote
  #15  
Old 02-08-2024, 02:04 AM
RRB's Avatar
RRB RRB is offline Looking for Macro Windows 11 Looking for Macro Office 2021
Susan Flamingo
Looking for Macro
 
Join Date: May 2014
Location: The Holy City of Jerusalem
Posts: 263
RRB is on a distinguished road
Default

Hi - it is me here again to bother you. Hope you are not getting sick of me...

The document I am doing my scholarly work on is in Hebrew. In Hebrew we use many acronyms that are designated as such with double quotes in them.

So 'Washington District of Columbia' would equal out to 'Washington D"C'

Now these quotes are going to disqualify the whole TC field and it will not be picked up in the TOC field.

So we have to replace it like this:
Washington D/"C
And then it works.

So I need to scan the selected text before it is inserted by the above macro that I wrote and see if there are quotes in the middle of a word they should be replaced with ' /" ' and only then inserted into the TC field.

This is over my small programming head.

Could you help me?

Thanks again

Susan
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perfect macro not working when its code is inserted in larger macro RobiNew Word VBA 3 10-18-2023 03:19 AM
Macro to Remove Paras with Line Spac 6; Macro to Convert Paragraphs to Outline Numbered venganewt Word VBA 0 01-25-2022 06:28 PM
Looking for Macro Footnote extraction macro [Why is this macro so slow? / anyway to make it faster?] Le_Blanc Word VBA 10 03-22-2021 11:38 AM
Spell check macro within macro button field doesn't work in one document samuelle Word VBA 0 07-20-2016 02:27 AM
Looking for Macro Macro Question: Need help making a macro to highlight the first word in every sentence LadyAna Word 1 12-06-2014 10:39 PM

Other Forums: Access Forums

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