Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Breaking down Word Docs through Bookmarks or HypLinks 1

Status
Not open for further replies.

June1975

Programmer
Jul 22, 2003
35
GB
I have a word Document and have Cross references that jump to different sections of the Word Document.

I would like to seperate the Word Doc into individual sections (ie by each Bookmark or Hyperlink or Section)

For example I may have 13 sections in a Word doc which are all book marked or Hyperlinked and I need them saving in a Word Document individually by VBA automation.

The sections are ordered 1.1, 1.2, 1.3......I don't need to identify whcih section Im on - just need them saving in individual Word Docs.....

Can anyone help here - thanks....

------------Typical Sections for a country---------
1 THE MARKET PLACE
1.1 Total Market Sales
1.2 Leading Products
1.3 Leading New Products
1.4 Leading Markets
1.5 Cough, Cold & Respiratory Remedies
Cold Remedies
Cold Remedies - Preferred Forms
Respiratory & General Antiallergics
Cough Remedies
Cough Remedies - Preferred Forms
Sore Throat Remedies
Sore Throat Remedies - Preferred Forms
1.6 Pain Relief
General Pain Relief
General Pain Relief - Preferred Forms
Muscular Pain Relief
1.7 Vitamins, Minerals & Nutritional Supplements
Mineral Supplements
Multivitamins With Minerals
1.8 Digestive/Other Intestinal Remedies
Digestive Tract & Stomach Remedies
Digestive Tract & Stomach Remedies - Preferred Forms
Laxatives
Antidiarrhoeals
1.9 Skin Treatment
Skin Protectors & Emollients
Wound Healing Products
Antifungals
Wound & Skin Disinfection
2 COMPANY ANALYSIS
2.1 Leading Companies
 
1. Please use the TGML code tags. If you do not know how to do this, click the "Process TGML" link below the message area.

2. It depends. You need to give more details. For example, let's assume a document with three bookmarks ("One", "Two", "Three"). Further, the text is bookmarked like this:


Text of Bookmark 1 - bookmarked as "One"

Text of Bookmark 2 - bookmarked as "Two"

Text of Bookmark 3 - bookmarked as "Three"


Code:
Sub SplitFromBookmarks()
Dim oBM As Bookmark
Dim r As Range
Dim ThisDoc As Document
Dim ThatDoc As Document
Dim j As Long

Set ThisDoc = ActiveDocument

For Each oBM In ActiveDocument.Bookmarks
   Set r = oBM.Range
   Set ThatDoc = Documents.Add
   ThatDoc.Range = r
   ThatDoc.SaveAs FileName:="c:\zzz\Bookmark" _
         & j & ".doc"
   ThatDoc.Close
   Set ThatDoc = Nothing
   j = j + 1
Next
End Sub
will indeed create individual documents (and save them). The documents will be:

Bookmark0.doc
Bookmark1.doc
Bookmark2.doc

as J = 0 to start...but whatever.

BUT, and this is a BIG but....the contents will be:

Bookmark0.doc (the first bookmark)
"Text of Bookmark 1"

Fine, this is correct.

Bookmark1.doc (the second bookmark)
"Text of Bookmark 3"

Bookmark2.doc (the third bookmark)
"Text of Bookmark 2"

Why? Because the For Each loop will process all the bookmarks, in the default order, i.e. alphbetically.

One
Three
Two


Now, the Ranges will be correct (the range for each bookmark WILL match), but the naming could be problematic.

"I would like to seperate the Word Doc into individual sections (ie by each Bookmark or Hyperlink or Section)"

Be careful with terminology. "by Hyperlink" is a useless and irrelevant term in this context. A Hyperlink is not a range, it does not cover ANY part of the document. It is a pointer. It points to a location, it is not the location itself. So you can not separate the document "by Hyperlink".

You can certainly do it by Section, but by that I mean what Word considers a Section. That may (or may not) be what YOU consider a Section.

1 THE MARKET PLACE
1.1 Total Market Sales
1.2 Leading Products
1.3 Leading New Products
1.4 Leading Markets
1.5 Cough, Cold & Respiratory Remedies

Unless, and ONLY unless, those demarcations are separated by a SectionBreak...these are not (to Word) separate Sections. To do it by Section is very similar to using bookmarks.
Code:
Sub SplitFromSections()
Dim oSection As Section
Dim r As Range
Dim ThisDoc As Document
Dim ThatDoc As Document
Dim j As Long

Set ThisDoc = ActiveDocument

For Each oSection In ActiveDocument.Sections
   Set r = oSection.Range
   Set ThatDoc = Documents.Add
   ThatDoc.Range = r
   ThatDoc.SaveAs FileName:="c:\zzz\Bookmark" _
         & j & ".doc"
   ThatDoc.Close
   Set ThatDoc = Nothing
   j = j + 1
