Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

From [Outlook] Insert text to another application

Status
Not open for further replies.

Groom84

Technical User
Feb 27, 2009
19
SE
Hi
I would like to from Outlook control and do the following:

1. Insert text and/or string to an existing email
2. Insert text and/or string to another application

I want to avoid using "SendKeys" command.

Br
Groom
 



Hi,

What code do you have so far and where are you stuck?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
See the comment areas.

Public Sub CNCText()

Dim Application As New Outlook.Application
Dim mail As Outlook.MailItem
Dim mailBody As String
Dim SpacRat As String
Dim EngFed As String
Dim TackVia As String
Dim MyText As String

ActivateWin



For i = 1 To 20
JumpToPreviousControl (1)
GetCurrentControlText
DoEvents
If i = 19 Then
End
End If

If GetCurrentControlText = "BLA" Or GetCurrentControlText = "MAD" Or GetCurrentControlText = "BOM" Or GetCurrentControlText = "RAP" Then i = 21
Next



'If BLA or MAD occurs in the text

If GetCurrentControlText = "BLA" Or GetCurrentControlText = "MAD" Then

JumpToPreviousControl (10)
TackVia = GetCurrentControlText

If GetCurrentControlText = "BLA" Or GetCurrentControlText = "MAD" Then

JumpToPreviousControl (1)
EngFed = "</b><br>Engrave Feed: <b>" & GetCurrentControlText()
Else
EngFed = "</b><br>Engrave Feed: <b>" & GetCurrentControlText()
End If

JumpToPreviousControl (18)
SpacRat = "Spacing Ratio: <b>" & GetCurrentControlText()


End If



If GetCurrentControlText = "BOM" Or GetCurrentControlText = "RAP" Then

JumpToPreviousControl (10)
TackVia = GetCurrentControlText

If GetCurrentControlText = "BOM" Or GetCurrentControlText = "RAP" Then

JumpToPreviousControl (1)
EngFed = "</b><br>Engrave Feed: <b>" & GetCurrentControlText()
Else
EngFed = "</b><br>Engrave Feed: <b>" & GetCurrentControlText()
End If

JumpToPreviousControl (18)
SpacRat = "Spacing Ratio: <b>" & GetCurrentControlText()


End If

mailBody = EngFed & SpacRat



Set mail = Application.CreateItem(olMailItem)


With mail
.SentOnBehalfOfName = "Woom"
.CC = ("Woom")
.Subject = "Try Macro"
.HTMLBody = mailBody




End With


mail.Display

'Here we start with what I would like the macro to do, we give a value to our string

MyText = "<Replace this with your text>"

'next we go to a text file window

AppActivate ("blabla.txt")

'now I am stuck here, don't know how to insert the Mytext in the txt.file
' so in the txt.file it will be inserted: <Replace this with your text>


End
End Sub

Private Sub JumpToPreviousControl(ByVal times As Integer)
Dim i As Integer
For i = 1 To times
SendKeys ("+{Tab}"), True
Next
End Sub

Private Function GetCurrentControlText() As String

Dim text As String
Dim clipBoardText As DataObject
Set clipBoardText = New DataObject


SendKeys ("^C"), True



clipBoardText.GetFromClipboard
text = clipBoardText.getText(1)


GetCurrentControlText = text

End Function

Sub ActivateWin()
Dim errorTrap1 As String

On Error Resume Next

If Err.Number = 5 Then Err = 0: AppActivate ("New Text")
If Err.Number = 5 Then Err = 0: AppActivate ("CNC")

If Err.Number = 5 Then Err = 0: End
End Sub
 



Typically, you would OPEN a text file for OUTPUT, loop thru your data, WRITING it to the file and then finally CLOSE the file.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Yes, something like that. It's just an example.
 


Well did you check out VB Help on that feature?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 


Outlook_VB_Help said:
Open Statement


Enables input/output (I/O) to a file.

Syntax

