Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2007

Tip: Looking for answers? Try searching our database.

Break external link's in Word

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SpEkTr - 30 Jan 2007 09:35 GMT
I have a word document linked to other word document. I would like to
have a macro which would update and break all that link's.
For update it's not a problem, but when I try to break the link's I
break a TOC too. I use the same macro as for UPDATE, i just replaced
the word update to break:

   Dim rngDoc As Range
   Dim oDoc As Document
   Dim aTOC As TableOfContents
   Set oDoc = ActiveDocument
   For Each rngDoc In oDoc.StoryRanges
       rngDoc.Fields.Unlink
     While Not (rngDoc.NextStoryRange Is Nothing)
       Set rngDoc = rngDoc.NextStoryRange
       rngDoc.Fields.Unlink
    Wend
  Next rngDoc

I already had such macro, but when i moved to office 2007 I lost
everything..Unfortunatelly..

So the main problem is that this function break's the TOC too, whis is
not the thing I wan't. I wan't the TOC to stay, so that i could
navigate through it..

Can somebody give me a tip?

Thanks

Dejan
Stefan Blom - 30 Jan 2007 10:39 GMT
One way would be to check the Type property for each field, and only
unlink the ones that you don't want to keep.

For example, if you want to unlink INCLUDETEXT fields, you could do
as follows:

Replace rngDoc.Fields.UnLink with:

For Each f in rngDoc.Fields
  If f.Type = 36 Then
     f.Unlink    'Unlinks INCLUDETEXT fields only...
  End If
Next f

For a list of all wdFieldType constants, look in the Object Browser in
the Visual Basic Editor. (In Word 97/2000/2002/2003, at least, you can
access the Object Browser by pressing F2.)

Signature

Stefan Blom
Microsoft Word MVP

> I have a word document linked to other word document. I would like to
> have a macro which would update and break all that link's.
[quoted text clipped - 26 lines]
>
> Dejan
SpEkTr - 30 Jan 2007 11:21 GMT
Thanks Stefan,
I've modified this code to something like this:
Public Sub BreakLink()
   Dim rngDoc As Range
   Dim F As Field
   Dim oDoc As Document
   Dim aTOC As TableOfContents
   Set oDoc = ActiveDocument
   For Each rngDoc In oDoc.StoryRanges
       For Each F In rngDoc.Fields
           If F.Type = 36 Then
               F.Unlink    'Unlinks INCLUDETEXT fields only...
           End If
       Next F
     While Not (rngDoc.NextStoryRange Is Nothing)
       Set rngDoc = rngDoc.NextStoryRange
       rngDoc.Fields.Unlink
    Wend
  Next rngDoc

End Sub

Now the problem is that there is no type 36 so, this F.Unlink could
not execute. Is the above code correct, in general?
Stefan Blom - 30 Jan 2007 11:31 GMT
You forgot to change the *second* occurrence of rngDoc.Fields.Unlink,
which is why the code executes.

Signature

Stefan Blom
Microsoft Word MVP

> Thanks Stefan,
> I've modified this code to something like this:
[quoted text clipped - 20 lines]
> Now the problem is that there is no type 36 so, this F.Unlink could
> not execute. Is the above code correct, in general?
SpEkTr - 30 Jan 2007 13:02 GMT
You are correct.
But If I'm going with debugger through code, Unlink statement does not
execute..Maybe should I use different type, but I don't know which to
exclude TOC.

Public Sub BreakLink()
   Dim rngDoc As Range
   Dim F As Field
   Dim oDoc As Document
   Dim aTOC As TableOfContents
   Set oDoc = ActiveDocument
   For Each rngDoc In oDoc.StoryRanges
       For Each F In rngDoc.Fields
           If F.Type = 36 Then
               F.Unlink    'Unlinks INCLUDETEXT fields only...
           End If
       Next F
     While Not (rngDoc.NextStoryRange Is Nothing)
       Set rngDoc = rngDoc.NextStoryRange
       For Each F In rngDoc.Fields
           If F.Type = 36 Then
               F.Unlink    'Unlinks INCLUDETEXT fields only...
           End If
       Next F
    Wend
  Next rngDoc
End Sub
Stefan Blom - 30 Jan 2007 13:43 GMT
Well, my example only unlinks INCLUDETEXT fields. Obviously, if you
have no such fields, nothing will happen. If you are trying to delete
all fields except TOC fields, use:

        For Each F In rngDoc.Fields
            If F.Type <> wdFieldTOC Then
                F.Unlink    'Unlinks anything but the TOC field
            End If
        Next F

Instead of wdFieldTOC you can use its numeric value (13).

Assuming that the Visual Basic Editor is unchanged in Office 2007, you
should be able to see all wdFieldType constants in the Object Browser.

Signature

Stefan Blom
Microsoft Word MVP

> You are correct.
> But If I'm going with debugger through code, Unlink statement does not
[quoted text clipped - 23 lines]
>    Next rngDoc
> End Sub
SpEkTr - 30 Jan 2007 13:48 GMT
Stefan, thank you for being so kind :)

I did as you told, and at the end I add this for updating the TOC:
   For Each aTOC In ActiveDocument.TablesOfContents
       aTOC.Update
   Next aTOC

And now work's ok :)

Now I must find where to change shortcut for macro in 2007, and that's
it :) Thank you :)
Stefan Blom - 30 Jan 2007 13:56 GMT
You are welcome.

Signature

Stefan Blom
Microsoft Word MVP

> Stefan, thank you for being so kind :)
>
[quoted text clipped - 7 lines]
> Now I must find where to change shortcut for macro in 2007, and that's
> it :) Thank you :)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.