Solved

Getting rid of a TOC entry

Posted on 2013-10-29
10
249 Views
Last Modified: 2013-11-20
Hi Experts!

I have a Word macro that copies text using SELECTION.COPY and pastes it where you specify. using SELECTION.PASTE. The problem is it also copies the hidden TOC entry. Is it possible to either edit this selection before pasting OR to not copy the hidden TOC text?

Thanks!

Ed Matsuoka
0
Comment
Question by:Eddie_Aeffect
  • 6
  • 4
10 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39609845
It's not clear what the exact problem is. If the TOC is embedded inside the range that you want to copy, you could copy in two parts.
0
 

Author Comment

by:Eddie_Aeffect
ID: 39609992
Sorry. I wasn't sure the best way to describe this. My macro copies the page break, the title and the first 10-12 rows of a table and pastes them where ever the cursor is when you run the macro. This is because some of our tables bleed over as many as ten pages and we like each page to have its own title. The problem is when you update the TOC, you end up with this:

Table of Contents
Question 1: What books do you own?                       1
Question 1: What books do you own? (continued)    2
Question 1: What books do you own? (continued)    3
Question 1: What books do you own? (continued)    4
Question 1: What books do you own? (continued)    5
Question 2: How many total books?                         6

Instead I want this:

Table of Contents
Question 1: What books do you own?                       1
Question 2: How many total books?                         6

Make sense?

I tried both editing SELECTION.TEXT and copying it to another string variable but when I do I lose the formatting. Is there a way around this?
0
 

Author Comment

by:Eddie_Aeffect
ID: 39657517
Hi Experts!

No one responded to the comment above so maybe this one will work better. I wrote a cumbersome bit of code that seems to work. The only problem is I get this message WORD HAS REACHED THE END OF THE DOCUMENT. DO YOU WANT TO SEARCH FROM THE BEGINNING for every table. So if I have 500 tables I either have to hit it 500 times OR end the macro without running some code at the end. How do I get rid of this message?

For x = 1 To NumberTables

    Selection.Find.ClearFormatting
   
    With Selection.Find
        .Text = "(Continued)"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindAsk
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With

   If Selection.Find.Execute Then
        ' Selection.Find.Execute Replace:=wdReplaceAll !THIS LINE DOESN'T WORK!

        Selection.HomeKey Unit:=wdLine
        Selection.EndKey Unit:=wdLine
        Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
        Selection.MoveUp Unit:=wdLine, Count:=2, Extend:=wdExtend
       
        For z = 1 To Len(Selection.Text) - 3
            If Asc(Mid$(Selection.Text, z, 1)) = 12 Then
                Selection.MoveDown Unit:=wdLine, Count:=z, Extend:=wdExtend
                Exit For
            End If
        Next z
       
        Selection.Copy
       
        TheTextBefore = Selection.Text
        TheTextAfter = GetField(TheTextBefore, 2, "")
        Selection.Delete Unit:=wdCharacter, Count:=1
        Selection.InsertAfter TheTextAfter
        Selection.Font.Size = 12
        Selection.Font.Bold = True
    End If
    Selection.MoveDown Unit:=wdLine, Count:=1

Next x

Thanks!

Ed Matsuoka
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39658214
If your TOC is based on heading paragraphs and you copy these, then you will create new entries in an updated TOC. I don't recommend editing the TOC after it has been created.

Perhaps you should create copies of the heading level style(s) involved, but with the Outline level set to body text. Use the original style for the first occurrence and new style for where you don't want the entries to appear in the TOC
0
 

Author Comment

by:Eddie_Aeffect
ID: 39662884
The problem is the proprietary software we use generates that text. So what my macro does is unhide the TOC information, select it with the rest of the information I want to copy, find the {} brackets that frame it, delete those brackets and anything between them if the title contains (CONTINUED) and then pastes it back in. As I said, inelegant, but it seems to be working except for getting that pesky WORD HAS REACHED THE END OF THE DOCUMENT... which I have told people that they have to, for now, CTRL-BREAK out of, which doesn't allow the rest of the macro (which turns the hidden text off) to run. Any ideas?

Thanks!
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 76

Expert Comment

by:GrahamSkan
ID: 39662910
Can you post a non-confidential example, so we an understand better, please?
0
 

Author Comment

by:Eddie_Aeffect
ID: 39663328
Sure! Here is the macro (and attached function) as well as a before and after Word doc.
congress-before-macro.doc
congress-after-macro.doc
DeleteTOC.bas
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 400 total points
ID: 39663502
Ah, I see that are using Table entry fields. It is more usual to use Heading styles which is why I suggested changing the style instead.

Here is a simple way to delete the table entry fields.
Sub DelTCFields()
    Dim fld As Field
    Dim rng As Range
    
    For Each fld In ActiveDocument.Fields
        Debug.Print fld.Type, fld.Code.Text
        If fld.Type = wdFieldTOCEntry Then
            Set rng = fld.Code
            rng.Expand wdParagraph
            If InStr(rng.Text, "(Continued)") Then
            fld.Delete
            End If
        End If
    Next fld
    ActiveDocument.Fields.Update
End Sub

Open in new window

0
 

Author Comment

by:Eddie_Aeffect
ID: 39663592
I KNEW you guys would know an elegant answer! I don't use Heading styles so I have to admit I don't know how this works but it works great. Thanks.
0
 

Author Closing Comment

by:Eddie_Aeffect
ID: 39663596
Fast, elegant code.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Online collaboration can help businesses be more efficient, help employees grow their skills and foster a team environment.
Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now