Name

hyperlink.CreateNewDocument(Filename, EditNow, Overwrite)

Synopsis

Sets the hyperlink to create a new file when clicked.

Argument

Settings

Filename

The name of the file to create.

EditNow

True opens the file for editing when the method runs; False waits until the user clicks the hyperlink to edit the document.

Overwrite

True replaces any existing file of the same name with a new, blank file when the method runs; False causes an error if the file already exists.

Warning

Excel may lock up if EditNow is True and Filename is a workbook (.xls).

Excel opens the file in the user’s default editor for the given file type. For example, the following code creates a new text file and opens it for editing in Notepad:

Sub CreateLinkedFile(  )
    Dim ws As Worksheet, hyp As Hyperlink, path As String
    Set ws = ActiveSheet
    path = ThisWorkbook.path
    Set hyp = ws.Hyperlinks.Add([a3], path & "\ch10_Readme.txt", _
      , "Click to edit the text file.", "Application Notes")
    hyp.CreateNewDocument path & "\ch10_Readme.txt", True, True
End Sub

Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.