|
Christian Fasold
Posts: 45
Joined: 2013-11-27
|
Hello everybody,
I am fighting with the problem that I would like to remove all empty lines in a mail automatically.
I find it really hard to understand how to control the Word-Editor inside Outlook using VB.net ...
Do you perhaps have an example how I could remove all empty lines at the end of an email?
Thanks so much for any help! :)
Best regards,
Christian |
|
Posted 23 Mar, 2015 07:37:43
|
|
Top
|
|
Andrei Smolin
Add-in Express team
Posts: 18830
Joined: 2006-05-11
|
Hello Christian,
No, we don't have such an example. You get the word document by casting Inspector.WordEditor to Word.Document. Then you scan the document checking the lines. To program the Word-related code, you can record a VBA macro in Word while performing the search and deletion of empty lines in the Word UI. The macro will reveal the Word object model entities that you use. You'll need to translate that code to VB.NET.
Andrei Smolin
Add-in Express Team Leader |
|
Posted 23 Mar, 2015 07:55:40
|
|
Top
|
|
Christian Fasold
Posts: 45
Joined: 2013-11-27
|
Andrei,
thanks a lot for the tip.
I have now managed to do this:
- Remove the signature (which I wanted anyway)
Therefore I have used this code:
Dim myItem As Outlook.Inspector
Dim objItem As Object
Dim myOlApp As Outlook.Application = <APPNAME>.AddinModule.CurrentInstance.OutlookApp
myItem = myOlApp.ActiveInspector
objItem = myItem.CurrentItem
Dim mailItem As Outlook.MailItem = TryCast(objItem, Outlook.MailItem)
Dim objDoc As Word.Document
Dim objBkm As Word.Bookmark
Try
objDoc = mailItem.GetInspector.WordEditor
objBkm = objDoc.Bookmarks("_MailAutoSig")
If Not objBkm Is Nothing Then
objBkm.Select()
objDoc.Windows(1).Selection.Delete()
End If
objDoc = Nothing
objBkm = Nothing
Catch ex As Exception
' No signature found
' --> do nothing
End Try
Now I add some text from other sources and save it as a new MailItem.
What I now want to do is to make sure to delete all emtpy lines (starting from the end of the mailItem), and I have absolutely no idea how to do that :-(
I have tried your approach using VBA, but I am afraid that doesn't lead me anywhere:
Selection.MoveDown Unit:=wdScreen, Count:=3
Selection.TypeBackspace
Selection.TypeBackspace
Selection.TypeBackspace
Because that are just the keypresses... But I need "intelligence" to check if each line, starting from the list line is an empy line. If YES, delete it; if not (if there is some text), then stop.
Can you please help me a little bit with your extensive Word/Outlook experience how to achieve that?
In the end it might well be just a few lines but I have no idea how to start here (with all those sections, paragraphs, etc. in Word)...
Thanks a lot and best regards,
Christian |
|
Posted 23 Mar, 2015 08:12:38
|
|
Top
|
|
Andrei Smolin
Add-in Express team
Posts: 18830
Joined: 2006-05-11
|
You need to define what an empty line is. I suppose the simplest definition would be "two subsequent paragraph marks": since the first paragraph mark is the end of the previous paragraph and there's no symbols before the second mark (= the end of the second paragraph), this means the second paragraph is empty. I assume there can be other definitions; say, a series of line breaks, a table row containing no text, a list entry with non-visible text, whatever. Anyway, deleting isn't the thing you'd want to record in the macro. What you need is to *search* for the empty line. This is why the definition is so important.
And I'd recommend that you press Ctrl+Shift+8 to show paragraph marks and other hidden formatting symbols. This will help you see the difference between e.g. a paragraph mark and line break.
Andrei Smolin
Add-in Express Team Leader |
|
Posted 23 Mar, 2015 08:36:37
|
|
Top
|
|
Christian Fasold
Posts: 45
Joined: 2013-11-27
|
Hello Andrei,
thanks again for your reply.
Well the problem here is that I don't want to delete empty lines _inside_ the mail, but just the trailing empty lines at the end of the mail.
So wouldn't it be the easiest way (even though I don't know how to do that with Word) to jump to the very end of the document, and delete each line if it has no characters in it?
So:
- Loop this:
- Jump to the end of the document
- If line = "" then delete line
else exit the loop
How could I do that? For somebody with your experience that it hopefully easy ;-)
I on the other hand am heavily lost here with Word and the way it works with content :-(
Thanks a lot and best regards,
Chris |
|
Posted 23 Mar, 2015 08:42:14
|
|
Top
|
|
Andrei Smolin
Add-in Express team
Posts: 18830
Joined: 2006-05-11
|
Chris,
There's no "line of text" in the Word object model. Word operates on paragraphs, not lines.
I tried to attract your attention to the fact that to regard a paragraph as empty you need a definition. Enter some text in a Word document (Outlook email) and press {ENTER} - this starts a new paragraph. At this moment, the new paragraph is visually empty and Paragraph.Characters.Count returns 1 for this paragraph; 1 stands for the paragraph mark at the end of the paragraph. While the text cursor is positioned within the new paragraph, press {SHIFT+ENTER}. Now, according to the definition "no characters on the paragraph" the paragraph isn't empty because it contains an extra character; on another hand, you find two empty lines in the document by moving the text cursor. Press Ctrl+Shift+8 to show paragraph marks and you'll see the line break mark.
Chris, what is "empty paragraph" for you? When you know the answer, you'll be able to find such paragraphs. You have two options: you can scan Paragraph objects or, you can use the Search object. In my previous replies I meant using the Search object while recording the VBA macro. But scanning Paragraphs in the reverse order may work for you as well.
Andrei Smolin
Add-in Express Team Leader |
|
Posted 24 Mar, 2015 03:09:35
|
|
Top
|
|
Christian Fasold
Posts: 45
Joined: 2013-11-27
|
Andrei,
thank you so much :)
I have now created a solution which works:
Dim myItem As Outlook.Inspector
Dim objItem As Object
Dim myOlApp As Outlook.Application = <APPNAME>.AddinModule.CurrentInstance.OutlookApp
myItem = myOlApp.ActiveInspector
objItem = myItem.CurrentItem
Dim mailItem As Outlook.MailItem = TryCast(objItem, Outlook.MailItem)
Dim objDoc As Word.Document
Dim objBkm As Word.Sentences
Try
objDoc = mailItem.GetInspector.WordEditor
objBkm = objDoc.Sentences
If Not objBkm Is Nothing Then
Dim objRange As Word.Range
objRange = objBkm.Last()
Dim tempString As String = objRange.Text
For i As Integer = tempString.Count - 1 To 0 Step -1
Dim currentASCIIChar As Integer = Asc(tempString(i))
If (currentASCIIChar > 65 And currentASCIIChar < 126) Or (currentASCIIChar > 48 And currentASCIIChar < 57) Then
' Normal Text found --> ABORT
Exit For
Else
' Special character (e.g. new line) found, delete it
tempString = tempString.Remove(i, 1)
End If
Next
objRange.Text = tempString
End If
objDoc = Nothing
objBkm = Nothing
Catch ex As Exception
' --> Do nothing
End Try
What does it do?
It takes the last sentence from the mailItem and starts from the last character to remove any characters that are not normal text (e.g. new lines). As soon as we reach the first "real" text character the deleting stops.
Best regards,
Christian |
|
Posted 24 Mar, 2015 04:55:30
|
|
Top
|
|
Andrei Smolin
Add-in Express team
Posts: 18830
Joined: 2006-05-11
|
Christian,
I suppose your code may delete some formatting. I don't know the definition of sentence that Word uses. I suggest testing this code on hidden text, tables, InlineShape objects, Shape objects anchored to a paragraph with no other text.
Andrei Smolin
Add-in Express Team Leader |
|
Posted 24 Mar, 2015 07:13:06
|
|
Top
|
|
Christian Fasold
Posts: 45
Joined: 2013-11-27
|
Andrei,
thanks for the warning, all tests up to now were OK but I will keep an eye open :)
Thanks and best regards,
Christian |
|
Posted 24 Mar, 2015 07:29:09
|
|
Top
|
|
Andrei Smolin
Add-in Express team
Posts: 18830
Joined: 2006-05-11
|
You are welcome!
Andrei Smolin
Add-in Express Team Leader |
|
Posted 24 Mar, 2015 07:30:28
|
|
Top
|
|