如何在Excel宏中保留单元格的原始格式?

时间:2017-10-13 13:51:52

标签: excel vba email outlook

我是Excel VBA的新手,我设法创建了一个简单的宏来自动化一些电子邮件,这些电子邮件为我工作的人们提供了自定义报告。在这种情况下,我使用的工作表一般会根据添加到文件中的信息而改变。宏使用其中一些信息并创建自定义电子邮件。其中一些信息是时间和百分比值,在工作表中,单元格保留了这些格式。例如,如果该人在特定任务中工作了3个小时,则会将该单元显示为" 3:00"。但是在创建变量并使用它们来创建电子邮件时,这些值会以数字形式显示。

  

示例电子邮件

     

这是0.666666666666667报告!这种自动化的想法   电子邮件旨在帮助您实现每日目标期望。感觉   如果您认为可以做得更好,可以免费联系我们!

     

目前您正坐在:•评分时间00:00或00:00   质量时间•2.79282407407407E-02未评级时间从现在开始   0.666666666666667,你应该有0.3125,所以你缺少0.284571759259259。此外,您的每日完成目标现在为0.133333333333333。 PD:你有00:00所以你很完美!

我想以我需要的特定格式显示这些值,这也是单元格的原始格式。我告诉你代码,对不起,如果有新手的错误,我是新手。提前谢谢。

Sub SendEmail(what_address As String, subject_line As String, email_body As String)

Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")

    Dim olMail As Outlook.MailItem
    Set olMail = olApp.CreateItem(olMailItem)

    olMail.To = what_address
    olMail.Subject = subject_line
    olMail.BodyFormat = olFormatHTML
    olMail.HTMLBody = email_body
    olMail.Send

End Sub
Sub SendMassEmail()

row_number = 1

Do
DoEvents

    'Adding 1 to the counter

    row_number = row_number + 1

    'Creating the variables

    Dim mail_body_message As String
    Dim full_name As String
    Dim rating As String
    Dim quality_review As String
    Dim not_rating_time As String
    Dim inactive As String
    Dim absolute_target As String
    Dim missing As String
    Dim inactive_comment As String
    Dim expected_time As String
    Dim hour As String
    Dim recipient As String

    'Define the non-moving elements

    mail_body_message = Sheet10.Range("J2")
    hour = Sheet10.Range("J8")
    expected_time = Sheet10.Range("J11")
    recipient = Sheet10.Range("B" & row_number) & "@accenture.com"

    'Define the moving elements

    full_name = Sheet10.Range("A" & row_number)
    rating = Sheet10.Range("C" & row_number)
    quality_review = Sheet10.Range("D" & row_number)
    not_rating_time = Sheet10.Range("E" & row_number)
    inactive = Sheet10.Range("F" & row_number)
    absolute_target = Sheet10.Range("G" & row_number)
    missing = Sheet10.Range("H" & row_number)
    inactive_comment = Sheet10.Range("I" & row_number)

    'Replacing elements in the email body

    mail_body_message = Replace(mail_body_message, "replace_name", full_name)
    mail_body_message = Replace(mail_body_message, "replace_hour", hour)
    mail_body_message = Replace(mail_body_message, "replace_rating", rating)
    mail_body_message = Replace(mail_body_message, "replace_quality", quality_review)
    mail_body_message = Replace(mail_body_message, "replace_nrt", not_rating_time)
    mail_body_message = Replace(mail_body_message, "replace_expected", expected_time)
    mail_body_message = Replace(mail_body_message, "replace_missing", missing)
    mail_body_message = Replace(mail_body_message, "replace_target", absolute_target)
    mail_body_message = Replace(mail_body_message, "replace_inactive", inactive)
    mail_body_message = Replace(mail_body_message, "replace_inacomment", inactive_comment)


    Call SendEmail(recipient, "DO NOT RESPOND - TEST EMAIL", mail_body_message)

Loop Until row_number = Sheet10.Range("J5")

MsgBox "The e-mails have been sent!"

End Sub

1 个答案:

答案 0 :(得分:1)

您违反了单元格.Value属性。指定.Text属性以检索您在单元格中看到的内容。

class Main   {
   static ArrayList<Info> hotels = new ArrayList<Info>();
   static ArrayList<Info> tours = new ArrayList<Info>();

    public static void main(String[] args) throws IOException, ClassNotFoundException {
            Scanner sc = new Scanner(System.in );
            System.out.println("Введите тип  (Tours или Hotel) ");
            String type = sc.nextLine();
            System.out.println("Введите значение String ");
            String names= sc.nextLine();
            System.out.println("Введите значение Int ");
            int ints=sc.nextInt();
            System.out.println("Введите количество элементов массива ");
            int si = sc.nextInt();
            int array[]= new int[si];
            System.out.println("Введите элементы массива ");
            for (int i = 0; i<si; i++){
                array[i]=sc.nextInt();
            }
            if(Objects.equals(type, "Hotel")==true){
                hotels = (ArrayList<Info>)deserData("hotels.ser");
                System.out.println("jjj"+hotels.size());
                Info hotel = new Hotel(names, ints, array);
                hotels.add(hotel);
                serData("hotels", hotels);
            }
            else{
                tours = (ArrayList<Info>) deserData ("tours");
                Info tour = new Tours(names,ints,array);
                tours.add(tour);
                serData("tours", tours);
            }

       }

        private static Info  deserData(String file){
            Info objs = null;
            FileInputStream fol1 = null;
            ForLab2 fl2 = new ForLab2();
            try {
                fol1 = new FileInputStream(file);
//                ObjectInputStream in = new ObjectInputStream(fol1);
                objs=fl2.inputInfo(fol1);
                fol1.close();
//                in.close();
            } catch (FileNotFoundException e) {
                System.out.println("Ошибка1");
            } catch (IOException e) {
                System.out.println("Ошибка2");
            }
            return objs;

        }
        private static void serData(String file, Object obj) throws IOException {
            FileOutputStream fil1 = new FileOutputStream(file +".ser");
            ObjectOutputStream out = new ObjectOutputStream(fil1);
            out.writeObject(obj.toString());
            fil1.close();
            out.close();
        }
    }

   static Info inputInfo(InputStream in) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        String type = null;
        String nameNew = null;
        int intNew=0;
        int[] nArr = new int[0];
        int k=0;
        Info i= new Hotel();
        while ((strLine = br.readLine()) != null){
            if (k==0){
            type=strLine;
            k++;
        }
          else if(k==1 || k==3 || k==5){
                strLine="";
                k++;
            }
            else if (k==2){
              String result = strLine.replaceAll("[\\[\\]]", "");
              result= result.replaceAll(",", "");
//              String result = strLine.replaceAll("[|]|,", "");
                nArr = Arrays.stream(result.split(" ")).mapToInt(Integer::parseInt).toArray();
              k++;
            }
            else if(k==4){
                nameNew = strLine;
                k++;
            }
            else if (k==6){
                intNew=Integer.parseInt(strLine);

            }

        }
        if(Objects.equals(type, "Hotel")==true){
            i = new Hotel(nameNew, intNew, nArr);
        }
        else{
            i=new Tours(nameNew, intNew, nArr);
        }
        return i;
   }

您也可以将所有变量声明为字符串。