Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add.Attachments can this be linked to a variable? 2

Status
Not open for further replies.

JonEx

Programmer
Joined
Feb 26, 2004
Messages
21
Location
BB
Hey,

ok i have a form that sends emails now i have added a textbox and a command button that opens up a dialog box asking what file to attach,and when you choose a file the textbox shows the location.
example

[c:\file.txt ] [Command Button]
my question is ..
since its a dialog box the attachments will vary right now it works if i just used
Attachments.Add (c:\file.txt) but since attachments will be different all the time .. is there someway i can link the
textbox which displays the location to the attachments.add ?
for example
Attachments.Add (txtboxattachments) ?
 
hey,
but it doesnt work .. it says system cannot find specified path
 
Hey eric,

ok heres the code
code for command button to send email
************************************************************
Private Sub cmdSend_Click()
On Error GoTo Err_cmdSend_Click

Dim lngCurrentRow As Integer
Dim strTo As String
Dim strCC As String
Dim strBCC As String
Dim strAttach As String
Dim bOK As Boolean

If Me.Subject = STR_NULL1 Or IsNull(Me.Subject) Then
MsgBox "You have not entered a valid subject.",
vbCritical + vbOKOnly, "Error"
Exit Sub
End If

If Me.MessageText = STR_NULL1 Or IsNull(Me.MessageText)Then
MsgBox "You have not entered a valid message.", vbCritical + vbOKOnly, "Error"
Exit Sub
End If

strAttach = txtAttachment
If Me.txtAttachment = STR_NULL1 Or IsNull(Me.txtAttachment) Then
MsgBox "Note you did not attach any files.", vbOKOnly
Exit Sub
End If

For lngCurrentRow = 0 To lstTo.ListCount - 1
If lngCurrentRow = 0 Then
strTo = lstTo.Column(0, lngCurrentRow)
Else
strTo = strTo & ";" & lstTo.Column(0, lngCurrentRow)
End If
Next lngCurrentRow

If strTo = STR_NULL1 Or IsNull(strTo) Then
MsgBox "You have not entered a valid recipient address.", vbCritical + vbOKOnly, "Error"
Exit Sub
End If

For lngCurrentRow = 0 To lstCC.ListCount - 1
If lngCurrentRow = 0 Then
strCC = lstCC.Column(0, lngCurrentRow)
Else
strCC = strCC & ";" & lstCC.Column(0, lngCurrentRow)
End If
Next lngCurrentRow

For lngCurrentRow = 0 To lstBCC.ListCount - 1
If lngCurrentRow = 0 Then
strBCC = lstBCC.Column(0, lngCurrentRow)
Else
strBCC = strBCC & ";" & lstTo.Column(0, lngCurrentRow)
End If
Next lngCurrentRow

Call SendMsg(Me.Subject, Me.MessageText, strTo, strCC, strBCC, strAttach)

Exit_cmdSend_Click:
Exit Sub

Err_cmdSend_Click:
MsgBox err.Description
Resume Exit_cmdSend_Click

End Sub
************************************************************
code for the attachment button

************************************************************
Private Sub cmdBrowse_Click()
On Error GoTo err_cmdBrowse

