Two problems sending an E-mail for Outlook 2007 and VB 2005

  • Thread starter Thread starter Stephen Plotnick
  • Start date Start date
S

Stephen Plotnick

WordFileName = "C:\temp\Steve.HTM"

Dim WordContent As String

Dim AppWord As New Microsoft.Office.Interop.Word.ApplicationClass

With AppWord
.Visible = False
.ScreenUpdating = False
.DisplayAlerts = WdAlertLevel.wdAlertsNone
End With

Dim WordDoc As Microsoft.Office.Interop.Word.Document
WordDoc = AppWord.Documents.Open(CType(WordFileName, Object))
WordDoc.Content.Select()
WordDoc.Content.Copy()
Dim objClipboard As IDataObject = Clipboard.GetDataObject()

If objClipboard.GetDataPresent(DataFormats.Html) Then
WordContent = objClipboard.GetData(DataFormats.Html)
Else
WordContent = "XXX"
End If

WordDoc.Close()
WordDoc = Nothing
AppWord.Quit()

Filename = "C:\temp\IL Broker List.xls"
If Not File.Exists(Filename) Then
Console.WriteLine("E-mail name and address file does not exist")
automail("(e-mail address removed);", "Problem with STEVE.xls" ,
"Please ensure spreadsheet of E-mail address is in the C:\TEMP directory",
WordFileName)
Else
Console.WriteLine("Sending documents")
obook = GetObject("C:\TEMP\STEVE.xls")
Dim irow As Integer
Dim SendPersonName As String
For irow = 2 To 25000
If obook.Worksheets(1).Cells(irow, 1).value <> "" Then
'MsgBox(obook.Worksheets(1).Cells(irow, icol).Value)
SendPersonName = obook.Worksheets(1).cells(irow,
2).value & " " & obook.Worksheets(1).cells(irow, 3).value
Console.WriteLine(SendPersonName)
automail(obook.Worksheets(1).Cells(irow, 1).value,
"Looking To Buy Investment Propert ", WordContent, WordFileName)
Else
irow = 25000
End If
Next
End If
Close_rtn()
End Sub

Public Sub automail(ByVal mail_to As String, ByVal subject As String,
ByVal msg As String, ByVal filename As String)
Dim myOutlook As New Outlook.Application()
Dim myMailItem, attach As Object
myMailItem = myOutlook.CreateItem(Outlook.OlItemType.olMailItem)
myMailItem.HTMLbody = msg


If File.Exists(filename) Then
attach = myMailItem.Attachments
'DO NOT Send the attachement
'attach.Add(filename)
End If

If Trim(mail_to) <> "" Then
myMailItem.To = Trim(mail_to)
End If

myMailItem.SendUsingAccount =
myOutlook.Application.Session.Accounts("WellInvestments")
'myMailItem.SendUsingAccount = "WellInvestments"

subject = subject
myMailItem.Subject = subject
myMailItem.Send()
myMailItem = Nothing
myOutlook = Nothing
End Sub

Problem 1:
In the SendUsingAccount (not commented) I get the error below.
In the SendUsingAccount (commented out) the program processes but it sends
out from ny default E-mail.

Problem 2:
I have a Word Docement that I want to get the information into the body of
the E-mail. There is simple formatting, like bold and undeline. I've tried
"DOC", "DOCX", "RTF", "HTM". I cannot get the text to look correct. It is
either non formatted text or ends up having all kinds of HTML code.

Thanks in advance for assistance,
Steve


System.Reflection.TargetParameterCountException was unhandled
Message="Number of parameters specified does not match the expected
number."
Source="Microsoft.VisualBasic"
StackTrace:
at Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object
o, Type objType, String name, Object[] args, String[] paramnames, Boolean[]
CopyBack)
at
Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object
Instance, Type Type, String MemberName, Object[] Arguments, String[]
ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
at DaveWellsEMAIL.Module1.automail(String mail_to, String subject,
String msg, String filename) in C:\Users\Stephen Plotnick\Documents\Visual
Studio 2005\Projects\DaveWellsEMAIL\DaveWellsEMAIL\Module1.vb:line 95
at DaveWellsEMAIL.Module1.Main() in C:\Users\Stephen
Plotnick\Documents\Visual Studio
2005\Projects\DaveWellsEMAIL\DaveWellsEMAIL\Module1.vb:line 69
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
 