Next
End Sub
All the Sections are extracted out as separate documents.

faq219-2884

Gerry
My paintings and sculpture
 
Thanks Fumei. This is usefull
The sections code is useful, but I need tomaintain thesection titles in the filenames of the extracted documents. How would i do this?.......Would i need to read the first line of the section into a variable?
 
....Also how do I preserve the formatting - for example bullets and numbering?......
 
1. To preserve format, use the same template for the extracted documents.

2. "Would i need to read the first line of the section into a variable?". Be careful. IF the Section - a real Word Section, not a editorial structure type section - has the text string you want to use as the first paragraph, then it is easy. The answer is...yes

faq219-2884

Gerry
My paintings and sculpture
 
Fumei - Thanks.

How do I do 1). If I use the splitsections code above?......
Do I need to have a Word Template and open that explicitlyrather than use Set ThatDoc = Documents.Add

Di I need to enable any settings for this template?

-----------------
1. To preserve format, use the same template for the extracted documents.
 
You can add a new document from any template.

Syntax: Documents.Add Template:=full path

E.g.
Code:
Set ThatDoc = Documents _
   .Add(Template:="c:\zzz\cool jajaja.dot")
Notice the brackets. They are required when using Set for a Document object.

Adding a document using a template (no brackets)
Code:
Documents.Add Template:="c:\zzz\cool jajaja.dot"

Setting a document object with the template (needs brackets)
Code:
Set ThatDoc = Documents _
   .Add(Template:="c:\zzz\cool jajaja.dot")

faq219-2884

Gerry
My paintings and sculpture
 
I still lose the formatting when using a template.
Bullets and Indenation are lost!!! Is ther a way round this if I use the Hyperlinsk collection? Do I need to detect the start and end range for each section and copy this range and then paste into a new word doc?......
 
Here is an example of the first section....

1 THE MARKET PLACE
1.1 TOTAL MARKET SALES
• In this report sales refer to ex-manufacturer prices, unless stated otherwise.
• In 2007, the UK's total non Rx-bound market was valued at US$3.01 billion following a sales increment of 3% in local currency dollar (LCD) terms.
• Self-medication sales remained flat in LCD terms, leaving a turnover of US$2.24 billion. The proportion of sales attributed to self-medication therefore fell to 74.4%.
• At retail selling prices (RSP), the non Rx-bound market was worth US$5.65 billion, of which self-medication accounted for US$4.11 billion (i.e. 72.7% share). The gross margin in the self-medication sector was therefore 45%.
• Wider product accessibility has led to competitive pricing from supermarkets and other retailers, which has dampened value growth.
• It was reported that, as a result of the lifting of restrictions on pharmacies opening more than 100 hours per week, 250 new pharmacies had been opened by the end of 2007.
• Although P self-medication products remain banned from self-selection within pharmacies, open displays are now permitted to give consumers a better view of the full range of products available.
• Internet pharmacies continue to grow, and these sell both P and GSL products. In early 2008, the Royal Pharmaceutical Society of Great Britain (RPSGB) offered consumers the opportunity to verify legitimate online pharmacies through a new logo on registered pharmacies' homepage. This initiative will further raise consumer confidence in these outlets.
• During 2007, five POM-to-P switches were approved, as were nine P-to-GSL switches. In each case, switches included products from the Skin Treatment and Pain Relief markets (for full details, see Regulatory Environment).
• Although broader product availability and switching should drive growth, the self-medication market remains highly competitive and there has been increased consolidation in the pharmacy sector. During 2007, Phoenix acquired the Nucare chain of pharmacies, while the Co-operative Group purchased 51 pharmacies from the PCT Healthcare Group – thereby solidifying its position as the UK's third largest pharmacy group.
• Pharmacies have reportedly been forced to compete with retailers that flout the Medicines & Healthcare Products Regulatory Agency (MHRA) recommendations. According to reports, Pain Relievers continue to be sold in multiple packs by discounters such as Poundland and The 99p Store: three packs can cost as little as £1 (US$2.05).
TABLE_TOTAL COUNTRY SALES
OTCR - VOL 1_2 - UK - TOTAL SALES
CHART_SELF-MED SHARE EX-MANUFACTURERS
UK_TOTAL_EXMAN
(CHART_SELF-MED SHARE RSP)
UK_TOTAL_RSP
CHART_GROWTH CHARTS (X4)
UK_GROWTH_RX_USD
UK_GROWTH_SM_USD
UK_GROWTH_RX_LCD
UK_GROWTH_SM_LCD
 
Try this. Adjusting for your own template name and path of course.
Code:
Sub SplitFromSections2()
Dim oSection As Section
Dim ThisDoc As Document
Dim ThatDoc As Document
Dim j As Long

