Macro VBA pour obtenir le texte sélectionné dans Outlook 2003
-
25-09-2019 - |
Question
Je suis en train d'utiliser cet extrait de code pour obtenir le texte sélectionné dans Outlook 2003
Sub SelectedTextDispaly()
On Error Resume Next
Err.Clear
Dim oText As TextRange
''# Get an object reference to the selected text range.
Set oText = ActiveWindow.Selection.TextRange
''# Check to see whether error occurred when getting text object
''# reference.
If Err.Number <> 0 Then
MsgBox "Invalid Selection. Please highlight some text " _
& "or select a text frame and run the macro again.", _
vbExclamation
End
End If
''# Display the selected text in a message box.
If oText.Text = "" Then
MsgBox "No Text Selected.", vbInformation
Else
MsgBox oText.Text, vbInformation
End If
End Sub
Lors de l'exécution de cette macro je reçois l'erreur
---------------------------
Microsoft Visual Basic
---------------------------
Compile error:
User-defined type not defined
Dois-je ajouter des références à corriger ça?
La solution
@Kusleika, j'ai essayé l'option que vous aviez suggéré et toujours les mêmes erreurs a été soulevée. Merci pour l'aide
Peut-être que je ne l'avais pas formulé ma question de la manière appropriée
Un peu plus googler a révélé que ce ne est pas possible d'obtenir le texte sélectionné d'un courrier dans le panneau de prévisualisation. http://www.eggheadcafe.com/forumarchives/outlookprogram_VisualBasica/Aug2005/post23481044. asp
Je devais ajuster l'exigence pour que je puisse faire une action d'une fenêtre de l'élément de courrier.
Le code suivant m'a aidé (dû faire quelques changements pour répondre à mes besoins)
Sub Blue_Code_Highlight()
Dim msg As Outlook.MailItem
Dim insp As Outlook.Inspector
Set insp = Application.ActiveInspector
If insp.CurrentItem.Class = olMail Then
Set msg = insp.CurrentItem
If insp.EditorType = olEditorHTML Then
Set hed = msg.GetInspector.HTMLEditor
Set rng = hed.Selection.createRange
rng.pasteHTML "<font style='color: blue; font-family:Times New Roman; font-size: 10pt;'>" & rng.Text & "</font><br/>"
End If
End If
Set insp = Nothing
Set rng = Nothing
Set hed = Nothing
Set msg = Nothing
End Sub
Source: http://www.outlookcode.com/threads.aspx ? ForumID = 4 & messageid = 26992
@Kusleika merci pour l'aide, puis-je fermer ce fil. Pls moi.
Autres conseils
Juste au cas où quelqu'un utilise l'éditeur de texte au lieu de html, vous pouvez également insérer cette partie:
If insp.EditorType = olEditorWord Then
Set hed = msg.GetInspector.WordEditor
Set word = hed.Application
Set rng = word.Selection
rng.Font.Name = "Times New Roman"
rng.Font.Size = 10
rng.Font.Color = wdColorBlack
End If
pour obtenir la même quand le mot est l'éditeur. j'ai essayé de le coller dans un commentaire sur la réponse acceptée, mais il a détruit la mise en forme et était assez inutile, si l'affichage comme une réponse.
Dim oText As Range
TextRange est une propriété de l'objet TextFrame. Elle retourne un objet Range. Il n'y a pas d'objet TextRange.