L
Lee Kiwiflame
Does anybody know if VBA has changed with Word 2007? Is it best to convert
Word 2003 dot templates to Word 2007 dotm templates?
I have some code which inserts a hyperlink when a button is clicked.
However, since we upgraded to Word 2007, it crashes Word.
I noticed that if I removed the 'TextToDisplay:=displaytext' part of the
following code, the button works however it inserts the full file path
instead of just the file name.
Code:
Sub Hyperlink()
Dim fd As FileDialog, displaytext As String
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Use a With...End With block to reference the FileDialog object.
With fd
'Set the initial path to the Agenda Attachments folder.
.InitialFileName = "M:\processes\documents\agenda attachments\"
.Title = "Select the File to which you want to create the link"
'Use the Show method to display the File Picker dialog box and
return the user's action.
'If the user presses the action button...
If .Show = -1 Then
displaytext = .SelectedItems(1)
While InStr(displaytext, "\") > 0
displaytext = Mid(displaytext, InStr(displaytext, "\") + 1)
Wend
displaytext = Left(displaytext, Len(displaytext) - 4)
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range,
Address:=.SelectedItems(1), TextToDisplay:=displaytext
' 'If the user presses Cancel...
Else
End If
End With
'Set the object variable to Nothing.
Set fd = Nothing
End Sub
I can't work out why. Thanks
Word 2003 dot templates to Word 2007 dotm templates?
I have some code which inserts a hyperlink when a button is clicked.
However, since we upgraded to Word 2007, it crashes Word.
I noticed that if I removed the 'TextToDisplay:=displaytext' part of the
following code, the button works however it inserts the full file path
instead of just the file name.
Code:
Sub Hyperlink()
Dim fd As FileDialog, displaytext As String
'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Use a With...End With block to reference the FileDialog object.
With fd
'Set the initial path to the Agenda Attachments folder.
.InitialFileName = "M:\processes\documents\agenda attachments\"
.Title = "Select the File to which you want to create the link"
'Use the Show method to display the File Picker dialog box and
return the user's action.
'If the user presses the action button...
If .Show = -1 Then
displaytext = .SelectedItems(1)
While InStr(displaytext, "\") > 0
displaytext = Mid(displaytext, InStr(displaytext, "\") + 1)
Wend
displaytext = Left(displaytext, Len(displaytext) - 4)
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range,
Address:=.SelectedItems(1), TextToDisplay:=displaytext
' 'If the user presses Cancel...
Else
End If
End With
'Set the object variable to Nothing.
Set fd = Nothing
End Sub
I can't work out why. Thanks