#1
|
|||
|
|||
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:
|
#2
|
|||
|
|||
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. |
#3
|
|||
|
|||
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 |
#4
|
|||
|
|||
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 |
#5
|
|||
|
|||
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? Last edited by macropod; 04-11-2018 at 09:48 PM. Reason: Deleted unecessary quote of entire post replied to |
#6
|
|||
|
|||
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. |
#7
|
|||
|
|||
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 |
#8
|
|||
|
|||
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 |
#9
|
|||
|
|||
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 |
#10
|
|||
|
|||
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.. |
#11
|
||||
|
||||
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] |
#12
|
|||
|
|||
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 |
#13
|
|||
|
|||
Quote:
Thanks for your reply. Can you explain more about the method? Or could you please suggest me a macro to do this problem automatically? |
#14
|
||||
|
||||
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 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] |
#15
|
|||
|
|||
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 |
|
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 |