Is the HTML in the Word doc correctly formatted? Look at it and an HTML
message and try to see where the HTML is not the same.

I'd suspect that you aren't getting a valid Account object, I've used
SendUsingAccount with valid Account objects with no problems, although
reading that object property only works right in a Send event. Try getting
an Account object using one of your formulations and see if you do get a
valid Account object or if it's Nothing.

I'd also make sure to resolve all recipients as a matter of good practice:
myMailItem.Recipients.ResolveAll




Stephen Plotnick said:
WordFileName = "C:\temp\Steve.HTM"

Dim WordContent As String

Dim AppWord As New Microsoft.Office.Interop.Word.ApplicationClass

With AppWord
.Visible = False
.ScreenUpdating = False
.DisplayAlerts = WdAlertLevel.wdAlertsNone
End With

Dim WordDoc As Microsoft.Office.Interop.Word.Document
WordDoc = AppWord.Documents.Open(CType(WordFileName, Object))
WordDoc.Content.Select()
WordDoc.Content.Copy()
Dim objClipboard As IDataObject = Clipboard.GetDataObject()

If objClipboard.GetDataPresent(DataFormats.Html) Then
WordContent = objClipboard.GetData(DataFormats.Html)
Else
WordContent = "XXX"
End If

WordDoc.Close()
WordDoc = Nothing
AppWord.Quit()

Filename = "C:\temp\IL Broker List.xls"
If Not File.Exists(Filename) Then
Console.WriteLine("E-mail name and address file does not
exist")
automail("(e-mail address removed);", "Problem with STEVE.xls" ,
"Please ensure spreadsheet of E-mail address is in the C:\TEMP directory",
WordFileName)
Else
Console.WriteLine("Sending documents")
obook = GetObject("C:\TEMP\STEVE.xls")
Dim irow As Integer
Dim SendPersonName As String
For irow = 2 To 25000
If obook.Worksheets(1).Cells(irow, 1).value <> "" Then
'MsgBox(obook.Worksheets(1).Cells(irow, icol).Value)
SendPersonName = obook.Worksheets(1).cells(irow,
2).value & " " & obook.Worksheets(1).cells(irow, 3).value
Console.WriteLine(SendPersonName)
automail(obook.Worksheets(1).Cells(irow, 1).value,
"Looking To Buy Investment Propert ", WordContent, WordFileName)
Else
irow = 25000
End If
Next
End If
Close_rtn()
End Sub

Public Sub automail(ByVal mail_to As String, ByVal subject As String,
ByVal msg As String, ByVal filename As String)
Dim myOutlook As New Outlook.Application()
Dim myMailItem, attach As Object
myMailItem = myOutlook.CreateItem(Outlook.OlItemType.olMailItem)
myMailItem.HTMLbody = msg


If File.Exists(filename) Then
attach = myMailItem.Attachments
'DO NOT Send the attachement
'attach.Add(filename)
End If

If Trim(mail_to) <> "" Then
myMailItem.To = Trim(mail_to)
End If

myMailItem.SendUsingAccount =
myOutlook.Application.Session.Accounts("WellInvestments")
'myMailItem.SendUsingAccount = "WellInvestments"

subject = subject
myMailItem.Subject = subject
myMailItem.Send()
myMailItem = Nothing
myOutlook = Nothing
End Sub

Problem 1:
In the SendUsingAccount (not commented) I get the error below.
In the SendUsingAccount (commented out) the program processes but it sends
out from ny default E-mail.

Problem 2:
I have a Word Docement that I want to get the information into the body of
the E-mail. There is simple formatting, like bold and undeline. I've tried
"DOC", "DOCX", "RTF", "HTM". I cannot get the text to look correct. It is
either non formatted text or ends up having all kinds of HTML code.

Thanks in advance for assistance,
Steve


System.Reflection.TargetParameterCountException was unhandled
Message="Number of parameters specified does not match the expected
number."
Source="Microsoft.VisualBasic"
StackTrace:
at Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object
o, Type objType, String name, Object[] args, String[] paramnames,
Boolean[] CopyBack)
at
Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object
Instance, Type Type, String MemberName, Object[] Arguments, String[]
ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
at DaveWellsEMAIL.Module1.automail(String mail_to, String subject,
String msg, String filename) in C:\Users\Stephen Plotnick\Documents\Visual
Studio 2005\Projects\DaveWellsEMAIL\DaveWellsEMAIL\Module1.vb:line 95
at DaveWellsEMAIL.Module1.Main() in C:\Users\Stephen
Plotnick\Documents\Visual Studio
2005\Projects\DaveWellsEMAIL\DaveWellsEMAIL\Module1.vb:line 69
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
 
