- Katılım
- 2 Mart 2011
- Mesajlar
- 120
- Excel Vers. ve Dili
-
İşyerinnde Excel 2003
Evde Excel 2010
merhabalar,
bu kod ile excel dosyasında "D:\Oğuz\S.A.P\TESLİM EDİLENLER" dizinine klasör açıp sayfayı farklı kaydediyorum.
bu kod ile de outlookdan mail gönderebiliyorum.
şimdi benim sorum şu, ilk kodla klasör açıp kaydettiğim excel dosyamı otomatik olarak outlookda dosya eki olarak nasıl eklerim ?
bu kod ile excel dosyasında "D:\Oğuz\S.A.P\TESLİM EDİLENLER" dizinine klasör açıp sayfayı farklı kaydediyorum.
Kod:
Private Sub CommandButton1_Click()
If MsgBox("S.A.P. Kaydedilecek Emin Misiniz? ?", vbQuestion + vbYesNo, "Dikkat") = vbNo Then Exit Sub
Sheets("Sap").[D23] = TextBox1.Value
Sheets("Sap").[B41] = TextBox1.Value + " " + "S.A.P. TESLİMAT"
Sheets("Sap").[F23] = TextBox2.Value
Sheets("Sap").[D24] = TextBox3.Value
Sheets("Sap").[D27] = TextBox4.Value
Sheets("Sap").[G25] = TextBox13.Value
Sheets("Sap").[G31] = ComboBox2.Value
Sheets("Sap").[D38] = ComboBox1.Value
Sheets("Sap").[D39] = TextBox12.Value
Unload SAPTESLIM
Dim Dosya_Yolu, Dosya_Adı, ds
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set Dosya_Adı = Range("B41")
Dosya_Yolu = "D:\Oğuz\S.A.P\TESLİM EDİLENLER"
Set ds = CreateObject("Scripting.FileSystemObject")
x = Dosya_Yolu & "\" & Dosya_Adı
a = ds.FolderExists(x)
If a <> True Then
ds.CreateFolder x
End If
If Len(Dosya_Yolu) <= 3 Then Dosya_Yolu = Replace(Dosya_Yolu, "\", "")
Sheets(Array("Sap")).Copy
ActiveWorkbook.SaveAs Filename:="" & x & "\" & Dosya_Adı & " .xls", FileFormat:=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False
ActiveWorkbook.Close
MsgBox Dosya_Yolu & "\" & Dosya_Adı & ".xls" & " Dosya kayıt edildi"
Application.ScreenUpdating = True
Application.DisplayAlerts = True
If MsgBox("S.A.P. Dosyasını Yazdırmak İstiyor musunuz? ?", vbQuestion + vbYesNo, "Dikkat") = vbNo Then Exit Sub
Sheets("Sap").Range("a1:g40").PrintOut Copies:=1
ActiveWorkbook.Save
ActiveWorkbook.Close
End Sub
Kod:
Dim rng As Range
Dim OutApp As Object
Dim OutMail As Object
Set rng = Nothing
On Error Resume Next
'Only the visible cells in the selection
Set rng = Selection.SpecialCells(xlCellTypeVisible)
'You can also use a range if you want
Set rng = Sheets("MAİL OLUŞTUR").Range("A3:D28").SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected" & _
vbNewLine & "please correct and try again.", vbOKOnly
Exit Sub
End If
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = [F1]
.CC = ""
.BCC = ""
.Subject = [A1] + " " + [B1]
.HTMLBody = RangetoHTML(rng)
.Display
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2010
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
TempFile = Environ$("temp") & "/" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
'Copy the range and create a new workbook to past the data in
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.ReadAll
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")
'Close TempWB
TempWB.Close savechanges:=False
'Delete the htm file we used in this function
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
Unload Me
End Function