aajnabi
06-21-2003, 03:34 PM
Faxing from Access with WinFax using DDE, Part 3
Access Archon Column #112
Support Functions
basUtilities contains several supporting functions. The OpenWinFax function (which is run from the Open event of frmFax) attempts to open a DDE channel as a test of whether the WinFax Manager is already running; the next line closes the channel. If the channel can't be set, an error handler runs code from error 282 to run the WinFax Manager with a Shell command.
OpenWinFax in turn uses the WinFaxDir function. This function uses the GetWinFaxPath function (described below) to get the current path to the WinFax folder from tblInfo, a small table used to store miscellaneous information for use in the database. The initial value is the default location, C:\Program Files\WinFax\. The FileSystemObject from the Scripting Runtime library is used to test this folder; if it is found, WinFaxDir is set to this path. If the folder path is not found, an InputBox is popped up where the user can enter another path, which in turn is tested until a valid path is entered; the new path is saved to tblInfo with the SaveWinFaxPath sub. A backslash is appended to the path if needed.
The GetWinFaxPath function sets up a DAO recordset based on tblInfo, and picks up the value of the WinFaxPath field. The SaveWinFaxPath function also uses a DAO recordset based on this table, to save a new WinFax path.
VBA Code
Public Function OpenWinFax()
'Opens an instance the WinFax Manager (if it is not open) when
'the Fax form is opened
On Error GoTo ErrorHandler
Dim lngChannel As Long
lngChannel = DDEInitiate(Application:="FAXMNG32", topic:="CONTROL")
DDETerminate channum:=lngChannel
ErrorHandlerExit:
Exit Function
ErrorHandler:
If Err.Number = 282 Then
strWinFaxDir = WinFaxDir & "FAXMNG32.EXE"
Shell strWinFaxDir
Resume ErrorHandlerExit
Else
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End If
End Function
Public Function WinFaxDir() As String
On Error GoTo ErrorHandler
Dim strWinFaxPath As String
Dim fso As Scripting.FileSystemObject
'Check whether the standard WinFax path folder exists
Set fso = CreateObject("Scripting.FileSystemObject")
strWinFaxPath = GetWinFaxPath
TestFolder:
If Not fso.FolderExists(strWinFaxPath) Then
'Ask for custom path
strPrompt = "WinFax folder: "
strTitle = "WinFax custom path"
strWinFaxPath = CStr(InputBox(strPrompt, strTitle))
If Right(strWinFaxPath, 1) <> "\" Then
strWinFaxPath = strWinFaxPath & "\"
End If
GoTo TestFolder
Else
'Path is found; save to tblInfo
Call SaveWinFaxPath(strWinFaxPath)
WinFaxDir = strWinFaxPath
End If
ErrorHandlerExit:
Exit Function
ErrorHandler:
MsgBox "Error No: " & Err.Number & "; Description: " & _
Err.Description
Resume ErrorHandlerExit
End Function
Public Function GetWinFaxPath() As String
On Error GoTo ErrorHandler
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("tblInfo")
rst.MoveFirst
GetWinFaxPath = rst![WinFaxPath]
rst.Close
ErrorHandlerExit:
Exit Function
ErrorHandler:
MsgBox "Error No: " & Err.Number & "; Description: " & _
Err.Description
Resume ErrorHandlerExit
End Function
Public Sub SaveWinFaxPath(strPath As String)
On Error GoTo ErrorHandler
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("tblInfo")
With rst
.MoveFirst
.Edit
![WinFaxPath] = strPath
.Update
.Close
End With
ErrorHandlerExit:
Exit Sub
ErrorHandler:
MsgBox "Error No: " & Err.Number & "; Description: " & _
Err.Description
Resume ErrorHandlerExit
End Sub
References
The code in the sample database needs the following references (in addition to the default references):
Microsoft DAO 3.60 (or highest version you have)
Microsoft Scripting Runtime
If you import code or objects into a database of your own, you may need to set one or more of these references. References are set in the References dialog, opened from the VBA window. For more information on working with references, see Access Archon #107, Working with References. (http://www.helenfeddema.com/access.htm)
Access Archon Column #112
Support Functions
basUtilities contains several supporting functions. The OpenWinFax function (which is run from the Open event of frmFax) attempts to open a DDE channel as a test of whether the WinFax Manager is already running; the next line closes the channel. If the channel can't be set, an error handler runs code from error 282 to run the WinFax Manager with a Shell command.
OpenWinFax in turn uses the WinFaxDir function. This function uses the GetWinFaxPath function (described below) to get the current path to the WinFax folder from tblInfo, a small table used to store miscellaneous information for use in the database. The initial value is the default location, C:\Program Files\WinFax\. The FileSystemObject from the Scripting Runtime library is used to test this folder; if it is found, WinFaxDir is set to this path. If the folder path is not found, an InputBox is popped up where the user can enter another path, which in turn is tested until a valid path is entered; the new path is saved to tblInfo with the SaveWinFaxPath sub. A backslash is appended to the path if needed.
The GetWinFaxPath function sets up a DAO recordset based on tblInfo, and picks up the value of the WinFaxPath field. The SaveWinFaxPath function also uses a DAO recordset based on this table, to save a new WinFax path.
VBA Code
Public Function OpenWinFax()
'Opens an instance the WinFax Manager (if it is not open) when
'the Fax form is opened
On Error GoTo ErrorHandler
Dim lngChannel As Long
lngChannel = DDEInitiate(Application:="FAXMNG32", topic:="CONTROL")
DDETerminate channum:=lngChannel
ErrorHandlerExit:
Exit Function
ErrorHandler:
If Err.Number = 282 Then
strWinFaxDir = WinFaxDir & "FAXMNG32.EXE"
Shell strWinFaxDir
Resume ErrorHandlerExit
Else
MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description
Resume ErrorHandlerExit
End If
End Function
Public Function WinFaxDir() As String
On Error GoTo ErrorHandler
Dim strWinFaxPath As String
Dim fso As Scripting.FileSystemObject
'Check whether the standard WinFax path folder exists
Set fso = CreateObject("Scripting.FileSystemObject")
strWinFaxPath = GetWinFaxPath
TestFolder:
If Not fso.FolderExists(strWinFaxPath) Then
'Ask for custom path
strPrompt = "WinFax folder: "
strTitle = "WinFax custom path"
strWinFaxPath = CStr(InputBox(strPrompt, strTitle))
If Right(strWinFaxPath, 1) <> "\" Then
strWinFaxPath = strWinFaxPath & "\"
End If
GoTo TestFolder
Else
'Path is found; save to tblInfo
Call SaveWinFaxPath(strWinFaxPath)
WinFaxDir = strWinFaxPath
End If
ErrorHandlerExit:
Exit Function
ErrorHandler:
MsgBox "Error No: " & Err.Number & "; Description: " & _
Err.Description
Resume ErrorHandlerExit
End Function
Public Function GetWinFaxPath() As String
On Error GoTo ErrorHandler
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("tblInfo")
rst.MoveFirst
GetWinFaxPath = rst![WinFaxPath]
rst.Close
ErrorHandlerExit:
Exit Function
ErrorHandler:
MsgBox "Error No: " & Err.Number & "; Description: " & _
Err.Description
Resume ErrorHandlerExit
End Function
Public Sub SaveWinFaxPath(strPath As String)
On Error GoTo ErrorHandler
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("tblInfo")
With rst
.MoveFirst
.Edit
![WinFaxPath] = strPath
.Update
.Close
End With
ErrorHandlerExit:
Exit Sub
ErrorHandler:
MsgBox "Error No: " & Err.Number & "; Description: " & _
Err.Description
Resume ErrorHandlerExit
End Sub
References
The code in the sample database needs the following references (in addition to the default references):
Microsoft DAO 3.60 (or highest version you have)
Microsoft Scripting Runtime
If you import code or objects into a database of your own, you may need to set one or more of these references. References are set in the References dialog, opened from the VBA window. For more information on working with references, see Access Archon #107, Working with References. (http://www.helenfeddema.com/access.htm)