Macro VBA para obtener el texto seleccionado en Outlook 2003
-
25-09-2019 - |
Pregunta
Estoy intentando utilizar este fragmento de código para obtener el texto seleccionado en 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
Al ejecutar esta macro me sale el error
---------------------------
Microsoft Visual Basic
---------------------------
Compile error:
User-defined type not defined
¿Necesito agregar alguna referencia para solucionar este problema?
Solución
@Kusleika, probé la opción que sugeriste y aún aparecían los mismos errores.Gracias por la ayuda
Quizás no había formulado mi pregunta de la manera adecuada.
Un poco más de búsqueda en Google reveló que no es posible obtener el texto seleccionado de un correo en el panel de vista previa. http://www.eggheadcafe.com/forumarchives/outlookprogram_VisualBasica/Aug2005/post23481044.asp
Entonces tuve que ajustar el requisito para poder realizar una acción desde una ventana de elemento de correo.
El siguiente código me ayudó (tuve que hacer algunos cambios para satisfacer mis necesidades)
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
Fuente:http://www.outlookcode.com/threads.aspx?forumid=4&messageid=26992
@Kusleika gracias por la ayuda, ¿puedo cerrar este hilo?Por favor, avísame.
Otros consejos
Sólo en caso de que alguien está usando la palabra en lugar de editor de HTML, puede también insertar esta parte:
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
para obtener similar cuando la palabra es el editor. He intentado pegar esto en un comentario en la respuesta aceptada, pero destruyó el formato y era bastante inútil, por lo envíos como respuesta.
Dim oText As Range
TextRange es una propiedad del objeto TextFrame. Devuelve un objeto Range. No hay ningún objeto TextRange.