-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSamsOrderModule.vba
1189 lines (988 loc) · 33.2 KB
/
SamsOrderModule.vba
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Function SelectNcopy() As String
' select first visible cell in ISBN column
Range([A2], Cells(Rows.Count, "A")).SpecialCells(xlCellTypeVisible)(1).Select
firstrow = ActiveCell.Row
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
' check the order type Local/Air or Sea
Dim tmprng As Range, c As Range, isSeaOrder As Boolean, isLocalOrAir As Boolean
isSeaOrder = True
isLocalOrAir = True
Set Ret = Range("A:Z").Find("ORDER", SearchOrder:=xlByRows, LookAt:=xlWhole)
If firstrow = lastrow Then
Set tmprng = Union(Cells(firstrow, Ret.Column), Cells(1, Ret.Column))
Else
Set tmprng = Range(Cells(firstrow, Ret.Column), Cells(lastrow, Ret.Column))
End If
For Each c In tmprng.SpecialCells(xlCellTypeVisible)
If c = "" Then
isLocalOrAir = False
End If
Next
Set Ret = Range("A:Z").Find("SEA", SearchOrder:=xlByRows, LookAt:=xlWhole)
If firstrow = lastrow Then
Set tmprng = Union(Cells(firstrow, Ret.Column), Cells(1, Ret.Column))
Else
Set tmprng = Range(Cells(firstrow, Ret.Column), Cells(lastrow, Ret.Column))
End If
For Each c In tmprng.SpecialCells(xlCellTypeVisible)
If c = "" Then
isSeaOrder = False
End If
Next
If isSeaOrder And isLocalOrAir Then
MsgBox "Cannot decide it is Local/Air or Sea order. Choose Manually."
ElseIf Not isSeaOrder And Not isLocalOrAir Then
MsgBox "This is neither a Local/Air nor Sea order. Please check."
ElseIf isSeaOrder Then
t = "SEA"
ElseIf isLocalOrAir Then
t = "ORDER"
End If
' Select and copy ISBN, Title, Quantity, Status and Date
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
firstrow = ActiveCell.Row
Dim rng1 As Range, rng2 As Range, rng3 As Range, rng4 As Range, rng5 As Range, rngUnion As Range
If t <> "ORDER" And t <> "SEA" And t <> "HK" Then
MsgBox "Unknow Order Type: " & t
End If
' Select ISBN
Set Ret = Range("A:Z").Find("ISBN", SearchOrder:=xlByRows, LookAt:=xlWhole)
Set rng1 = Range(Cells(firstrow, Ret.Column), Cells(lastrow, Ret.Column))
'Title
Set Ret = Range("A:Z").Find("Title", SearchOrder:=xlByRows, LookAt:=xlWhole)
Set rng2 = Range(Cells(firstrow, Ret.Column), Cells(lastrow, Ret.Column))
' Quantity: ORDER/SEA/HK
Set Ret = Range("A:Z").Find(t, SearchOrder:=xlByRows, LookAt:=xlWhole)
Set rng3 = Range(Cells(firstrow, Ret.Column), Cells(lastrow, Ret.Column))
'Status
Set Ret = Range("A:Z").Find("STATUS", SearchOrder:=xlByRows, LookAt:=xlWhole)
Set rng4 = Range(Cells(firstrow, Ret.Column), Cells(lastrow, Ret.Column))
'Date
Set Ret = Range("A:Z").Find("DATE", SearchOrder:=xlByRows, LookAt:=xlWhole)
Set rng5 = Range(Cells(firstrow, Ret.Column), Cells(lastrow, Ret.Column))
Set rngUnion = Union(rng1, rng2, rng3, rng4, rng5)
rngUnion.Select
Selection.copy
SelectNcopy = t
End Function
Sub copy_from_order_to_template()
Workbooks.Open filename:="J:\NewSouth Books\Inventory\Templates\Purchase Order Template.xltx"
'Windows("Purchase Order Template1").Activate
Range("A23").Select
'ActiveWindow.SmallScroll Down:=12
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End Sub
Sub make_total_line()
lastrow_plus = Cells(Rows.Count, 1).End(xlUp).Row + 1
Range(Cells(lastrow_plus, 1), Cells(lastrow_plus, 6)).Select
Call total_line
End Sub
Sub MakeOrderStep1()
'todo: automatically check website about the status, release date and title of an ISBN so that filling in orders will be easier
'todo: HK order?
' get publiser
Dim pub As String, ABK As String, country As String, freight As String, tld As Range
Set Ret = Range("A:Z").Find("PUB", SearchOrder:=xlByRows, LookAt:=xlWhole)
pub_column = Ret.Column
lastrow = Cells(Rows.Count, pub_column).End(xlUp).Row
pub = Cells(lastrow, pub_column)
pub = Replace(pub, " ", "")
t = SelectNcopy()
If pub = "UNB" Then
pub = "SOS"
End If
If pub = "PLG" Then
pub = "BIR"
End If
Call copy_from_order_to_template
Call make_total_line
' fill A6 the ABK publications
Set tld = Range("I7:I207").Find(pub, SearchOrder:=xlByRows)
ABK = Cells(tld.Row, tld.Column - 1)
If ABK = "" Then
Exit Sub
End If
Cells(6, 1) = ABK
''freight = Cells(tld.Row, tld.Column + 2)
country = Cells(tld.Row, tld.Column + 1)
country = Replace(country, " ", "")
' call USA air or UK air
If t = "ORDER" Then
If country = "USA" Or country = "CAN" Then
Call USA_Air
ElseIf country = "UK" Or country = "FRA" Then
Call UK_Air2
ElseIf country = "SING" Then
MsgBox "Singpore has sea freight only!"
End If
ElseIf t = "SEA" Then
If country = "USA" Or country = "CAN" Then
Call USAseafreight
ElseIf country = "UK" Or country = "FRA" Then
Call UK_SEA
ElseIf country = "SING" Then
Call SINGsea
ElseIf country = "AUS" Then
MsgBox "Local publisher is using Sea order"
End If
ElseIf t = "HK" Then
Call HKsea
End If
End Sub
Sub main_sea()
Call MakeOrderStep1
End Sub
Sub main_local_air()
Call MakeOrderStep1
End Sub
Sub main_hk()
Call MakeOrderStep1
End Sub
Sub main_post()
Dim pub As String, country As String, freight As String, tld As Range
HK_DELIVERY = "Global Connect Cargo Logistics Ltd (Infinity Cargo Express)"
pub = Cells(2, 2)
Set tld = Range("I7:I207").Find(pub, SearchOrder:=xlByRows)
country = Cells(tld.Row, tld.Column + 1)
fname = Cells(2, 2) & Cells(1, 2)
If country = "USA" Or country = "CAN" Or country = "UK" Or country = "SING" Or country = "FRA" Then
Call OS_PO_Format
ElseIf country = "AUS" Then
Call Aust_P_O
End If
' set print area
lastrow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Range(Cells(1, 1), Cells(lastrow, 6)).Select
ActiveSheet.PageSetup.PrintArea = Selection.Address
Cells(7, 1).Select
' save file
savepath = ""
allorderwbname = ""
For Each book In Workbooks
If book.Name Like "[0-9]*.xlsx" Or book.Name Like "Source.xlsx" Then
savepath = book.Path & "\"
allorderwbname = book.Name
End If
Next book
delivery_to = Range("A11")
If delivery_to = HK_DELIVERY Then
fname = fname & "-HK Drop Ship"
End If
ActiveWorkbook.SaveAs filename:=savepath & fname
'TODO: automatically add PO number on OSA sheet
AIR_FREIGHT = "Air Freight"
SEA_FREIGHT = "SEA FREIGHT"
LOCAL_FREIGHT = "Local"
freight = Cells(8, 1)
orderwbname = ActiveWorkbook.Name
tmpoffset = 0
' todo: fix the name issue
If freight = SEA_FREIGHT Or freight = AIR_FREIGHT Then
tmpoffset = 7
Workbooks("MyOrders.xlsx").Sheets("OSA").Activate
Else
tmpoffset = 4
Workbooks("MyOrders.xlsx").Sheets("LOA").Activate
End If
If pub = "SOS" Then
pub = "UNB"
End If
Set Ret = Range("C3:C100").Find(pub, SearchOrder:=xlByRows, LookAt:=xlWhole)
If Ret = "" Then
MsgBox "cannot find pub:(" & pub & ")"
End If
If Cells(Ret.Row, Ret.Column + tmpoffset) = "" Then
Cells(Ret.Row, Ret.Column + tmpoffset) = fname
Cells(Ret.Row, Ret.Column + tmpoffset).Font.Name = "Times New Roman"
Cells(Ret.Row, Ret.Column + tmpoffset).Font.Size = 9
' If freight = AIR_FREIGHT Or freight = LOCAL_FREIGHT Then
' Cells(Ret.Row, Ret.Column + tmpoffset).Font.Color = RGB(0, 123, 234)
' Else
' Cells(Ret.Row, Ret.Column + tmpoffset).Font.Color = RGB(94, 162, 38)
' End If
Else
Cells(Ret.Row, Ret.Column + tmpoffset) = Cells(Ret.Row, Ret.Column + tmpoffset) & ", " & fname
'commapos = InStr(Cells(Ret.Row, Ret.Column + tmpoffset), ",")
' commapos = InStrRev(Cells(Ret.Row, Ret.Column + tmpoffset), ",")
' If freight = AIR_FREIGHT Or freight = LOCAL_FREIGHT Then
' Cells(Ret.Row, Ret.Column + tmpoffset).Characters(commapos).Font.Color = RGB(0, 123, 234)
' Else
' Cells(Ret.Row, Ret.Column + tmpoffset).Characters(commapos).Font.Color = RGB(94, 162, 38)
' End If
End If
Workbooks(orderwbname).Activate
Workbooks(allorderwbname).Activate
Workbooks(orderwbname).Activate
End Sub
Sub main_post_n_send()
Dim pub As String, country As String, freight As String, tld As Range
Dim specialworkbook As String, folder As String, fullname As String
Dim book As Workbook
'todo: 3. make it
HK_DELIVERY = "Global Connect Cargo Logistics Ltd (Infinity Cargo Express)"
pub = Cells(2, 2)
Set tld = Range("I7:I207").Find(pub, SearchOrder:=xlByRows)
country = Cells(tld.Row, tld.Column + 1)
fname = Cells(2, 2) & Cells(1, 2)
If country = "USA" Or country = "CAN" Or country = "UK" Or country = "SING" Or country = "FRA" Then
Call OS_PO_Format
ElseIf country = "AUS" Then
Call Aust_P_O
End If
' set print area
lastrow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Range(Cells(1, 1), Cells(lastrow, 6)).Select
ActiveSheet.PageSetup.PrintArea = Selection.Address
Cells(7, 1).Select
' save file
savepath = ""
For Each book In Workbooks
If book.Name Like "[0-9]*.xlsx" Or book.Name Like "Source.xlsx" Then
savepath = book.Path & "\"
End If
Next book
delivery_to = Range("A11")
If delivery_to = HK_DELIVERY Then
fname = fname & "-HK Drop Ship"
End If
ActiveWorkbook.SaveAs filename:=savepath & fname
' todo: for SOS, just save it;
'TODO: automatically add PO number on OSA sheet
' find specialsheetname
specialworkbook = ""
For Each book In Workbooks
If book.Worksheets(1).Name = "get_all_orders" Then
specialworkbook = book.Name
End If
Next book
' convert to pdf
' order is already opened in single mode
folder = Application.ActiveWorkbook.Path & "\"
fullname = Application.ActiveWorkbook.Name
Call convert_to_pdf_function(specialworkbook, folder, fullname)
Call send_order_function(specialworkbook, folder, fullname)
End Sub
Sub get_all_orders()
Dim pub As String, folder As String, country As String, freight As String, tld As Range
Dim file As String, metafile As String, newbook As Workbook
'todo: add \ if not there
folder = Cells(1, 1)
Set newbook = Workbooks.Add
specialsheetname = "get_all_orders"
book1wbname = ActiveWorkbook.Name
ActiveSheet.Name = specialsheetname
Cells(1, 1) = folder
file = Dir(folder & "*.xlsx")
Count = 2
While (file <> "")
'MsgBox file
testcheck = file Like "[0-9]*.xlsx"
pub = Left(file, 3)
If testcheck Then
MsgBox "This is the meta file"
ElseIf pub = "SOS" Then
MsgBox "SOS: You should use POD template"
'ElseIf pub = "CUR" Or pub = "OBR" Or pub = "NHN" Then
' MsgBox "Put " & file & " in Brooke_to_send"
Else
Cells(Count, 1) = file
Count = Count + 1
End If
file = Dir
Wend
' copy the ABK, Publication information from template
Workbooks.Open filename:="J:\NewSouth Books\Inventory\Templates\Purchase Order Template.xltx"
templatewbname = ActiveWorkbook.Name
Range("H7:K106").Select
Selection.copy
Workbooks(book1wbname).Activate
Range("C2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Workbooks(templatewbname).Activate
ActiveWorkbook.Close savechanges:=False
' copy the EMAILS sheet from order file
Workbooks.Open filename:="J:\NewSouth Books\Inventory\Keyed Orders\Order List.xlsx"
orderlistwbname = ActiveWorkbook.Name
Sheets("EMAILS").copy after:=Workbooks(book1wbname).Sheets(1)
Workbooks(orderlistwbname).Activate
ActiveWorkbook.Sheets("EMAILS").Activate
ActiveWorkbook.Close savechanges:=False
Workbooks(book1wbname).Activate
ActiveWorkbook.Sheets(specialsheetname).Activate
End Sub
Sub send_order_function(specialworkbookname As String, folder As String, fullname As String)
Dim pub As String, country As String, freight As String, ABK As String, tld As Range, orderfmt As Range
Dim fname As String, wrdArray() As String, emailto As String, copy As String, copy2 As String, copy3 As String, copy4 As String, copy5 As String
Dim greetstr As String
'TODO: 1. handle Perseus air and sea order;
'todo: 2. if current active is get_all_orders, then do as usual; if current is an order, then send seperate email
'folder = "J:\NewSouth Books\Inventory\Keyed Orders\221214\"
'folder = "D:\Users\z3507168\Desktop\test\"
'folder = Cells(1, 1)
' some constant
local_addr = "NewSouth Books c/- TL Distribution Pty Ltd"
US_ADDR = "Aeronet"
UK_SEA_ADDR = "JAS Forwarding UK Limited"
UK_AIR_ADDR = "JAS Forwarding UK Limited"
AIR_FREIGHT = "Air Freight"
SEA_FREIGHT = "SEA FREIGHT"
LOCAL_FREIGHT = "Local"
SING_ADDR = "C.T.FREIGHT PTE LTD"
orderwbname = ActiveWorkbook.Name
book1wbname = specialworkbookname
'Workbooks(book1wbname).Activate
'' open order file
'fullname = ActiveCell
'wrdArray = Split(fullname, ".")
'fname = wrdArray(0)
'fullpathname = folder & fullname
'xlsfullpathname = fullpathname
'pdffullpathname = folder & fname & ".pdf"
'Workbooks.Open FileName:=folder & fullname
'freight = Cells(8, 1)
'Address = Cells(11, 1)
'ABK = Cells(6, 1)
'ActiveWorkbook.Close
wrdArray = Split(fullname, ".")
fname = wrdArray(0)
xlsfullpathname = folder & fullname
pdffullpathname = folder & fname & ".pdf"
freight = Cells(8, 1)
Address = Cells(11, 1)
ABK = Cells(6, 1)
Workbooks(book1wbname).Activate
Set tld = Range("C1:C200").Find(ABK, SearchOrder:=xlByRows)
pub = Cells(tld.Row, tld.Column + 1)
If pub = "CRW" Then
pub = "CRW UK"
MsgBox "Use CRW UK by default."
End If
'todo check everywhere this affects
If pub = "CS1" Then
pub = "CSO"
End If
' search in the order list and get pdf, email information
ActiveWorkbook.Sheets("EMAILS").Activate
Set orderfmt = Range("A1: A200").Find(pub, SearchOrder:=xlByRows)
pdf_or_xls = Cells(orderfmt.Row, orderfmt.Column + 1)
emailto = Cells(orderfmt.Row, orderfmt.Column + 3)
copy = Cells(orderfmt.Row, orderfmt.Column + 4)
copy2 = Cells(orderfmt.Row, orderfmt.Column + 5)
copy3 = Cells(orderfmt.Row, orderfmt.Column + 6)
copy4 = Cells(orderfmt.Row, orderfmt.Column + 7)
copy5 = Cells(orderfmt.Row, orderfmt.Column + 8)
Workbooks(book1wbname).Sheets("get_all_orders").Activate
If isEmail(emailto) = False Then
MsgBox "Email is not valid: " & Email
Exit Sub
End If
If isEmail(copy) = False Then
copy = ""
End If
If isEmail(copy2) = False Then
copy2 = ""
End If
If isEmail(copy3) = False Then
copy3 = ""
End If
If isEmail(copy4) = False Then
copy4 = ""
End If
If isEmail(copy5) = False Then
copy5 = ""
End If
CC = "[email protected];" & "[email protected];" & _
copy & ";" & copy2 & ";" & copy3 & ";" & copy4 & ";" & copy5 & ";"
' draft email
Dim objOutlook As Object
Dim objMailMessage As Outlook.MailItem
Dim emlBody, sendTo As String
Dim wkbook As String
Application.ScreenUpdating = False
Set objOutlook = CreateObject("Outlook.Application")
Set objMailMessage = objOutlook.CreateItem(0)
Subject = "Order " & fname
If Address = US_ADDR Then
fdt_to = "Aeronet"
ElseIf Address = UK_SEA_ADDR Then
fdt_to = "JAS Forwarding"
ElseIf Address = UK_AIR_ADDR Then
fdt_to = "JAS Forwarding"
ElseIf Address = local_addr Then
fdt_to = "TL Distribution."
ElseIf Address = SING_ADDR Then
fdt_to = "C.T. Freight Singapore."
End If
If Address = SING_ADDR Then
fdt_freight = ""
Else
If freight = AIR_FREIGHT Then
fdt_freight = " for air freight."
ElseIf freight = SEA_FREIGHT Then
fdt_freight = " for sea freight."
Else
fdt_freight = ""
End If
End If
greetstr = "<p>Hello,</p>"
If pub = "CSO" Then
greetstr = "<p>Hi Dalene,</p>"
ElseIf pub = "BER" Then
greetstr = "<p>Hi Raudah,</p>"
End If
strbody = greetstr & _
"<p>Please find attached " & Subject & " for delivery to " & fdt_to & fdt_freight & _
"</p>"
If pub = "SOI" Then
strbody = strbody & _
"<p>Please responde to nearest quantity.</p>"
ElseIf pub = "OBR" Then
strbody = strbody & "<p>Can you please let me know if anything on this order cannot be supplied in full?</p>"
ElseIf pub = "NHN" Then
strbody = strbody & "<p>Please advise of any out of stocks, status code or pub date changes.</p>"
ElseIf pub = "TPG" Then
Subject = Subject & " UK Delivery"
End If
notestr1 = "We have decided to consolidate our freight arrangements to JAS Forwarding UK, meaning all air and sea orders are now delivered to the same address. Please update your system so that all air freight orders are sent to the following address:" & _
"<p>JAS Forwarding UK Limited" & "<br>Cargopoint Bedfont Road" & "<br>Stanwell" & "<br>Middlesex" & "<br>TW19 7NZU UK" & _
"<br>Contact: Scott Caldwell" & "<br>scaldwell@ jasuk.com" & "<br>+44 01784 229 004</p>" & "<p>If you have any questions, please let me know.</p>"
If Address = UK_AIR_ADDR And freight = AIR_FREIGHT Then
strbody = strbody & notestr1
End If
Dim signature As String
'signature = objMailMessage.HTMLBody
signature = Environ("appdata") & _
"\Microsoft\Signatures\newsouthbooks.htm"
If pdf_or_xls = "XLS" Then
attachfile = xlsfullpathname
ElseIf pdf_or_xls = "PDF" Then
attachfile = pdffullpathname
End If
If Dir(signature) <> "" Then
signature = GetBoiler(signature)
Else
signature = ""
End If
With objMailMessage
.To = emailto
.CC = CC
.HTMLBody = strbody & "<br>" & signature
.Subject = Subject
.Attachments.Add attachfile
.Display
'.Save
End With
Workbooks(orderwbname).Activate
'workbooks(s
'With Selection.Interior
' .Pattern = xlSolid
' .PatternColorIndex = xlAutomatic
' 'yellow : 65535, green: 5296274
' .Color = 65535
' .TintAndShade = 0
' .PatternTintAndShade = 0
'End With
'Cells(ActiveCell.Row + 1, ActiveCell.Column).Select
End Sub
Sub send_order()
Dim pub As String, folder As String, country As String, freight As String, ABK As String, tld As Range, orderfmt As Range
Dim fname As String, fullname As String, wrdArray() As String, emailto As String, copy As String, copy2 As String, copy3 As String, copy4 As String, copy5 As String
Dim greetstr As String
'TODO: 1. handle Perseus air and sea order;
'todo: 2. if current active is get_all_orders, then do as usual; if current is an order, then send seperate email
'folder = "J:\NewSouth Books\Inventory\Keyed Orders\221214\"
folder = "D:\Users\z3507168\Desktop\test\"
folder = Cells(1, 1)
' some constant
local_addr = "NewSouth Books c/- TL Distribution Pty Ltd"
US_ADDR = "Aeronet"
UK_SEA_ADDR = "JAS Forwarding UK Limited"
UK_AIR_ADDR = "JAS Forwarding UK Limited"
AIR_FREIGHT = "Air Freight"
SEA_FREIGHT = "SEA FREIGHT"
LOCAL_FREIGHT = "Local"
SING_ADDR = "C.T.FREIGHT PTE LTD"
book1wbname = ActiveWorkbook.Name
Workbooks(book1wbname).Activate
' open order file
fullname = ActiveCell
wrdArray = Split(fullname, ".")
fname = wrdArray(0)
fullpathname = folder & fullname
xlsfullpathname = fullpathname
pdffullpathname = folder & fname & ".pdf"
Workbooks.Open filename:=folder & fullname
freight = Cells(8, 1)
Address = Cells(11, 1)
ABK = Cells(6, 1)
ActiveWorkbook.Close
Workbooks(book1wbname).Activate
Set tld = Range("C1:C200").Find(ABK, SearchOrder:=xlByRows)
pub = Cells(tld.Row, tld.Column + 1)
If pub = "CRW" Then
pub = "CRW UK"
MsgBox "Use CRW UK by default."
End If
'todo check everywhere this affects
If pub = "CS1" Then
pub = "CSO"
End If
' search in the order list and get pdf, email information
ActiveWorkbook.Sheets("EMAILS").Activate
Set orderfmt = Range("A1: A200").Find(pub, SearchOrder:=xlByRows)
pdf_or_xls = Cells(orderfmt.Row, orderfmt.Column + 1)
emailto = Cells(orderfmt.Row, orderfmt.Column + 3)
copy = Cells(orderfmt.Row, orderfmt.Column + 4)
copy2 = Cells(orderfmt.Row, orderfmt.Column + 5)
copy3 = Cells(orderfmt.Row, orderfmt.Column + 6)
copy4 = Cells(orderfmt.Row, orderfmt.Column + 7)
copy5 = Cells(orderfmt.Row, orderfmt.Column + 8)
If isEmail(emailto) = False Then
MsgBox "Email is not valid: " & Email
Exit Sub
End If
If isEmail(copy) = False Then
copy = ""
End If
If isEmail(copy2) = False Then
copy2 = ""
End If
If isEmail(copy3) = False Then
copy3 = ""
End If
If isEmail(copy4) = False Then
copy4 = ""
End If
If isEmail(copy5) = False Then
copy5 = ""
End If
CC = "[email protected];" & "[email protected];" & _
copy & ";" & copy2 & ";" & copy3 & ";" & copy4 & ";" & copy5 & ";"
' draft email
Dim objOutlook As Object
Dim objMailMessage As Outlook.MailItem
Dim emlBody, sendTo As String
Dim wkbook As String
Application.ScreenUpdating = False
Set objOutlook = CreateObject("Outlook.Application")
Set objMailMessage = objOutlook.CreateItem(0)
Subject = "Order " & fname
If Address = US_ADDR Then
fdt_to = "Aeronet"
ElseIf Address = UK_SEA_ADDR Then
fdt_to = "JAS Forwarding"
ElseIf Address = UK_AIR_ADDR Then
fdt_to = "JAS Forwarding"
ElseIf Address = local_addr Then
fdt_to = "TL Distribution."
ElseIf Address = SING_ADDR Then
fdt_to = "C.T. Freight Singapore."
End If
If Address = SING_ADDR Then
fdt_freight = ""
Else
If freight = AIR_FREIGHT Then
fdt_freight = " for air freight."
ElseIf freight = SEA_FREIGHT Then
fdt_freight = " for sea freight."
Else
fdt_freight = ""
End If
End If
greetstr = "<p>Hello,</p>"
If pub = "CSO" Then
greetstr = "<p>Hi Dalene,</p>"
ElseIf pub = "BER" Then
greetstr = "<p>Hi Raudah,</p>"
End If
strbody = greetstr & _
"<p>Please find attached " & Subject & " for delivery to " & fdt_to & fdt_freight & _
"</p>"
If pub = "SOI" Then
strbody = strbody & _
"<p>Please responde to nearest quantity.</p>"
ElseIf pub = "OBR" Then
strbody = strbody & "<p>Can you please let me know if anything on this order cannot be supplied in full?</p>"
ElseIf pub = "NHN" Then
strbody = strbody & "<p>Please advise of any out of stocks, status code or pub date changes.</p>"
ElseIf pub = "TPG" Then
Subject = Subject & " UK Delivery"
End If
notestr1 = "We have decided to consolidate our freight arrangements to JAS Forwarding UK, meaning all air and sea orders are now delivered to the same address. Please update your system so that all air freight orders are sent to the following address:" & _
"<p>JAS Forwarding UK Limited" & "<br>Cargopoint Bedfont Road" & "<br>Stanwell" & "<br>Middlesex" & "<br>TW19 7NZU UK" & _
"<br>Contact: Scott Caldwell" & "<br>scaldwell@ jasuk.com" & "<br>+44 01784 229 004</p>" & "<p>If you have any questions, please let me know.</p>"
If Address = UK_AIR_ADDR And freight = AIR_FREIGHT Then
strbody = strbody & notestr1
End If
Dim signature As String
'signature = objMailMessage.HTMLBody
signature = Environ("appdata") & _
"\Microsoft\Signatures\newsouthbooks.htm"
If pdf_or_xls = "XLS" Then
attachfile = xlsfullpathname
ElseIf pdf_or_xls = "PDF" Then
attachfile = pdffullpathname
End If
If Dir(signature) <> "" Then
signature = GetBoiler(signature)
Else
signature = ""
End If
With objMailMessage
.To = emailto
.CC = CC
.HTMLBody = strbody & "<br>" & signature
.Subject = Subject
.Attachments.Add attachfile
.Display
'.Save
End With
Workbooks(book1wbname).Sheets("get_all_orders").Activate
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
'yellow : 65535, green: 5296274
.Color = 65535
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Cells(ActiveCell.Row + 1, ActiveCell.Column).Select
End Sub
Function GetBoiler(ByVal sFile As String) As String
'Dick Kusleika
Dim fso As Object
Dim ts As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
GetBoiler = ts.readall
ts.Close
End Function
Sub convert_to_pdf()
Dim pub As String, folder As String, country As String, freight As String, ABK As String, tld As Range, orderfmt As Range
Dim fname As String, fullname As String, wrdArray() As String
'folder = "J:\NewSouth Books\Inventory\Keyed Orders\221214\"
folder = Cells(1, 1)
book1wbname = ActiveWorkbook.Name
Workbooks(book1wbname).Activate
' open order file
fullname = ActiveCell
wrdArray = Split(fullname, ".")
fname = wrdArray(0)
fullpathname = folder & fullname
Workbooks.Open filename:=folder & fullname
orderwbname = ActiveWorkbook.Name
ABK = Cells(6, 1)
' find the TLD
Workbooks(book1wbname).Activate
Set tld = Range("C1:C200").Find(ABK, SearchOrder:=xlByRows)
pub = Cells(tld.Row, tld.Column + 1)
' todo: switch between UK/HK according to freight type
If pub = "CRW" Then
pub = "CRW UK"
MsgBox "Use CRW UK by default."
End If
'todo check everywhere this affects
If pub = "CS1" Then
pub = "CSO"
End If
' search in the order list and get pdf, email information
ActiveWorkbook.Sheets("EMAILS").Activate
Set orderfmt = Range("A1: A200").Find(pub, SearchOrder:=xlByRows)
pdf_or_xls = Cells(orderfmt.Row, orderfmt.Column + 1)
Workbooks(orderwbname).Activate
If pdf_or_xls = "PDF" Then
Dim ws As Worksheet
Dim strPath As String
Dim myFile As Variant
Dim strFile As String
On Error GoTo errHandler
Set ws = ActiveSheet
strFile = folder & fname & ".pdf"
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
If myFile <> "False" Then
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
'MsgBox "PDF file has been created."
End If
errHandler:
If Err.Number <> 0 Then
MsgBox "Error " & Err.Number & " just occured."
MsgBox "Could not create PDF file"
End If
ElseIf pdf_or_xls = "XLS" Then
MsgBox "no need."
Else
MsgBox "unkown format: " & pdf_or_xls & "!!!!"
End If
ActiveWorkbook.Close
Workbooks(book1wbname).Activate
ActiveWorkbook.Sheets("get_all_orders").Activate
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
'yellow : .Color = 65535
.Color = 5296274
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Cells(ActiveCell.Row + 1, ActiveCell.Column).Select
End Sub
Sub convert_to_pdf_function(specialworkbookname As String, folder As String, fullname As String)
Dim pub As String, country As String, freight As String, ABK As String, tld As Range, orderfmt As Range
Dim fname As String, wrdArray() As String
orderwbname = ActiveWorkbook.Name
book1wbname = specialworkbookname
' suppose upon enter this function, the order workbook is already opened and activated
' open order file
'fullname = ActiveCell
wrdArray = Split(fullname, ".")
fname = wrdArray(0)
'fullpathname = folder & fullname
'Workbooks.Open FileName:=folder & fullname
'orderwbname = ActiveWorkbook.Name
'Workbooks(fullname).Activate
ABK = Cells(6, 1)
If ABK = "" Then
MsgBox "ABK is empty. Check"
Exit Sub
End If
' find the TLD
Workbooks(book1wbname).Activate
Set tld = Range("C1:C200").Find(ABK, SearchOrder:=xlByRows)
pub = Cells(tld.Row, tld.Column + 1)
If pub = "CRW" Then
pub = "CRW UK"
MsgBox "Use CRW UK by default."
End If
'todo check everywhere this affects
If pub = "CS1" Then
pub = "CSO"
End If
' search in the order list and get pdf, email information
ActiveWorkbook.Sheets("EMAILS").Activate
Set orderfmt = Range("A1: A200").Find(pub, SearchOrder:=xlByRows)
pdf_or_xls = Cells(orderfmt.Row, orderfmt.Column + 1)
ActiveWorkbook.Sheets("get_all_orders").Activate
Workbooks(orderwbname).Activate
If pdf_or_xls = "PDF" Then
Dim ws As Worksheet
Dim strPath As String
Dim myFile As Variant
Dim strFile As String
On Error GoTo errHandler
Set ws = ActiveSheet
strFile = folder & fname & ".pdf"
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
If myFile <> "False" Then
ws.ExportAsFixedFormat _
Type:=xlTypePDF, _
filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
'MsgBox "PDF file has been created."
End If
errHandler:
If Err.Number <> 0 Then
MsgBox "Error " & Err.Number & " just occured."
MsgBox "Could not create PDF file"
End If
ElseIf pdf_or_xls = "XLS" Then
MsgBox "no need."
Else
MsgBox "unkown format: " & pdf_or_xls & "!!!!"
End If
'ActiveWorkbook.Close
Workbooks(orderwbname).Activate
'ActiveWorkbook.Sheets("Sheet1").Activate
'With Selection.Interior
' .Pattern = xlSolid
' .PatternColorIndex = xlAutomatic
' 'yellow : .Color = 65535
' .Color = 5296274
' .TintAndShade = 0
' .PatternTintAndShade = 0
'End With
'Cells(ActiveCell.Row + 1, ActiveCell.Column).Select
End Sub
Sub FixDateFromMac2PC()
'fix date issue when copying from mac to pc
col = ActiveCell.Column
Row = ActiveCell.Row
Cells(23, 10) = 1462
Range("J23").Select
Selection.copy
Cells(Row, col).Select
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd, SkipBlanks:= _
False, Transpose:=False
Application.CutCopyMode = False
Selection.NumberFormat = "m/d/yyyy"