' This script grids all data files of the specified type in the specified directory.
' You must define the input file extension and directory under "USER-DEFINED VARIABLES"
' Grid files (*.grd) are saved in the specified output directory.
'
' Common errors and troubleshooting:
' - If the script appears to do nothing, ensure the input_directory is valid.
' - "xMin must be < xMax" usually means there is no griddable data. Check that data exists, is in columns ABC, and is formatted as numbers (not text or general).
' - "Insufficient data in worksheet" means there are fewer than three griddable Z values.
' - "Inadequate data (all data lie in a horizontal plane)" indicates that all Z values are the same.
' - "Unknown worksheet import format" suggests the file format is incompatible with Surfer, commonly an issue with Excel 2007 files in Surfer versions 8 or lower.

Sub Main
	
	'Clear any output in the Imediate window.
	Debug.Clear

	'==========================================================================
    'USER-DEFINED VARIABLES
    '==========================================================================
	file_extension	= "dat"
	input_directory	= "C:\Program Files\Golden Software\Surfer Beta\Samples\"
	output_directory	= "C:\Temp"
	'==========================================================================

	'Initialize Surfer
	Set SurferApp = CreateObject("Surfer.Application")

	'Make sure the file extension has no extra . and the data/output directories has a trailing \
	file_extension	= LCase(Right(file_extension,(Len(file_extension) - InStrRev(file_extension,"."))))
	If  Len(input_directory) - InStrRev(input_directory, "\") <> 0 Then input_directory = input_directory + "\"
	If  Len(output_directory) - InStrRev(output_directory, "\") <> 0 Then output_directory = output_directory + "\"

	'Get the first data file in the specified directory that matches the file extension
	data_file = Dir( input_directory  + "*." + file_extension)

	'Direct the script to the FileError section if an error occurs during file processing
	On Error GoTo FileError

	'Execute the following while loop until there are no more data files in input_directory
	While data_file <> ""
		
		'Print the name of the current data file being processed in the Immediate window
		Debug.Print data_file

		'Define the path and file name of the output grid		
		grid_file = output_directory + Left(data_file, Len(data_file) - (Len(data_file) - InStrRev(data_file, ".") + 1) ) + ".grd"

		'Grid the data file with the current Surfer defaults (but do not fill the screen with grid reports)
		SurferApp.GridData6(DataFile:= input_directory + data_file, ShowReport:=False, OutGrid:=grid_file)

		'Get the next file in the directory that matches the specified file extension
		data_file = Dir()
	Wend

	'Close the Surfer application
	SurferApp.Quit

	'Display any error messages in a message box
	If errorDescriptions <> "" Then
		MsgBox errorDescriptions, vbInformation, "One or more data files could not be gridded"
	End If

	'Exit the current Sub procedure
	Exit Sub

	'Print a meaningful error message in the Immediate window for each file that did not grid correctly
	FileError :
	Debug.Print  "Error: " & Err.Description
	errorDescriptions = errorDescriptions & data_file & vbCrLf & Err.Description & vbCrLf & vbCrLf
	Resume Next

End Sub
