![]() |
|
|
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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
|
|
#5
|
|||
|
|||
|
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 |
|
#6
|
|||
|
|||
|
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.. |
|
#7
|
|||
|
|||
|
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 |
|
|
|
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 |