Set ThisDoc = ActiveDocument

For Each oSection In ActiveDocument.Sections
   oSection.Range.Copy
   Set ThatDoc = Documents _
      .Add(Template:="c:\zzz\TestCopySection.dot")
   ThatDoc.Range.PasteAndFormat Type:=wdFormatOriginalFormatting
   ThatDoc.SaveAs FileName:="c:\zzz\TestUsingSection" _
         & j & ".doc"
   ThatDoc.Close
   Set ThatDoc = Nothing
   j = j + 1
Next
End Sub
That should work.

faq219-2884

Gerry
My paintings and sculpture
 
Gerry, in your code I don't see why you use ThisDoc ?
 
PHV, correct, it is not really needed. It is just a way to keep (in my mind) the explicitness of the original document. It keeps it clear of the creating, using, saving, closing, destroying the object of the OTHER doc (ThatDoc). However, yes, absolutely, it is not really needed, as the the other doc (ThatDoc) is closed and the object destroyed, it should make the original ActiveDocument be still the ActiveDocument.

Unless you are doing other operations, with other document objects, no, ThisDoc is not needed.

Sometimes, though, I do in fact perform other operations on documents. So, putting ThisDoc in was a classic example of good habits sometimes also being useless ones. <grin>

faq219-2884

Gerry
My paintings and sculpture
 
Fumei

I am re-visiting this again! Any help would be appreciated.

The template I am using contains styles, however when the Word documents are broken into sections the new document section does not relate to the original style and point to another one.

Is there a reason for this? And how would I preserve or make the new document point to the original style?

 
Fumei

Im using a template (without the content )as per the original word doc but Im losing the formatting when the docs are split. (Styles are maintained) Here is the code...
What am I missing?

For Each oSection In ActiveDocument.Sections
strCtyTitle = ActiveDocument.Paragraphs(1)
' we need to strip the CtyTitles by 1 as it contains a ?
ilen = Len(strCtyTitle)
strCtyTitle = Left(strCtyTitle, ilen - 1)
oSection.Range.Copy
Set ThatDoc = Documents _
.Add(Template:="C:\Documents and Settings\APrema\Application Data\Microsoft\Templates\TestCopySection.dot")
ThatDoc.Range.PasteAndFormat Type:=wdFormatOriginalFormatting
strtitle = Trim(ActiveDocument.Paragraphs(1))
ilen = Len(strtitle)
strtitle = Left(strtitle, ilen - 1)
If strtitle = "" Then
strtitle = Trim(ActiveDocument.Paragraphs(2))
ilen = Len(strtitle)
strtitle = Left(strtitle, ilen - 1)
End If
ThatDoc.SaveAs FileName:="c:\OTC\" & "OTCR - " & strCtyTitle & " " & strtitle & ".doc"
ThatDoc.Close
Set ThatDoc = Nothing
j = j + 1
Next
End Sub
 
but Im losing the formatting when the docs are split. (Styles are maintained) "

This is a contradiction.

Styles are the formatting. If the styles are maintained...but the formatting is different, then someone has done some manual formatting in the source document.

There is nothing you can do about it. Except of course making sure there is NO manual formatting.

Again, IF the styles are maintained - and they should be as you are using the same template - then if the format is not the same as the source, then the source has been manually formatted.

faq219-2884

Gerry
My paintings and sculpture
 
Fumei - I have used the styles to format the text in the original Word doc. This is then split using the VBA section breaks as above (I am using a template that contains the styles.) - But the formatting is lost ie the styles applied are lost when you look at the split up documents. Can you advise - thanks?
 
It should work. Again, IF the paragraphs have not be manually formatted, AND they are using the same styles (by using the same template), then the format of the split up docs should be the same.

It works for me.

Tell you what. If you can remove any information you do not want me to see, send me a sample. I would need a starting document AND the .dot file (the template). Both.

I can take a look at it if you like. If you wish to do this, you can send them (ZIP'd together please) to:

gerry dot knight at hrsdc-rhdsc dot gc dot ca

No promises, but I will look at it if you like.


Gerry
 
Fumei - thanks for the offer -
What I have found is that if I select the range and then copy it the styles and formatting are retained in the split doc......as per below....Not sure why it works this way and not as above but so far so good - thanks for your help..

oSection.Range.Select
oSection.Range.Copy
'Set ThatDoc = Documents _
.Add(Template:="C:\Documents and Settings\APrema\Desktop\TEMPLATE.doc")

Documents.Add Template:= _
"C:\TEMPLATE.dot", NewTemplate:= _
False, DocumentType:=0
Selection.PasteAndFormat (wdPasteDefault)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top