1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
| Sub tester1()
Dim dn() As Variant
ThisWorkbook.Sheets("shipdata").Select
Range("B1:B3").Select
i = 0
For Each cell In Selection
If cell.Value = "" Then
MsgBox ("leeg")
Exit For
End If
If cell.Offset(0, 11).Value > 0 And cell.Offset(0, 12).Value = "0" Then
MsgBox ("true")
i = i + 1
dn(i) = cell.Offset(0, 11).Value
End If
Next
MsgBox (dn1)
MsgBox (dn2)
End Sub
Sub EmailOSO()
'Error 429 occurs with GetObject if Outlook is not running.
On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
If Err.Number = 429 Then 'Outlook is NOT running.
MsgBox "Outlook moet aan staan bij het gebruik van deze subroutine!!!"
Exit Sub
End If
If MsgBox("Weet je zeker dat je de OSO-File wilt verzenden?", vbQuestion + vbYesNo) = vbNo Then
Exit Sub
End If
If MsgBox("Wil je de stock query refreshen?", vbQuestion + vbYesNo) = vbYes Then
Sheets("Stock").Select
Range("A1").Select
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
End If
Sheets("SO").Select
Range("A1").Select
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
Dim filepath As String
Dim CurrentDate As String
Dim CurrentTime As String
Dim Filename As String
CurrentTime = Time 'This variable contains the current time.
CurrentDate = Format(Date, "DD-MM-YYYY") 'This variable contains the current date.
If Left(CurrentDate, 1) = "0" Then
CurrentDate = Right$(CurrentDate, Len(CurrentDate) - 1)
End If
If Len(CurrentDate) = 9 Then
If Mid(CurrentDate, 3, 1) = "0" Then
CurrentDate = Left$(CurrentDate, 2) & Right$(CurrentDate, 6)
End If
End If
If Len(CurrentDate) = 10 Then
If Mid(CurrentDate, 4, 1) = "0" Then
CurrentDate = Left$(CurrentDate, 3) & Right$(CurrentDate, 6)
End If
End If
SaveDate = CurrentDate
Dim SaveBackupString As String 'This is the string which contains the variables for saving a copy of this workbook as backup.
SaveBackupString = WorkbookPath & "\backup\" & Format(CurrentDate, "DD-MM-YY") & " " & Format(CurrentTime, "hh.mm.ss") & " " & ActiveWorkbook.Name
'ActiveWorkbook.SaveCopyAs Filename:=SaveBackupString 'This string saves the copy of this workbook in the backup folder (which is in the root of the source file).
'ActiveWorkbook.Save
For Each cell In ActiveWorkbook.Sheets("COUNTER").Range("A:A")
If cell.Value = CurrentDate Then
ActiveWorkbook.Sheets("COUNTER").Unprotect
cell.Offset(0, 1).Value = cell.Offset(0, 1).Value + 1
IDNumber = cell.Offset(0, 1).Value
ActiveWorkbook.Sheets("COUNTER").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
End If
Next
CurrentDate = Format(Date, "DD-MM-YYYY")
Filename = "OSO" & "_" & CurrentDate & "_" & IDNumber & ".txt"
Destfolder = ActiveWorkbook.Path & "\" & "Backup OSO-files" & "\" & CurrentDate & "\"
Set wsh = CreateObject("WScript.Shell")
Set fs = CreateObject("Scripting.FileSystemObject")
If Not fs.FolderExists(Destfolder) Then
fs.CreateFolder Destfolder
End If
'##################################################################################
ActiveWorkbook.Sheets("OSO").Range("A1").Select
y1 = ActiveCell.Row
For Each cell In ActiveWorkbook.Sheets("OSO").Range("A:A")
If cell.Value = "" And cell.Offset(1, 0).Value = "" Then
y2 = cell.Offset(-1, 0).Row
Exit For
End If
Next
OSORANGE = "A" & y1 & ":" & "A" & y2
MyFileName = Destfolder & Filename
For Each cell In ActiveWorkbook.Sheets("OSO").Range(OSORANGE)
Open MyFileName For Append As #1
Print #1, cell.Value
Close #1
Next
'########################################################################################
'Working in Office 2000-2010
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "OSO" & vbNewLine & vbNewLine & _
"OSO "
On Error Resume Next
With OutMail
.to = "MYEMAIL@BLABLA.com"
.CC = ""
.BCC = ""
.Subject = Filename
.Body = strbody
.Attachments.Add (Destfolder & Filename)
.Send 'or use .Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
ActiveWorkbook.Worksheets("OSO_DB").Select
ActiveSheet.Unprotect
If Range("A2") = "" Then
Range("A2").Select
Else
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
End If
MyPath = Destfolder & Filename
ActiveCell.Formula = "=HYPERLINK(""" & MyPath & """,""" & Filename & """)"
ActiveCell.Offset(0, 1).Value = CurrentTime
ActiveCell.Offset(0, 2).Value = ActiveWorkbook.Sheets("Shiplist").Range("DNFROM").Value
ActiveCell.Offset(0, 3).Value = ActiveWorkbook.Sheets("Shiplist").Range("DNTO").Value
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
ActiveWorkbook.Save
End Sub |