Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-10-2018, 09:56 PM
liliaroma liliaroma is offline Windows 7 64bit Office 2013
Novice
 
Join Date: Nov 2017
Posts: 14
liliaroma is on a distinguished road
Smile How to add or hide automatically 500 “Heading 4”?

Hi there,



I have a question of adding the “Ingredients” part formatted in Heading 4 for many recipes. But first of all, for the clearness, I will describe my job.

My job is cookbook publishing so I have to work with a huge number of cooking recipes. I often hire freelancers to rewrite the recipes. However, for keeping the copyright and preventing freelancers from taking my recipes, I often delete the part of “Ingredients” formatted in Heading 4. With the help of the administrator on this web, I could delete these with one click by the code of Macros program: https://www.msofficeforums.com/word-vba/37364-how-delete-automatically-500-heading-4-all.html. I am very thankful to the administrator for the help.

Now, I have a question that after the freelancers send my rewritten files, Is there any quick method to add the “Ingredient” part again to the rewritten files to publish them? I still often do it manually (copy & paste) and it takes a lot of time. I thought a lot and had 2 following ideas:
  1. Is there any ways in Microsoft Word 2013 that I can add all the deleted “Ingredient” parts by the code of Macros program above?
  2. Another idea is that I will hide the “Ingredient” parts and freelancers cannot see or open these (I am not sure it has this program or not lol) and then I send the freelancers. When they finish it and send it to me, I will make the “Ingredient” appear to publish.
Attached Images
File Type: png Screenshot_1.png (223.9 KB, 22 views)
Attached Files
File Type: zip 20 Reicpes [Sample].zip (1.36 MB, 14 views)
Reply With Quote
  #2  
Old 04-11-2018, 01:17 AM
slaycock slaycock is offline Windows 7 64bit Office 2016
Expert
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

There are a number of ways you can do this.

1. Save the ingredients as a separate word file and then use an includetext field to reinclude the ingredients. Thus your document will show the ingredients but the freelancer will not.