myMailItem.Recipients.ResolveAll()

myMailItem.SendUsingAccount = "WellInvestments"

The WellInvestments is the name I've given to this E-mail account. In
Outlook 2007 under tools-Account Settings "WellInvestments" is under the
name field. I have three other accounts and this one is the last one in the
list; it is also not the default one. The default one is on the top and also
the one that my program shows as the E-mail client that is going to be used
for sending the E-mails.

I'll work on the Word problem later. I've tried everything that I've read
and I can go to an E-mail and create one manually and hit the CTRL-V to
paster manaully (my program still has the Word doc in the clipboard) and it
looks perfect. I just can't get the thing to get it there programmatically.

THanks,
Steve



Ken Slovak - said:
Is the HTML in the Word doc correctly formatted? Look at it and an HTML
message and try to see where the HTML is not the same.

I'd suspect that you aren't getting a valid Account object, I've used
SendUsingAccount with valid Account objects with no problems, although
reading that object property only works right in a Send event. Try getting
an Account object using one of your formulations and see if you do get a
valid Account object or if it's Nothing.

I'd also make sure to resolve all recipients as a matter of good practice:
myMailItem.Recipients.ResolveAll




Stephen Plotnick said:
WordFileName = "C:\temp\Steve.HTM"

Dim WordContent As String

Dim AppWord As New Microsoft.Office.Interop.Word.ApplicationClass

With AppWord
.Visible = False
.ScreenUpdating = False
.DisplayAlerts = WdAlertLevel.wdAlertsNone
End With

Dim WordDoc As Microsoft.Office.Interop.Word.Document
WordDoc = AppWord.Documents.Open(CType(WordFileName, Object))
WordDoc.Content.Select()
WordDoc.Content.Copy()
Dim objClipboard As IDataObject = Clipboard.GetDataObject()

If objClipboard.GetDataPresent(DataFormats.Html) Then
WordContent = objClipboard.GetData(DataFormats.Html)
Else
WordContent = "XXX"
End If

WordDoc.Close()
WordDoc = Nothing
AppWord.Quit()