Me.txtAttachment = GetOpenFile_CLT("C:\", "Select a File to Attach.")
Me.txtAttachment = LCase(Me.txtAttachment)

If Me.txtAttachment <> STR_NULL1 And Not IsNull(Me.txtAttachment) Then
Me.txtAttachmentAlias.Enabled = True
Me.txtAttachmentAlias.SetFocus
Else
Me.txtAttachmentAlias.Enabled = False
End If

exit_cmdBrowse:
Exit Sub

err_cmdBrowse:
MsgBox err.Description
Resume exit_cmdBrowse
End Sub

************************************************************
code for module that has function GetOpenFile_CLT
Function GetOpenFile_CLT(strInitialDir As String, strTitle As String) As String
' Comments : Simple file open routine. For additional options, use GetFileOpenEX_CLT()
' Parameters: strInitialDir - path for the initial directory, or blank for the current directory
' strTitle - title for the dialog
' Returns : string path, name and extension of the file selected
'
Dim fOK As Boolean
Dim typWinOpen As CLTAPI_WINOPENFILENAME
Dim typOpenFile As CLTAPI_OPENFILE
Dim strFilter As String

On Error GoTo PROC_ERR

' Set reasonable defaults for the structure
strFilter = CreateFilterString_CLT("All Files (*.*)", "*.*")

If strInitialDir <> "" Then
typOpenFile.strInitialDir = strInitialDir
Else
typOpenFile.strInitialDir = CurDir()
End If

If strTitle <> "" Then
typOpenFile.strDialogTitle = strTitle
End If

typOpenFile.strFilter = strFilter
typOpenFile.lngFlags = OFN_HIDEREADONLY Or OFN_SHOWHELP

' Convert the CLT structure to a Win structure
ConvertCLT2Win typOpenFile, typWinOpen

' Call the Common dialog
fOK = CLTAPI_GetOpenFileName(typWinOpen)

' Convert the Win structure back to a CLT structure
ConvertWin2CLT typWinOpen, typOpenFile

GetOpenFile_CLT = typOpenFile.strFullPathReturned

PROC_EXIT:
Exit Function

PROC_ERR:
GetOpenFile_CLT = ""
Resume PROC_EXIT

End Function

************************************************************
module code for automation of outlook
************************************************************
Public Sub SendMsg(strSubject As String, _
strBody As String, _
strTo As String, _
strAttach As String, _
Optional strCC As String = STR_NULL1, _
Optional strBCC As String = STR_NULL1)

Dim oLapp As Outlook.Application
Dim oItem As Outlook.MailItem

Set oLapp = CreateObject("Outlook.Application")
Set oItem = oLapp.CreateItem(0)
With oItem
.Subject = strSubject
.To = strTo
.Attachments = strAttach

If Not IsMissing(strCC) And strCC <> STR_NULL1 Then
.CC = strCC
End If
If Not IsMissing(strBCC) And strBCC <> STR_NULL1 Then
.BCC = strBCC
End If
.Body = strBody

.Send

End With
Set oLapp = Nothing
Set oItem = Nothing

End Sub

************************************************************
 
oh sorry about that ..had changed it up before posting it again
code for sendmsg
************************************************************
Public Sub SendMsg(strSubject As String, _
strBody As String, _
strTo As String, _
strAttach As String, _
Optional strCC As String = STR_NULL1, _
Optional strBCC As String = STR_NULL1)

Dim oLapp As Outlook.Application
Dim oItem As Outlook.MailItem

Set oLapp = CreateObject("Outlook.Application")
Set oItem = oLapp.CreateItem(0)
With oItem
.Subject = strSubject
.To = strTo
.Attachments.Add (txtAttachments)

If Not IsMissing(strCC) And strCC <> STR_NULL1 Then
.CC = strCC
End If
If Not IsMissing(strBCC) And strBCC <> STR_NULL1 Then
.BCC = strBCC
End If
.Body = strBody

.Send

End With
Set oLapp = Nothing
Set oItem = Nothing

End Sub



 
I believe you should change the line

.Attachments.Add (txtAttachments)

to

.Attachments.Add (strAttach)


HTH,
Eric
 
Hey thanks luceze, tried it .. still doesnt work.
Same Response " System cannot find the specified path"
could it have something to do with these other functions within this module ?

************************************************************
Option Compare Database
Option Explicit

' Declarations for Windows Common Dialogs procedures
Private Type CLTAPI_OPENFILE
strFilter As String ' Filter string
intFilterIndex As Long ' Initial Filter to display.
strInitialDir As String ' Initial directory for the dialog to open in.
strInitialFile As String ' Initial file name to populate the dialog with.
strDialogTitle As String ' Dialog title
strDefaultExtension As String ' Default extension to append to file if user didn't specify one.
lngFlags As Long ' Flags (see constant list) to be used.
strFullPathReturned As String ' Full path of file picked.
strFileNameReturned As String ' File name of file picked.
intFileOffset As Integer ' Offset in full path (strFullPathReturned) where the file name (strFileNameReturned) begins.
intFileExtension As Integer ' Offset in full path (strFullPathReturned) where the file extension begins.
End Type

Const ALLFILES = "All Files"

Private Type CLTAPI_WINOPENFILENAME
lStructSize As Long
hWndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustrFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
Flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustrData As Long
lpfnHook As Long
lpTemplateName As String
End Type

Const OFN_ALLOWMULTISELECT = &H200
Const OFN_CREATEPROMPT = &H2000
Const OFN_EXPLORER = &H80000
Const OFN_FILEMUSTEXIST = &H1000
Const OFN_HIDEREADONLY = &H4
Const OFN_NOCHANGEDIR = &H8
Const OFN_NODEREFERENCELINKS = &H100000
Const OFN_NONETWORKBUTTON = &H20000
Const OFN_NOREADONLYRETURN = &H8000
Const OFN_NOVALIDATE = &H100
Const OFN_OVERWRITEPROMPT = &H2
Const OFN_PATHMUSTEXIST = &H800
Const OFN_READONLY = &H1
Const OFN_SHOWHELP = &H10

Declare Function CLTAPI_GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" _
(pOpenfilename As CLTAPI_WINOPENFILENAME) _
As Boolean

Declare Function CLTAPI_GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" _
(pOpenfilename As CLTAPI_WINOPENFILENAME) _
As Boolean

Declare Sub CLTAPI_ChooseColor Lib "msaccess.exe" Alias "#53" _
(ByVal hwnd As Long, rgb As Long)

************************************************************
Sub ConvertCLT2Win(CLT_Struct As CLTAPI_OPENFILE, Win_Struct As CLTAPI_WINOPENFILENAME)
' Comments : Converts the passed CLTAPI structure to a Windows structure
' Parameters: CLT_Struct - record of type CLTAPI_OPENFILE
' Win_Struct - record of type CLTAPI_WINOPENFILENAME
' Returns : Nothing
'
Dim strFile As String * 512

On Error GoTo PROC_ERR

Win_Struct.hWndOwner = Application.hWndAccessApp
Win_Struct.hInstance = 0

If CLT_Struct.strFilter = "" Then
Win_Struct.lpstrFilter = ALLFILES & Chr$(0) & "*.*" & Chr$(0)
Else
Win_Struct.lpstrFilter = CLT_Struct.strFilter
End If
Win_Struct.nFilterIndex = CLT_Struct.intFilterIndex

Win_Struct.lpstrFile = String(512, 0)
Win_Struct.nMaxFile = 511

Win_Struct.lpstrFileTitle = String$(512, 0)
Win_Struct.nMaxFileTitle = 511

Win_Struct.lpstrTitle = CLT_Struct.strDialogTitle
Win_Struct.lpstrInitialDir = CLT_Struct.strInitialDir
Win_Struct.lpstrDefExt = CLT_Struct.strDefaultExtension

Win_Struct.Flags = CLT_Struct.lngFlags

Win_Struct.lStructSize = Len(Win_Struct)

PROC_EXIT:
Exit Sub

PROC_ERR:
Resume PROC_EXIT

End Sub
************************************************************
Sub ConvertWin2CLT(Win_Struct As CLTAPI_WINOPENFILENAME, CLT_Struct As CLTAPI_OPENFILE)
' Comments : Converts the passed CLTAPI structure to a Windows structure
' Parameters: Win_Struct - record of type CLTAPI_WINOPENFILENAME
' CLT_Struct - record of type CLTAPI_OPENFILE
' Returns : Nothing
'
On Error GoTo PROC_ERR

CLT_Struct.strFullPathReturned = Left(Win_Struct.lpstrFile, InStr(Win_Struct.lpstrFile, vbNullChar) - 1)
CLT_Struct.strFileNameReturned = RemoveNulls_CLT(Win_Struct.lpstrFileTitle)
CLT_Struct.intFileOffset = Win_Struct.nFileOffset
CLT_Struct.intFileExtension = Win_Struct.nFileExtension

PROC_EXIT:
Exit Sub

PROC_ERR:
Resume PROC_EXIT

End Sub
************************************************************
Function CreateFilterString_CLT(ParamArray varFilt() As Variant) As String
' Comments : Builds a Windows formatted filter string for "file type"
' Parameters: varFilter - parameter array in the format:
' Text, Filter, Text, Filter ...
' Such as:
' "All Files (*.*)", "*.*", "Text Files (*.TXT)", "*.TXT"
' Returns : windows formatted filter string
'
Dim strFilter As String
Dim intCounter As Integer
Dim intParamCount As Integer

On Error GoTo PROC_ERR

' Get the count of paramaters passed to the function
intParamCount = UBound(varFilt)

If (intParamCount <> -1) Then

' Count through each parameter
For intCounter = 0 To intParamCount
strFilter = strFilter & varFilt(intCounter) & Chr$(0)
Next

' Check for an even number of parameters
If (intParamCount Mod 2) = 0 Then
strFilter = strFilter & "*.*" & Chr$(0)
End If

End If

CreateFilterString_CLT = strFilter

PROC_EXIT:
Exit Function

PROC_ERR:
CreateFilterString_CLT = ""
Resume PROC_EXIT

End Function
************************************************************
Function RemoveNulls_CLT(strIn As String) As String
' Comments : Removes terminator from a string
' Parameters: strIn - string to modify
' Return : modified string
'
Dim intChr As Integer

intChr = InStr(strIn, Chr$(0))

If intChr > 0 Then
RemoveNulls_CLT = Left$(strIn, intChr - 1)
Else
RemoveNulls_CLT = strIn
End If

End Function
************************************************************
 
The only thing that I can suggest is to add

msgbox strAttach

at the beginning of your function to see what value is being passed to the function. Also make sure that the file exists and is in the directory that your argument strAttach specifies.

I tested your code on a sample DB that I have and it worked fine.

Let me know if you get things sorted.

HTH,
Eric
 
hey luceze,
did that in the command button and i put the msgbox just before the call sendmsg function ..
came up with the location of the file i had attached.
but when i put the msgbox strattach in the actual sendmsg function the message box comes up blank..
is there a way i can send u the dbase perhaps ? maybe you can figure it out if you actually saw it running ?
 
Got it. Change this line in your command button sub from

Call SendMsg(Me.Subject, Me.MessageText, strTo, strCC, strBCC, strAttach)

To:

Call SendMsg(Me.Subject, Me.MessageText, strTo, strAttach, strCC, strBCC)

One of your arguments was out of order.

Eric
 
Hey,
yesss ! it worked ! thank you so much luceze. It's amazing how easily we overlook stuff!

thanks alot,
regards,
Jon
 
I tossed a star at both of you for these posts. I was struggling to get email working in my db and this discussion helped me out a lot!

Thanks for the detailed code and solution. The help in Access wasn't cutting it this time. Much appreciated!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top