Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro that unlinks the fields in a document, and then deletes itself

Status
Not open for further replies.

CookingFat

Technical User
Jun 30, 2021
6
0
0
GB
Hi all,

The following macro unlinks the fields in a document, and then deletes itself:

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

Sub UnlinkFields()

Dim oRange As Word.Range

With ActiveDocument
For Each oRange In .StoryRanges
Do
oRange.Fields.Unlink
Set oRange = oRange.NextStoryRange
Loop Until oRange Is Nothing
Next
End With

ThisDocument.VBProject.VBComponents.Remove _
ThisDocument.VBProject.VBComponents("NewMacros")

End Sub

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

The document is "read-only". The idea is that I'd run the above macro, then save the document as a new document, and then when I email the (new) document to someone else they'd receive a document that 1) doesn't contain any fields, 2) doesn't contain any macros, and 3) isn't attached to any template.

However, does anyone know if it's possible to modify the above macro so that it unlinks the fields only within the text boxes in the document? The issue is that, for this particular document, the text boxes contain fields that are linked to an Excel spreadsheet (which I want to unlink and replace with text), but the remaining parts of the document contain a standard hyperlink and a mailto link (which I don't want to replace with text).

Thanks very much,
Steve

Edit: I should've mentioned that the part of the above macro that deletes itself requires the following "Macro Security" option to be ticked; "Trust access to the VBA project object module".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top