Ao attachment2link.mmbas

From ActivityOwnerWiki
Jump to: navigation, search
'ao_Attachment2Link  Copyright: http://creativecommons.org/licenses/by-nc-nd/3.0/
'detaches 1st attachment from selected topic(s) and adds hyperlink to saved copy in subfolder under map's directory
'Version: 10Nov2009
'Information: http://wiki.activityowner.com/
Option Explicit
Sub Main
	Dim activetopic As Topic
	Dim fname As String
	Dim subfolder As String
	subfolder="attachments"
	If Len(subfolder)>0 Then
		On Error Resume Next 'create directory if needed
		MkDir ActiveDocument.Path & "\" & subfolder
		On Error GoTo 0
	End If
	For Each activetopic In ActiveDocument.Selection
		If activetopic.Attachments.Count>0 Then
			If Len(subfolder)>0 Then
				fname = ActiveDocument.Path & "\" & subfolder & "\" & activetopic.Attachments.Item(1).FileName
			Else
				fname = ActiveDocument.Path & "\" & activetopic.Attachments.Item(1).FileName
			End If
			On Error Resume Next 'confirm file doesn't already exist
			FileDateTime(fname)
			If Err.Number>0 Then
				Err.Clear
				On Error GoTo 0
				activetopic.Attachments.Item(1).SaveAs(fname)
				activetopic.CreateHyperlink(fname)
				activetopic.Attachments.Remove(1)
			Else
				MsgBox("File Exists on disk")
			End If
		End If
	Next
End Sub