PDA

View Full Version : Unlink links in Footer & Text Boxes



Asi
03-05-2009, 07:29 AM
Hi

I have a word doc which I open from an Excel macro.
This doc has many links to an excel workbook.
When I open this doc I update all links and then break them all using this code:

wdDoc.Fields.Update
wdDoc.Fields.Unlink


The problem is that now the word doc has changed and it also contains links in it's Footer and in some text boxes. Those links are not updated and unlinked.

I tried modifing the code to:

For i = wdDoc.Fields.Count To 1 Step -1
wdDoc.Fields(i).Unlink
Next i


but it doesn't help. I guess I need to refer to the footer & text boxes links specifically but I can't seem to find the way.

Can you please tell me, how can I unlink those links as well ?

I'll be happy that if somebody adds a text box to this doc (with a link inside), he won't need to inform me. I want to be able to find all links in the doc including those in footer & text boxes and unlink them.

Thanks
Asi

fumei
03-05-2009, 11:54 AM
Take a look at StoryRanges in Help. While most people execute code to loop through ALL the StoryRanges, I fail to see the advantage to this. If all you need is the HeaderFooter story, then just action that. So....
Dim oHF As Word.HeaderFooter
Dim oSection As Word.Section
Dim oField As Word.Field

For Each oSection In wdDoc.Sections
For Each oHF In oSection.Footers
For Each oField In oHf.Range.Fields
oField.UnLink
Next
Next
Next

will unlink all Fields, in all Footers, in all Sections.

Asi
03-09-2009, 04:14 AM
Thanks a lot for your reply..

I managed to Update & Unlink all links located on Footers using your advise.
However, I still can't find a way to do the same for the "Text Box" that I have.
I tried using the following StoryRanges code and still no sucess.

For Each oStoryRange In wdDoc.StoryRanges
For Each oField In oStoryRange.Fields
oField.Update
oField.Unlink
Next
Next



I also thought that by using StoryRanges, I won't need the sections/footers loop that you advised anymore because it will do it for all links in the doc but it looks like this code only unlink the links in the doc itself and not footers/text boxes links.

do you have any explanation & advise how to do it for the "Text Box" links as well.

Thanks again
Asi

fumei
03-10-2009, 08:54 AM
I do not understand.

Are these actual linked textboxes? By that I mean separate textboxes that contain text that is directly linked between them? This is a whole other issue, and I may not be able to help with that, as I can not abide textboxes in the first place. And linking text between separate textboxes is - for me - utter madness.

If you have textboxes (inserted by Insert > Textbox) then those are Shapes, and you will have to work with the Shapes collection. Further, if these are in the footer, then you will have to work with the Shapes collection of the footer story, and its range. They will not be in the document Shapes collection.

Asi
03-11-2009, 02:45 AM
I managed to do that using the shapes collection as you suggested.
There are 2 way I checked:
1st way:

For i = 1 To wdDoc.Shapes.Count
wdDoc.Shapes(i).Select
Selection.Fields.Update
Selection.Fields.Unlink
Next


2nd way:

For i = 1 To wdDoc.Shapes.Count
If wdDoc.Shapes(i).TextFrame.HasText = True Then
wdDoc.Shapes(i).TextFrame.TextRange.Fields.Update
wdDoc.Shapes(i).TextFrame.TextRange.Fields.Unlink
End If
Next

However, while both ways works fine when I put the code in the word doc itself (when testing), the 1st way doesn't work when I move the code to the Excel Workbook module (As I mentioned in my first post: "I have a word doc which I open from an Excel macro.")
The failure is:
Run-time error '438':
Object doesn't support this property or method
on the following line:
Selection.Fields.Update
I guess the problem is related to late binding but I can't seem to solve the problem. Why excel doesn't like me using this ?

Selection.Fields.Update

fumei
03-11-2009, 08:31 AM
Because it has to be fully qualified. It is usually better to always fully qualify things when actioning between applications.

Selection, because it is not fully qualified, means that Excel is taking "Selection" and wondering what the heck to do with that.

You are making an instance of Word, yes? Something like wdApp, or whatever. I am assuming this because of the wdDoc you are using.

so it should be:
wdApp.Selection.whatever

Selection is a property of Word, so you must use the instance of Word. This is why:

both ways works fine when I put the code in the word doc itself (when testing), the 1st way doesn't work when I move the code to the Excel Workbook moduleYes, it works in Word because it is Word object. It will not work in Excel because it is NOT an Excel object. Thus, fully qualify it using the instance of Word you have.