Filename = "C:\temp\IL Broker List.xls"
If Not File.Exists(Filename) Then
Console.WriteLine("E-mail name and address file does not
exist")
automail("(e-mail address removed);", "Problem with STEVE.xls"
, "Please ensure spreadsheet of E-mail address is in the C:\TEMP
directory", WordFileName)
Else
Console.WriteLine("Sending documents")
obook = GetObject("C:\TEMP\STEVE.xls")
Dim irow As Integer
Dim SendPersonName As String
For irow = 2 To 25000
If obook.Worksheets(1).Cells(irow, 1).value <> "" Then
'MsgBox(obook.Worksheets(1).Cells(irow, icol).Value)
SendPersonName = obook.Worksheets(1).cells(irow,
2).value & " " & obook.Worksheets(1).cells(irow, 3).value
Console.WriteLine(SendPersonName)
automail(obook.Worksheets(1).Cells(irow, 1).value,
"Looking To Buy Investment Propert ", WordContent, WordFileName)
Else
irow = 25000
End If
Next
End If
Close_rtn()
End Sub

Public Sub automail(ByVal mail_to As String, ByVal subject As String,
ByVal msg As String, ByVal filename As String)
Dim myOutlook As New Outlook.Application()
Dim myMailItem, attach As Object
myMailItem = myOutlook.CreateItem(Outlook.OlItemType.olMailItem)
myMailItem.HTMLbody = msg


If File.Exists(filename) Then
attach = myMailItem.Attachments
'DO NOT Send the attachement
'attach.Add(filename)
End If

If Trim(mail_to) <> "" Then
myMailItem.To = Trim(mail_to)
End If

myMailItem.SendUsingAccount =
myOutlook.Application.Session.Accounts("WellInvestments")
'myMailItem.SendUsingAccount = "WellInvestments"

subject = subject
myMailItem.Subject = subject
myMailItem.Send()
myMailItem = Nothing
myOutlook = Nothing
End Sub

Problem 1:
In the SendUsingAccount (not commented) I get the error below.
In the SendUsingAccount (commented out) the program processes but it
sends out from ny default E-mail.

Problem 2:
I have a Word Docement that I want to get the information into the body
of the E-mail. There is simple formatting, like bold and undeline. I've
tried "DOC", "DOCX", "RTF", "HTM". I cannot get the text to look correct.
It is either non formatted text or ends up having all kinds of HTML code.

Thanks in advance for assistance,
Steve


System.Reflection.TargetParameterCountException was unhandled
Message="Number of parameters specified does not match the expected
number."
Source="Microsoft.VisualBasic"
StackTrace:
at
Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o, Type
objType, String name, Object[] args, String[] paramnames, Boolean[]
CopyBack)
at
Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object
Instance, Type Type, String MemberName, Object[] Arguments, String[]
ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
at DaveWellsEMAIL.Module1.automail(String mail_to, String subject,
String msg, String filename) in C:\Users\Stephen
Plotnick\Documents\Visual Studio
2005\Projects\DaveWellsEMAIL\DaveWellsEMAIL\Module1.vb:line 95
at DaveWellsEMAIL.Module1.Main() in C:\Users\Stephen
Plotnick\Documents\Visual Studio
2005\Projects\DaveWellsEMAIL\DaveWellsEMAIL\Module1.vb:line 69
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
 
Maybe this will help.

I copied a routine I found that I was going to use to "find" the accout
names. I added it to my program and get a an error "Type 'Outlook.Account'
not defined". Maybe I'm not setting up something properly.

Sub SendUsingAccount()

Dim myOutlook As New Outlook.Application()
<-- I needed to add this line
Dim oAccount As Outlook.Account
<-- this line gets little green underline
For Each oAccount In myOutlook.Application.Session.Accounts
<-- I needed to add myOutlook to the front of "Application.Session.Accounts
If oAccount.AccountType = olPop3 Then
Dim oMail As Outlook.MailItem
oMail = Application.CreateItem(olMailItem)
oMail.Subject = "Sent using POP3 Account"
oMail.Recipients.Add("(e-mail address removed)")
oMail.Recipients.ResolveAll()
oMail.SendUsingAccount = oAccount
oMail.Send()
End If
Next
End Sub
 
After trying to resolve this problem I found a solution.

Sub SendUsingAccount(ByVal mail_to As String, ByVal subject As String,
ByVal msg As String, ByVal filename As String)

Dim myOutlook As New Outlook.Application()
Dim OutNS = myOutlook.GetNamespace("MAPI")
OutNS.Logon()
Dim oAccount = OutNS.Accounts
Dim ctr As Integer = 1

For Each oAccount In myOutlook.Application.Session.Accounts
Dim oMail As Outlook.MailItem
oMail =
myOutlook.Application.CreateItem(Outlook.OlItemType.olMailItem)
oMail.HTMLBody = msg

If Trim(mail_to) <> "" Then
oMail.To = Trim(mail_to)
End If
subject = subject
oMail.Subject = subject
oMail.Recipients.ResolveAll()
oMail.SendUsingAccount = oAccount
If ctr = 2 Then
oMail.Send()
End If
ctr = ctr + 1
Next
End Sub

This method requires I know that the account order (I need the second one in
my example); I wish there was a way to find the actual value for "oAccount"
so I could define the SendUsingAccount as the actual Name. I also never
could figure how to resolve the olPop3 not defined. I ended up taking it out
of the check; you can see what I'm talking about below.

Steve
 
Working on my Word to body of text problem.

The contents being populated in the Clipboard from the Word document are
perfect. I can go to Outlook and create a new E-mail. GO to the body of the
E_mail an hit CTRL-V. The results are perfect.

No matter what I do for .DataFormats I cannot get the transfer to work.

If I use .DataFormats.Text I get a text version without any formatting.
If I use .DataFormats.HTML I get all kinds of HTML code - unreadable
If I use .DataFormats.RTF I get all kinds of garbage - unreadsble.

My origianl Doc has been converted to HTM, DOCX, and RTF and I've tried all
these combinations; nothing seems to work.

Isn't there a way to just copy from the clipboard to the body of the E-mail;
replicating the CTRL-V which works great.

THanks,
Steve



Ken Slovak - said:
Is the HTML in the Word doc correctly formatted? Look at it and an HTML
message and try to see where the HTML is not the same.

I'd suspect that you aren't getting a valid Account object, I've used
SendUsingAccount with valid Account objects with no problems, although
reading that object property only works right in a Send event. Try getting
an Account object using one of your formulations and see if you do get a
valid Account object or if it's Nothing.

I'd also make sure to resolve all recipients as a matter of good practice:
myMailItem.Recipients.ResolveAll




Stephen Plotnick said:
WordFileName = "C:\temp\Steve.HTM"

Dim WordContent As String

Dim AppWord As New Microsoft.Office.Interop.Word.ApplicationClass

With AppWord
.Visible = False
.ScreenUpdating = False
.DisplayAlerts = WdAlertLevel.wdAlertsNone
End With

Dim WordDoc As Microsoft.Office.Interop.Word.Document
WordDoc = AppWord.Documents.Open(CType(WordFileName, Object))
WordDoc.Content.Select()
WordDoc.Content.Copy()
Dim objClipboard As IDataObject = Clipboard.GetDataObject()

If objClipboard.GetDataPresent(DataFormats.Html) Then
WordContent = objClipboard.GetData(DataFormats.Html)
Else
WordContent = "XXX"
End If

WordDoc.Close()
WordDoc = Nothing
AppWord.Quit()

Filename = "C:\temp\IL Broker List.xls"
If Not File.Exists(Filename) Then
Console.WriteLine("E-mail name and address file does not
exist")
automail("(e-mail address removed);", "Problem with STEVE.xls"
, "Please ensure spreadsheet of E-mail address is in the C:\TEMP
directory", WordFileName)
Else
Console.WriteLine("Sending documents")
obook = GetObject("C:\TEMP\STEVE.xls")
Dim irow As Integer
Dim SendPersonName As String
For irow = 2 To 25000
If obook.Worksheets(1).Cells(irow, 1).value <> "" Then
'MsgBox(obook.Worksheets(1).Cells(irow, icol).Value)
SendPersonName = obook.Worksheets(1).cells(irow,
2).value & " " & obook.Worksheets(1).cells(irow, 3).value
Console.WriteLine(SendPersonName)
automail(obook.Worksheets(1).Cells(irow, 1).value,
"Looking To Buy Investment Propert ", WordContent, WordFileName)
Else
irow = 25000
End If
Next
End If
Close_rtn()
End Sub

Public Sub automail(ByVal mail_to As String, ByVal subject As String,
ByVal msg As String, ByVal filename As String)
Dim myOutlook As New Outlook.Application()
Dim myMailItem, attach As Object
myMailItem = myOutlook.CreateItem(Outlook.OlItemType.olMailItem)
myMailItem.HTMLbody = msg


If File.Exists(filename) Then
attach = myMailItem.Attachments
'DO NOT Send the attachement
'attach.Add(filename)
End If

If Trim(mail_to) <> "" Then
myMailItem.To = Trim(mail_to)
End If

myMailItem.SendUsingAccount =
myOutlook.Application.Session.Accounts("WellInvestments")
'myMailItem.SendUsingAccount = "WellInvestments"

subject = subject
myMailItem.Subject = subject
myMailItem.Send()
myMailItem = Nothing
myOutlook = Nothing
End Sub

Problem 1:
In the SendUsingAccount (not commented) I get the error below.
In the SendUsingAccount (commented out) the program processes but it
sends out from ny default E-mail.

Problem 2:
I have a Word Docement that I want to get the information into the body
of the E-mail. There is simple formatting, like bold and undeline. I've
tried "DOC", "DOCX", "RTF", "HTM". I cannot get the text to look correct.
It is either non formatted text or ends up having all kinds of HTML code.

Thanks in advance for assistance,
Steve


System.Reflection.TargetParameterCountException was unhandled
Message="Number of parameters specified does not match the expected
number."
Source="Microsoft.VisualBasic"
StackTrace:
at
Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o, Type
objType, String name, Object[] args, String[] paramnames, Boolean[]
CopyBack)
at
Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object
Instance, Type Type, String MemberName, Object[] Arguments, String[]
ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
at DaveWellsEMAIL.Module1.automail(String mail_to, String subject,
String msg, String filename) in C:\Users\Stephen
Plotnick\Documents\Visual Studio
2005\Projects\DaveWellsEMAIL\DaveWellsEMAIL\Module1.vb:line 95
at DaveWellsEMAIL.Module1.Main() in C:\Users\Stephen
Plotnick\Documents\Visual Studio
2005\Projects\DaveWellsEMAIL\DaveWellsEMAIL\Module1.vb:line 69
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
 
Where is this code supposed to run, is it a shared addin or a VSTO addin or
standalone code?

Something is wrong with your references or something, I can reference
Outlook.Account in a declaration with no errors and I can retrieve an
account by name using olApp.Session.Accounts.Item("myAccountName") where
"myAccountName" is the DisplayName of the account I want.
 
That code looks wrong. How can you declare an oAccount object as
NameSpace.Accounts and then iterate the Accounts collection using that
Accounts collection object to retrieve an Account object?
 
I don't understand why you're messing around with the clipboard and worrying
about clipboard formats. Can't you just get the content of that Word doc as
a text string and pass that string along as the HTMLBody?
 
I do not know the answer. I do have VSTO loaded but I'm not sure what method
I'm using. I am using VS 2005 and Office 2007.

Steve
 
Document.Content is a Range object, just use that or to be specific in the
code use Document.Content.Text:

strText = oDoc.Content.Text
 
Well, you'd know what you're using better than I would.

Is there anything in the project that references VSTO, like comments or a
ThisApplication or ThisAddIn class? If not it's a shared addin.

Your code still doesn't make any sense whether it's VSTO or not, a
collection isn't a single object and I fail to see how a collection object
can be used to return one object from a collection in the For loop.
 
That works except it is still unformatted text. Maybe it is not possible to
transfer the information, especially since I can do it manually after the
program runs via the clipboard.

THanks,
Steve
 
Ken,

I actaully found some code doing google searches from an MVP named Sue. THis
is where I got the loop and tested by creating the same E-mail to the 4
accounts in my Outlook. It would be far better if I could do it by name.

In my references there are three different references to Outlook.

THanks for all you help. I'm as green as you can get. I just taught myself
VB in the last couple of months. I"ve been programming (COBOL) for many
years.
 
Sue Mosher would never post code that uses a collection object in place of
an item object, my guess is that you modified her code improperly. I have no
problems getting an Account object by using the name of the Account in place
of the Index property for Accounts.Item(index) or by iterating the Accounts
collection and checking for the Account display name.

You should never use more than one Outlook reference in your code if I
understand you correctly. If you're running in a COM addin you should just
use the Application object passed to you in the OnConnection event handler
(or in VSTO from the ThisAddIn.Application object. That's a trusted
Application object and is in-process with the running Outlook session.

In the project references you just reference the correct Outlook PIA for
managed code and use that. I'm not sure where your other references are
declared or how, but only use one.
 
If what you're getting is plain text with no HTML tags then that won't work
for an HTMLBody property.

There are also differences in the HTML produced when the Outlook editor or
the WordMail editor are used. When emails are exchanged between the two the
HTML gets even weirder looking.

I'd probably in this case save the Word doc to the file system as an HTML
file and read that file back in as a string using any of the available
methods for reading text files, perhaps a Stream. I'd also experiment with
hard-coding an HTML string and putting that into HTMLBody to ensure it ends
up with the type of results I wanted.
 
Finally I figured out a solution; I'll share this.

I took the word document and manually copy/paste into the body of the
E-mail. I than did a view source, copy and paste into a regular editor (I
use Visual Slick Edit). I than saved it as a text file. I used the same open
as it it was a Word document and did the content.text command to get it into
the body of the message.

THanks for all the help,
Steve


Ken Slovak - said:
Is the HTML in the Word doc correctly formatted? Look at it and an HTML
message and try to see where the HTML is not the same.

I'd suspect that you aren't getting a valid Account object, I've used
SendUsingAccount with valid Account objects with no problems, although
reading that object property only works right in a Send event. Try getting
an Account object using one of your formulations and see if you do get a
valid Account object or if it's Nothing.

I'd also make sure to resolve all recipients as a matter of good practice:
myMailItem.Recipients.ResolveAll




Stephen Plotnick said:
WordFileName = "C:\temp\Steve.HTM"

Dim WordContent As String

Dim AppWord As New Microsoft.Office.Interop.Word.ApplicationClass

With AppWord
.Visible = False
.ScreenUpdating = False
.DisplayAlerts = WdAlertLevel.wdAlertsNone
End With

Dim WordDoc As Microsoft.Office.Interop.Word.Document
WordDoc = AppWord.Documents.Open(CType(WordFileName, Object))
WordDoc.Content.Select()
WordDoc.Content.Copy()
Dim objClipboard As IDataObject = Clipboard.GetDataObject()

If objClipboard.GetDataPresent(DataFormats.Html) Then
WordContent = objClipboard.GetData(DataFormats.Html)
Else
WordContent = "XXX"
End If

WordDoc.Close()
WordDoc = Nothing
AppWord.Quit()

Filename = "C:\temp\IL Broker List.xls"
If Not File.Exists(Filename) Then
Console.WriteLine("E-mail name and address file does not
exist")
automail("(e-mail address removed);", "Problem with STEVE.xls"
, "Please ensure spreadsheet of E-mail address is in the C:\TEMP
directory", WordFileName)
Else
Console.WriteLine("Sending documents")
obook = GetObject("C:\TEMP\STEVE.xls")
Dim irow As Integer
Dim SendPersonName As String
For irow = 2 To 25000
If obook.Worksheets(1).Cells(irow, 1).value <> "" Then
'MsgBox(obook.Worksheets(1).Cells(irow, icol).Value)
SendPersonName = obook.Worksheets(1).cells(irow,
2).value & " " & obook.Worksheets(1).cells(irow, 3).value
Console.WriteLine(SendPersonName)
automail(obook.Worksheets(1).Cells(irow, 1).value,
"Looking To Buy Investment Propert ", WordContent, WordFileName)
Else
irow = 25000
End If
Next
End If
Close_rtn()
End Sub

Public Sub automail(ByVal mail_to As String, ByVal subject As String,
ByVal msg As String, ByVal filename As String)
Dim myOutlook As New Outlook.Application()
Dim myMailItem, attach As Object
myMailItem = myOutlook.CreateItem(Outlook.OlItemType.olMailItem)
myMailItem.HTMLbody = msg


If File.Exists(filename) Then
attach = myMailItem.Attachments
'DO NOT Send the attachement
'attach.Add(filename)
End If

If Trim(mail_to) <> "" Then
myMailItem.To = Trim(mail_to)
End If

myMailItem.SendUsingAccount =
myOutlook.Application.Session.Accounts("WellInvestments")
'myMailItem.SendUsingAccount = "WellInvestments"

subject = subject
myMailItem.Subject = subject
myMailItem.Send()
myMailItem = Nothing
myOutlook = Nothing
End Sub

Problem 1:
In the SendUsingAccount (not commented) I get the error below.
In the SendUsingAccount (commented out) the program processes but it
sends out from ny default E-mail.

Problem 2:
I have a Word Docement that I want to get the information into the body
of the E-mail. There is simple formatting, like bold and undeline. I've
tried "DOC", "DOCX", "RTF", "HTM". I cannot get the text to look correct.
It is either non formatted text or ends up having all kinds of HTML code.

Thanks in advance for assistance,
Steve


System.Reflection.TargetParameterCountException was unhandled
Message="Number of parameters specified does not match the expected
number."
Source="Microsoft.VisualBasic"
StackTrace:
at
Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o, Type
objType, String name, Object[] args, String[] paramnames, Boolean[]
CopyBack)
at
Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object
Instance, Type Type, String MemberName, Object[] Arguments, String[]
ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
at DaveWellsEMAIL.Module1.automail(String mail_to, String subject,
String msg, String filename) in C:\Users\Stephen
Plotnick\Documents\Visual Studio
2005\Projects\DaveWellsEMAIL\DaveWellsEMAIL\Module1.vb:line 95
at DaveWellsEMAIL.Module1.Main() in C:\Users\Stephen
Plotnick\Documents\Visual Studio
2005\Projects\DaveWellsEMAIL\DaveWellsEMAIL\Module1.vb:line 69
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
 
Back
Top