Solved

VB.net Excel Add-in Convert Word file to pfd and email

Posted on 2014-04-05
2
515 Views
Last Modified: 2014-04-06
Hi

I have an Excel 2007 add-in that I have built in VB.net 2010.

What VB.net code would I use to convert a closed Word .dotx file to pdf

and email the pfd file via Outlook?
0
Comment
Question by:murbro
2 Comments
 
LVL 27

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 39980852
You can use Office Interop, but is is better to use a managed dll.

Here is some sample code to convert a file to pdf using Office Interop:

Imports Microsoft.Office.Interop

Dim MyApp As New Word.Application
Dim MyWordDoc As Word.Document = MyApp.Documents.Open("C:\Test\Test.docx")
MyWordDoc.SaveAs(FileLocation, Word.WdSaveFormat.wdFormatPDF)

Open in new window


This is a sample sub to send an attachment (from http://www.dreamincode.net/forums/topic/60975-office-automation/):
Try
	    Dim objOutlook As Outlook.Application
	    Dim objEmail As Outlook.MailItem
	 
	    objOutlook = CType(CreateObject("Outlook.Application"), Outlook.Application)
	    objEmail = objOutlook.CreateItem(Outlook.OlItemType.olMailItem)
	 
	    Dim body As String
	    'Gets First word in TextBox
	    Dim firstWord As String = System.Text.RegularExp<b></b>ressions.Regex.Match(txtContact.Text, "^\w+\b").Value
	 
	    body = "Hi " & firstWord & "," & vbCrLf & vbCrLf
	    body += "Please find attached purchase order number " & txtPONumber.Text & "." & vbCrLf & vbCrLf
	    body += "Please confirm lead time." & vbCrLf & vbCrLf
	    body += "Thanks and Best Regards," & vbCrLf & cmbBuyer.SelectedItem.ToString
	 
	    With objEmail
	        .Subject = "Purchase Order " & txtPONumber.Text
	        .To = txtEmail.Text
	        .Body = body
	        .Attachments.Add(PDFFile)
	        .Display(True)
	    End With
	Catch ex As Exception
	    MsgBox("Unable to generate automatic email. Please create the email manually.", MsgBoxStyle.Information, "Error!")
	End Try

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 39980958
Great. Thanks very much
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question