Break save a word document from excel using VBA

robovacuum_2

New Member
Joined
Feb 2, 2020
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have created the following code to update my word document (by refreshing links to cells in an external excel workbook) and then save as a new macro-free word document (including a dynamic file name that reflects the following day's date).

I would like to break all links to excel prior to saving the new word file, such that the new file can not be updated should the excel file be refreshed (I.e., the links in word would be replaced with the refreshed excel cell value alone rather than maintaining the link)

Has anyone done this before / able to assist here?

See current code below

-----------------------------------

Sub Update_links()
Dim objWord As Object
Dim objdoc As Object

Set objWord = CreateObject("Word.Application")
Set objdoc = objWord.documents.Open("C:\Users\Desktop\NPF v1.docm")
objWord.Visible = True
objdoc.Activate

Application.AskToUpdateLinks = True
objWord.Options.updatelinksatopen = refreshlinks

objdoc.SaveAs ("C:\Users\Desktop\today\" & "BB 1" & Format(Sheets("BB (dynamic)").Range("C14").Value, "dd-mmm-yy"))
objdoc.Close

End Sub
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi Robovacuum, next time you post code, please use code tags around your code (click on the <vba/> button in the toolbar of the post window)

Something like this should work:
VBA Code:
Sub Update_links()
    Dim objWord As Object
    Dim objdoc As Object
    Dim shapeLoop As Shape
    
    Set objWord = CreateObject("Word.Application")
    Set objdoc = objWord.Documents.Open("C:\Users\Desktop\NPF v1.docm")
    objWord.Visible = True
    objdoc.Activate
    
    Application.AskToUpdateLinks = True
    objWord.Options.UpdateLinksAtOpen = refreshlinks
    
    With objdoc
        'loop through all OLE object and break the links
        For Each shapeLoop In .Shapes
            With shapeLoop
                If .Type = msoLinkedOLEObject Then
                    .LinkFormat.BreakLink
                End If
            End With
        Next shapeLoop
        .SaveAs ("C:\Users\Desktop\today\" & "BB 1" & _
                 Format(Sheets("BB (dynamic)").Range("C14").Value, "dd-mmm-yy"))
        .Close
    End
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

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