2. Use macros to move the ingredients section to custom xml. Keeps the ingredients in the file in an invisible format (but which could be viewed if someone knows they are stored as xml.
Reply With Quote
  #3  
Old 04-11-2018, 01:52 AM
liliaroma liliaroma is offline Windows 7 64bit Office 2013
Novice
 
Join Date: Nov 2017
Posts: 14
liliaroma is on a distinguished road
Default

Thanks a lot for your support. Could you explain more about your first method? Or you can give me a detailed example for the clearness. Because in the second one, I do not know anything about coding. I am just a content creator

Last edited by macropod; 04-11-2018 at 09:49 PM. Reason: Deleted unecessary quote of entire post replied to
Reply With Quote
  #4  
Old 04-11-2018, 02:44 AM
slaycock slaycock is offline Windows 7 64bit Office 2016
Expert
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

For a start you might gogle 'Word includetext' which would pull up many references including

https://wordmvp.com/FAQs/TblsFldsFms...textfields.htm

I've attached your sample file where I've modified the text to use an include text field for the first three sets of ingredients.

When saving the ingredients as a seperate file make sure you delete the final paragraph markers otherwise this will appear in the imported text.

When you insert the field your usage means that you want to make sure that the 'Preserve formatting during updates' checkbox is unchecked.

The field is inserted using

Insert.Quick Parts.Field....
Scroll down to find Include Text and select it
Add the file path/name
Uncheck 'Preserve foprmatting during updates'
Click OK
Attached Files
File Type: zip 20 Reicpes [Sample] with ingredients.zip (1.39 MB, 13 views)
Reply With Quote
  #5  
Old 04-11-2018, 03:12 AM
liliaroma liliaroma is offline Windows 7 64bit Office 2013
Novice
 
Join Date: Nov 2017
Posts: 14
liliaroma is on a distinguished road
Default

Hi,
I read fully your instructions and thanks for that. But I have a question that is in the attachment. How could you do this? You did this manually or by an automatic method?
Attached Images
File Type: png Screenshot_2.png (66.3 KB, 21 views)

Last edited by macropod; 04-11-2018 at 09:48 PM. Reason: Deleted unecessary quote of entire post replied to
Reply With Quote
  #6  
Old 04-11-2018, 03:23 AM
slaycock slaycock is offline Windows 7 64bit Office 2016
Expert
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

In this instance manually. It would be relatively simple to craft a macro to do this job but I didn't go that route as you indicated that your experience wouldn't lend itself to installing and running a macro.

If you are a commercial operation then maybe someone in your IT support would have the skills to do this for you.
Reply With Quote
  #7  
Old 04-11-2018, 03:45 AM
liliaroma liliaroma is offline Windows 7 64bit Office 2013
Novice
 
Join Date: Nov 2017
Posts: 14
liliaroma is on a distinguished road
Default

Thanks for your great idea. I wish that the administrator @macropod would support me more in order that my work becomes faster and easier with the automatic method.

Last edited by macropod; 04-11-2018 at 09:48 PM. Reason: Deleted unecessary quote of entire post replied to
Reply With Quote
  #8  
Old 04-11-2018, 07:35 AM
slaycock slaycock is offline Windows 7 64bit Office 2016
Expert
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

Fortunately I had a spare half hour after my lunch break.

Code:
Option Explicit

Public list_of_lists_of_ingredients_ranges  As Collection
Public ingredients_directory                As String

Sub covert_lists_of_ingredients_to_includetext_fields()

Dim index                               As Integer
Dim list_of_ingredients                 As Word.Range
Dim ingredients_path                    As String
Dim ingredients_range                   As Word.Range

    'create a directory for the ingredient files
    ingredients_directory = ActiveDocument.Path & "\" & Replace(ActiveDocument.Name, ".docx", vbNullString) & "_ingredients"
    If Len(Dir(ingredients_directory, vbDirectory)) = 0 Then
        MkDir ingredients_directory
    End If
    compile_list_of_lists_of_ingredients_ranges
    
    'create a document for each set of ingredients and insert insterttextfield
    For index = 1 To list_of_lists_of_ingredients_ranges.Count
    
        ingredients_path = create_ingredients_document(index)
        
        Set ingredients_range = list_of_lists_of_ingredients_ranges(index)
        With ingredients_range
            .Delete
            .Fields.Add _
                Range:=ingredients_range, _
                Type:=wdFieldIncludeText, _
                Text:=Chr$(34) & Replace(ingredients_path, "\", "\\") & Chr$(34), _
                preserveformatting:=False
        End With
    Next
    
End Sub

Sub compile_list_of_lists_of_ingredients_ranges()

Dim search_range                        As Word.Range
Dim ingredient_heading_ranges           As Collection
Dim heading_range                       As Variant
    
    Set ingredient_heading_ranges = New Collection
    Set search_range = ActiveDocument.StoryRanges(wdMainTextStory)
    
    With search_range.Find
        Do
            .ClearFormatting
            .Format = True
            .Wrap = wdFindStop
            .Style = "Heading 4"
            .Text = "Ingredients"
            .Execute
            If .Found Then
                ingredient_heading_ranges.Add search_range.Duplicate
            End If
        Loop While .Found
    End With
    
    Set list_of_lists_of_ingredients_ranges = New Collection
    
    ' now get the ranges for the lists of ingedients
    For Each heading_range In ingredient_heading_ranges
        list_of_lists_of_ingredients_ranges.Add get_ingredients_range_from_heading_range(heading_range)
    Next
        
End Sub

Function get_ingredients_range_from_heading_range(this_range As Variant) As Word.Range

    this_range.Move unit:=wdParagraph
    Do Until this_range.Next(unit:=wdParagraph).Style = "Normal"
        this_range.MoveEnd unit:=wdParagraph
    Loop
    Set get_ingredients_range_from_heading_range = this_range
    
End Function

Function create_ingredients_document(this_item As Integer) As String ' name of document
Dim this_doc                            As Word.Document
    
    Set this_doc = Application.Documents.Add(Template:=ActiveDocument.AttachedTemplate.FullName, Visible:=False)
    this_doc.StoryRanges(wdMainTextStory).FormattedText = list_of_lists_of_ingredients_ranges(this_item).FormattedText
    this_doc.StoryRanges(wdMainTextStory).Characters.Last.Delete
    this_doc.SaveAs2 _
        ingredients_directory _
        & "\" _
        & Replace(ActiveDocument.Name, ".docx", "_ingredients_" & Format(this_item, "00") & ".docx")
      
        
    create_ingredients_document = this_doc.FullName
    this_doc.Close savechanges:=False
End Function
Reply With Quote
  #9  
Old 04-11-2018, 10:11 AM
liliaroma liliaroma is offline Windows 7 64bit Office 2013
Novice
 
Join Date: Nov 2017
Posts: 14
liliaroma is on a distinguished road
Default

Thanks a lot for your help. I tried on your macros and the following is the video for the trying. However, it has the error. Please watch the clip and give me the support. I am very thankful about this.

Video: https://youtu.be/vGDmgghzUxk

Image: http://prntscr.com/j3wygh

Last edited by macropod; 04-11-2018 at 09:48 PM. Reason: Deleted unecessary quote of entire post replied to
Reply With Quote
  #10  
Old 04-11-2018, 10:50 AM
slaycock slaycock is offline Windows 7 64bit Office 2016
Expert
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

You installed the macros incorrectly.

Placing what I supplied inside another macro is incorrect.

If you already have other macros in your template then you should put my macros in a seperate module.

In the VBA IDE do the following

Insert.module
Rename the module to iingredients
copy the provided macros to this module.


From your document

click on the Developer tab
click on the macros button in the code group
select the macro 'covert_lists_of_ingredients_to_includetext_fields ' 'Pardon the type, it should say convert
click on the run button

That should do the trick..
Reply With Quote
  #11  
Old 04-11-2018, 03:08 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

This seems to be a very round-about approach to something that could be handled by formatting the 'Heading 4' Style as hidden text, along with whatever other Style is used for the ingredients...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #12  
Old 04-11-2018, 07:25 PM
liliaroma liliaroma is offline Windows 7 64bit Office 2013
Novice
 
Join Date: Nov 2017
Posts: 14
liliaroma is on a distinguished road
Default

Hey, Thanks for your support. I ran your code successfully. However, there is a problem that is when I use IncludeText, I still have to add and delete each Ingredient section manually. Therefore, I have to add and delete 500 Ingredient manually and it takes a lot of time.Do you have any solutions for this?

Last edited by macropod; 04-11-2018 at 09:48 PM. Reason: Deleted unecessary quote of entire post replied to
Reply With Quote
  #13  
Old 04-11-2018, 07:30 PM
liliaroma liliaroma is offline Windows 7 64bit Office 2013
Novice
 
Join Date: Nov 2017
Posts: 14
liliaroma is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
This seems to be a very round-about approach to something that could be handled by formatting the 'Heading 4' Style as hidden text, along with whatever other Style is used for the ingredients...

Thanks for your reply. Can you explain more about the method? Or could you please suggest me a macro to do this problem automatically?
Reply With Quote
  #14  
Old 04-11-2018, 08:10 PM
macropod's Avatar
macropod macropod is offline Windows 7 64bit Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Instead of using the 'Heading 4' and 'List Bullet' Styles throughout, had you used a unique Style name for both the 'Ingredients' headings and the ingredients themselves, all you would have to do is to change those Styles' text format to hidden or not-hidden, as desired. You can do that via Styles>Down Arrow>Style Name>Modify>Format>Font. No macros would be needed, though a quite simple macro could be used to do both:
Code:
Sub ShowHideIngredients()
Application.ScreenUpdating = False
With ActiveDocument
  With .Styles("Ingredients").Font
    .Hidden = Not .Hidden
  End With
  With .Styles("IngredientItems").Font
    .Hidden = Not .Hidden
  End With
End With
Application.ScreenUpdating = True
End Sub
where 'Ingredients' and 'IngredientItems' are the names of the Styles concerned.

As it is, a far simpler macro than you've already been given could be used to toggle the 'Ingredients' display on/off:
Code:
Sub ShowHideIngredients()
Application.ScreenUpdating = False
Dim RngHd As Range
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "Ingredients"
    .Style = wdStyleHeading4
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Do While .Find.Found
    Set RngHd = .Paragraphs(1).Range
    Set RngHd = RngHd.GoTo(What:=wdGoToBookmark, Name:="\HeadingLevel")
    RngHd.Font.Hidden = Not RngHd.Font.Hidden
    .Start = RngHd.End
    .Find.Execute
  Loop
End With
Set RngHd = Nothing
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #15  
Old 04-11-2018, 09:45 PM
liliaroma liliaroma is offline Windows 7 64bit Office 2013
Novice
 
Join Date: Nov 2017
Posts: 14
liliaroma is on a distinguished road
Default

That's great. Thank you very much, admin. Now I can show/hide Ingredients with just one click. I think my problem has been solved. Hoping the forum will grow more and more.

Last edited by macropod; 04-11-2018 at 09:47 PM. Reason: Deleted unecessary quote of entire post replied to
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find instances of heading text in the body of my doc, make cross reference to the actual heading MAHE Word VBA 4 03-03-2018 07:59 AM
How to delete automatically 500 “Heading 4” and all the contents in this from Navigation? liliaroma Word VBA 4 11-18-2017 07:53 PM
How to Hide/Un-hide a worksheet based on cell on another sheet. easton11 Excel Programming 1 06-02-2015 12:07 PM
Creating a table that automatically updates based on entries of a heading in the document cahphoenix Word 3 10-29-2014 01:11 PM
How to have a heading 1 file automatically appear in each header of any page? expert4knowledge Word 2 09-16-2012 10:38 AM

Other Forums: Access Forums

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