Solved

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

Posted on 2014-04-05
2
507 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 26

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now