Results 1 to 20 of 20

Thread: [RESOLVED] VBA Word - GET MERGE FIELD text.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    272

    Resolved [RESOLVED] VBA Word - GET MERGE FIELD text.

    Can anyone help me to get the text of merge fields in my word document which is given by my client.

    some of the filed names
    {MERGEFIELD M_152 \* MERGEFORMAT}
    {MERGEFIELD M_115 \* MERGEFORMAT}
    {MERGEFIELD M_112 \* MERGEFORMAT}
    {MERGEFIELD M_182 \* MERGEFORMAT}

    Edit: some of them are in header and footer section
    DONT RUN EXECUTABLE FILES FROM UNKNOWN SOURCES.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA Word - GET MERGE FIELD text.

    this will work, but i was unable to return the field by name
    vb Code:
    1. For i = 1 To d.Fields.Count
    2.     If d.Fields(i).Code = " MERGEFIELD NAME_2 " Then MsgBox d.Fields(i).Result: Exit For
    3. Next
    where d is a document object, note spaces in field name
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VBA Word - GET MERGE FIELD text.

    Moved To Office Development

  4. #4
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: VBA Word - GET MERGE FIELD text.

    Don't go at it through the field codes. Use the MailMerge object of the document.

    Code:
    activedocument.MailMerge.DataSource.DataFields("State").Value

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    272

    Re: VBA Word - GET MERGE FIELD text.

    Quote Originally Posted by westconn1
    this will work, but i was unable to return the field by name
    vb Code:
    1. For i = 1 To d.Fields.Count
    2.     If d.Fields(i).Code = " MERGEFIELD NAME_2 " Then MsgBox d.Fields(i).Result: Exit For
    3. Next
    where d is a document object, note spaces in field name
    Code:
    Set d = ActiveDocument
    For i = 1 To d.Fields.Count
    If d.Fields(i).Code = " MERGEFIELD M_108 \* MERGEFORMAT " Then
    MsgBox d.Fields(i).Result: Exit For
    End If
    Next
    Thanks for your reply. It is not counting the header and footer megefields. can you have any idea to get from header also?
    DONT RUN EXECUTABLE FILES FROM UNKNOWN SOURCES.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    272

    Re: VBA Word - GET MERGE FIELD text.

    Quote Originally Posted by dmaruca
    Don't go at it through the field codes. Use the MailMerge object of the document.

    Code:
    activedocument.MailMerge.DataSource.DataFields("State").Value

    It giving runtime error?
    "requested object not available"

    Also activedocument.MailMerge.DataSource.DataFields.count giving 0 results
    DONT RUN EXECUTABLE FILES FROM UNKNOWN SOURCES.

  7. #7
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: VBA Word - GET MERGE FIELD text.

    Are you sure it's a mailmerge document?

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    272

    Re: VBA Word - GET MERGE FIELD text.

    Quote Originally Posted by dmaruca
    Are you sure it's a mailmerge document?
    Sorry I dont know weather it is a mail merge document or not, but it has

    20 mail merge ->merge fields.
    DONT RUN EXECUTABLE FILES FROM UNKNOWN SOURCES.

  9. #9
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: VBA Word - GET MERGE FIELD text.

    It could be possible that the data source isn't open. I tested that on one of my mail merge documents before I posted. It echoed "Vermont" in my immediate window.

    From: http://msdn.microsoft.com/en-us/libr...ffice.11).aspx

    Use the State property to determine the status of the mail merge operation.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    272

    Re: VBA Word - GET MERGE FIELD text.

    Quote Originally Posted by dmaruca
    It could be possible that the data source isn't open. I tested that on one of my mail merge documents before I posted. It echoed "Vermont" in my immediate window.

    From: http://msdn.microsoft.com/en-us/libr...ffice.11).aspx

    Use the State property to determine the status of the mail merge operation.
    I have attached the document and deleted some fields that are confidential. Please look into it?


    Please note the file saved in ms97 format.
    Last edited by cutepraba; Dec 9th, 2008 at 03:14 AM.
    DONT RUN EXECUTABLE FILES FROM UNKNOWN SOURCES.

  11. #11
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: VBA Word - GET MERGE FIELD text.

    Your document has mail merge fields, but isn't connected to a data source. You aren't going to be able to use the MailMerge object of the document unless you first connect to a data source. So right now I must ask you: what are you trying to accomplish with this?

    If you just want to extract the data that's already present in the document, use westconn1's method. If you have a data source, use mine or open an odbc connection to the data.

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VBA Word - GET MERGE FIELD text.

    to get the fields in header or footer you would need to work with the documents headers and footers collection, try like
    vb Code:
    1. For c = 1 To d.Sections(1).Headers(1).Range.Fields.Count
    2.     Debug.Print d.Sections(1).Headers(1).Range.Fields(c).Code
    3. Next
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    272

    Re: VBA Word - GET MERGE FIELD text.

    Quote Originally Posted by westconn1
    to get the fields in header or footer you would need to work with the documents headers and footers collection, try like
    vb Code:
    1. For c = 1 To d.Sections(1).Headers(1).Range.Fields.Count
    2.     Debug.Print d.Sections(1).Headers(1).Range.Fields(c).Code
    3. Next
    Thank you all. Its working perfectly. This is what I want.

    what the index number means?
    d.Sections(1)?
    DONT RUN EXECUTABLE FILES FROM UNKNOWN SOURCES.

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    272

    Re: [RESOLVED] VBA Word - GET MERGE FIELD text.

    do you have a solution to find field count in text box?
    DONT RUN EXECUTABLE FILES FROM UNKNOWN SOURCES.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    272

    Re: [RESOLVED] VBA Word - GET MERGE FIELD text.

    I found the solution

    Code:
    ActiveDocument.Shapes(1).TextFrame.TextRange.Fields.Count
    DONT RUN EXECUTABLE FILES FROM UNKNOWN SOURCES.

  16. #16
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] VBA Word - GET MERGE FIELD text.

    what the index number means?
    d.Sections(1)
    documents can be divided into multiple section, also there are 3 headers and footers to each section
    as far as i can tell a document can only have 1 set (of 3) headers and footers regardless of how many sections, so it always safe to use sections(1) to work with headers and footers, but you will need to ascertain if megefields can be in the other headers and footers
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  17. #17
    Fanatic Member dmaruca's Avatar
    Join Date
    May 2006
    Location
    Jacksonville, FL
    Posts
    577

    Re: [RESOLVED] VBA Word - GET MERGE FIELD text.

    westconn1:

    A doc can have a different header and footer for every section. The link with previous property has to be set to false. You would have to loop through every section and check this property.

  18. #18
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [RESOLVED] VBA Word - GET MERGE FIELD text.

    @ dmaruca
    thnx for that, didn't know about link with previous
    as i have never created a document with more than 1 section, i guess that is why i had not discovered that
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  19. #19
    Registered User
    Join Date
    Mar 2015
    Posts
    1

    Cool Re: VBA Word - GET MERGE FIELD text.

    Quote Originally Posted by dmaruca View Post
    Don't go at it through the field codes. Use the MailMerge object of the document.

    Code:
    activedocument.MailMerge.DataSource.DataFields("State").Value
    cutepraba, you great! Your code solved mine!

  20. #20
    New Member
    Join Date
    May 2022
    Posts
    1

    Re: [RESOLVED] VBA Word - GET MERGE FIELD text.

    If you don't want to use a MailMerge document and you want to check the FILLIN and MERGEFIELD here is a code that I have just created.

    code:
    Code:
    'In field you put the CStr(field.code)
    
    Function name(field As String) As String
    Dim field2() As String, field3() as String
    field2 = Split(field, " ")
    field3 = Split(field, Chr(34))
    If field2(0) = "FILLIN" Then
        name = field3(1)
    End If
    If field2(0) = "MERGEFIELD" Then
        name = field2(2)
    End If
    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width