Open pathname For mode [Access access] [lock] As [#]filenumber [Len=reclength]

The Open statement syntax has these parts:

Part Description
pathname Required. String expression that specifies a file name — may include directory or folder, and drive.
mode Required. Keyword specifying the file mode: Append, Binary, Input, Output, or Random. If unspecified, the file is opened for Random access.
access Optional. Keyword specifying the operations permitted on the open file: Read, Write, or Read Write.
lock Optional. Keyword specifying the operations restricted on the open file by other processes: Shared, Lock Read, Lock Write, and Lock Read Write.
filenumber Required. A valid file number in the range 1 to 511, inclusive. Use the FreeFile function to obtain the next available file number.
reclength Optional. Number less than or equal to 32,767 (bytes). For files opened for random access, this value is the record length. For sequential files, this value is the number of characters buffered.



Remarks

You must open a file before any I/O operation can be performed on it. Open allocates a buffer for I/O to the file and determines the mode of access to use with the buffer.

If the file specified by pathname doesn't exist, it is created when a file is opened for Append, Binary, Output, or Random modes.

If the file is already opened by another process and the specified type of access is not allowed, the Open operation fails and an error occurs.

The Len clause is ignored if mode is Binary.

Important In Binary, Input, and Random modes, you can open a file using a different file number without first closing the file. In Append and Output modes, you must close a file before opening it with a different file number.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
I think we went offtrack.

Let me simplify the code:
Code:
Public Sub Sample()
    Dim MyText As String
    Dim MyData As New DataObject
    Set MyData = New DataObject
    
    
    MyText = "<Replace this with your text>"
    
    MyData.SetText MyText

    MyData.PutInClipboard


    AppActivate ("TE.txt") 'This is the open notepad window, where I would like the text to be inserted.
    
    MyData.getText   'This does not insert the text "<Replace this with your text>". Like it doesn't do anything...  Maybe I have to use some other code?
    DoEvents
   
End Sub
 
Groom: What Skip tries to tell you is that you don't NEED to activate notepad for this.
Notepad is a plain text editor.
Instead of trying to somehow magically push text into a plain text editor, you can simply WRITE plain text into a text file directly (which of course you can also open in Notepad if you wish).

The following code for example will do exactly this. write your text to a file and then open that file in notepad:
Code:
Public Sub Sample()
    Dim MyText As String
    Dim a as Integer
    
    MyText = "<Replace this with your text>"
    
    MyData.SetText MyText

    a = FreeFile
    Open "C:\test.txt" for output as a
    Print #a, MyText
    Close a
    
    Shell "notepad.exe C:\test.txt"

End Sub

Get the point?
;-)

Cheers,
MakeItSo

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Oooops!
Forgot to remove this line of yours from my code:
Code:
[COLOR=red][s]MyData.SetText MyText[/s][/color]

It is not needed.

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
And just in case you need to Append, rather than Output (which replaces the text content of an existing test.txt):
Code:
Public Sub Sample2()
Dim MyText As String
Dim a As Integer
  MyText = "Yadda yadda yadda"
  a = FreeFile
  Open "C:\test.txt" For [b]Append[/b] As a
     Print #a, MyText
  Close a
Shell "notepad.exe C:\test.txt"
End Sub
If test.txt exists - and say already has

<Replace this with your text>

then the above will make test.txt end up with:

<Replace this with your text>
Yadda yadda yadda


Gerry
 
Hi
Thanks for the feedback.
And the code really works =)

Code:
Open "C:\test.txt" For Append As a
starts a new txt file.

But I have a question for if I want to apply text to an already open application.

Like brining the focus to the application and add (append?) the text.
 
Depends on the application you wish to send to.
If it is Word, for example, you can use
Code:
Set Wrd = GetApplication("Word.Application")
Doesn't work with Notepad however.
There, on the other hand, you don't need it anyway, as you can always
Code:
Open blabla For [b]Append[/b]
;-)

For other such manipulation of text files, simply do a forum search, you'll find hundreds of examples.

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Yes, you need to state what the application is, and is it VBA compliant.

Gerry
 
That image looks like you wish to get that selection OUT...not insert into.

In any case, I may be wrong, but this looks like you would have to use API.

Gerry
 
Errrrmmm, wait a second:

This is the "map network drive" dialog?
I think you may be approaching this problem from the wrong side.
What EXACTLY are you tring to achieve?
Do you wish to MAP a drive based on some \\Server\Share value described in an E-mail or do you wish to READ the \\Server\Share value of an already mapped drive?

Both can be easily performed without Sendkeys or focussing some dialog, you know.


[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
No.
Sorry that the text "Wish copy this selection" was misleading/wrong). I was using the picture in another forum.

That was just an example of an area in a app/window where I would like the string value in >Outlook< to be inserted.

Say from Outlook Macro:
Code:
Dim MyData As New DataObject
Set MyData = New DataObject 
dim Insertthistext as string

Insertthistext = "Hello"
MyData.SetText Insertthistext
MyData.PutInClipboard
AppActivate ("Map Network Drive")

and avoid:
Code:
SendKeys ("^v"), True
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top