简体   繁体   中英

vb code to create excel sheet, add button to it and assign a macro to send mail

This is my first vb code.Never knew anything about vb except its name. didn't even know dim is used to declare variables in vb!!! Please help me with creation of the batch process..

I need to create a batch process which creates an excel sheet, imports values from flat into the excel sheet and sent as an attachment in an email. The excel sheet should also have a command button to send mail to the final recipient(xyz@gmail.com).

Values have to be imported into various columns depending on an 'id' field in flat file. All rows present in the flat file with same id are to be imported in one excel sheet. This excel sheet is to be sent as an attachment in a mail. The mail recipient has to read excel, put in comments(a column in excel) using drop down list, save and click a button(Forward To XYZ) to send the excel sheet to final recipient(xyz@gmail.com) with the saved comment. The creation of excel sheet, button and then a macro to send mail on button click is to be done using vb.net. please specify the namespaces imported(in VS 2008)

Consider this as the flat file data:

abc  cdsmdjn 1 337666746     
def  odkiejr 1 234585780   
ghi  kdjfjfn 1 287474675   
jkl  nfjjwke 2 455767875  
mno  jfhfiee 3 039484764

Values have to be separated using the third column. Hence, first three rows come in one excel workbook, 4th row in second workbook and 5th row in third book.

Here is some simple VBA code that I have that creates a button with an assigned macro. This code would likely be easily modified to work in VB.net. Hope it helps.

Sub CreateButton()

    Dim btn As button

    'create the button in the active sheet.
    'The 4 arguments are left, top, width and height --- i chose to base them off of cellular dimensions
    Set btn = ActiveSheet.Buttons.Add( _
        ActiveSheet.Columns(2).Left, _
        ActiveSheet.Rows(2).Top, _
        ActiveSheet.Columns(2).Width * 2, _
        ActiveSheet.Rows(1).Height * 2)

    'this is where you assing a macro to the btn
    btn.OnAction = "ShowMessageBox"

    'add a caption
    btn.Text = "Click Me"

    Set btn = Nothing

End Sub

Sub ShowMessageBox()

    MsgBox "Hello"

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM