Breaking linked tables using VBA

alexransome

Board Regular
Joined
Jun 10, 2011
Messages
192
Hello,
I have a sheet that has data in the cells and that data is linked to areas in a word document.

I'd like to break the link just before the word doc is saved so that the data doesn't update when opening next. The next person to open the word document will not want to see a dialog box asking to update the links (my boss!!!)

This is my code so far ...

VBA Code:
'Open Word and view the VAF-MASTER file
    Set wordapp = CreateObject("word.Application")
    wordapp.Documents.Open "\\FileLocation\FileName.docx"
    wordapp.Visible = True
    
'Break Excel links in the doc
    With ActiveDocument.Fields.Unlink
    
'Auto save
    wordapp.ActiveDocument.SaveAs "\\FileLocation\FileName-" & Sheets("eMail").Range("E16")

Thanks for any assistance and help.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I think your VBA is in Excel so ...ensure that you have added reference to Microsoft Word Object Library
(in VBA editor \ Tools \ References \ scroll down \ mark the correct box OK )

Try this ...
VBA Code:
Sub alexransome()
    Dim fld As Word.Field, wordApp As Word.Application, doc As Word.Document

'Open Word and view the VAF-MASTER file
    Set wordApp = CreateObject("word.Application")
    Set doc = wordApp.Documents.Open("\\FileLocation\FileName.docx")
    wordApp.Visible = True
   
'Loop through fields
    For Each fld In doc.Fields
            If fld.Type = wdFieldLink Then fld.LinkFormat.BreakLink
    Next fld
   
'Auto save
    doc.SaveAs "\\FileLocation\FileName-" & Sheets("eMail").Range("E16")
End Sub
 
Upvote 0
Thankyou for your suggestion. It works almost perfectly, the thing is that when the word document is saved then the formatting of the now broken links changes back to the standard format.

I did have to change the "* MERGEFORMAT" to "* CHARFORMAT" so that formatting would be lost. I tried all the other options of pasting unformatted links, RTF etc.
 
Upvote 0
Just noticed that the word document linked fields in the MASTER copy which is opened has ...\a \f 5 \r } at the end of the linked field when using ALT-F9 to view the links. In the fields where the formatting changes this is at the end ... \a \t \u \* CHARFORMAT }.

What would change in endings mean?
 
Upvote 0
I did not test the code so it is useful to know this
It happens presumably because Word formats the link at time of creation (taking the formatting from the source?) and destroys the formatting when the link is broken
When I get some time (which will not be today) I will see if I can fix that

What is the linked object and how are you creating the link in the first instance?
 
Upvote 0
I did not test the code so it is useful to know this
It happens presumably because Word formats the link at time of creation (taking the formatting from the source?) and destroys the formatting when the link is broken
When I get some time (which will not be today) I will see if I can fix that

What is the linked object and how are you creating the link in the first instance?

The linked object are just cells in the sheet. What I have done is to copy the cell and then paste (via paste button top left of ribbon bar) special, as unformatted text
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,700
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top