Get code from merge document

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Get code from merge document

Post by Rudi »

Hi,

I need to get the code from a merge document and add it into the file name for identification.

The merge document looks as follows:
Image 013.jpg
I need to extract the code after the hyphen on the 7th line of the document.
I have a recorded macro, but adapting it is a learning curve.

Any help with the code will be appreciated.

Code: Select all

Sub GetCode()
Dim x As Integer
    Selection.HomeKey Unit:=wdStory
    Selection.MoveDown Unit:=wdLine, Count:=6
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    'x = Selection.Characters.Count '< Get count of character in line for MID??
    Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "-"
        .Forward = True
    End With
    Selection.Find.Execute
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend '< Maybe a MID function or something?
    'Store the code in a variable that I can use in further code after this...
End Sub
You do not have the required permissions to view the files attached to this post.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78883
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Get code from merge document

Post by HansV »

Try this:

Code: Select all

Sub GetCode()
Dim x As Integer
Dim s As String
    Selection.HomeKey Unit:=wdStory
    Selection.MoveDown Unit:=wdLine, Count:=6
    s = ActiveDocument.Bookmarks("\line").Range.Text
    x = InStr(s, "-")
    s = Trim(Mid(s, x + 1, Len(s) - x - 2))
End Sub
s is the variable that holds the code.
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Get code from merge document

Post by Rudi »

TX. Splendid. :thumbup:
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
macropod
4StarLounger
Posts: 508
Joined: 17 Dec 2010, 03:14

Re: Get code from merge document

Post by macropod »

Rudi wrote:I need to get the code from a merge document and add it into the file name for identification.
In that case, why not use a macro to run the merge and get the details at that time. See, for example, Send Mailmerge Output to Individual Files in the Mailmerge Tips and Tricks thread at:
http://www.msofficeforums.com/mail-merg ... ricks.html" onclick="window.open(this.href);return false;
or:
http://windowssecrets.com/forums/showth ... amp-Tricks" onclick="window.open(this.href);return false;
Paul Edstein
[Fmr MS MVP - Word]

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Get code from merge document

Post by Rudi »

Many TX Paul.
I've made a note of those links for future reference. Those macros is certainly VERY handy and useful to know about.
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.