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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting text - would like it to be a merge field... 1

Status
Not open for further replies.

chris921

Technical User
Mar 9, 2004
82
GB
I am inserting text into my document via a vb6 program - it does this just fine (when I press the button, it inserts the text as required).

However, I'd like it to insert a mergefield (its a letter customisation program) - but I cant seem to do it.

Below is the slice of code that does it (label2's caption contains the code which relates to the mergecode)

Code:
        Clipboard.Clear
        Clipboard.SetText frmDocTailAssistant.Label2.Caption, vbCFText                                  'Put text on Clipboard
        On Error Resume Next
        Set wordobj = GetObject(, "Word.Application")                        'get existing instance of Word if there is one
        If Err Then Set wordobj = CreateObject("Word.Application")      ' otherwise create a new one
        On Error GoTo 0
        With wordobj
            .Visible = True
            If .Documents.Count = 0 Then                                        'no docs
                .Documents.Add                                                       'so create one
            Else
                On Error Resume Next
                .Documents("Document1").Activate                        'activate the special doc if loaded
                If Err Then
                    .Documents.Add                                                  'not loaded so create a new one
                End If
                On Error GoTo 0
            End If
            .Selection.Paste                                                           'paste clipboard to word doc
        End With

Is there anyway that I can automatically tell word that this is a mergefield coming in, not normal text?

Thanks,
 


Hi,

Check out the MailMergeFields Collection Object and MailMergeField Object.

Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 
I now have the following line - an error occurs when I'm trying to paste into a new document:

Code:
        .selection.Fields.Add range:=.selection.range, Type:=wdFieldEmpty, Text:=frmDocTailAssistant.Label2.Caption, PreserveFormatting:=True

The error is "value out of range" - and I'm guessing its the .selection.range thats causing it - any help? :)
 



Did you check HELP on MailMerge???
Code:
ActiveDocument.MailMerge.Fields.Add Range:=Selection.Range, _
    Name:="MiddleInitial"

Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top