-
Dec 8th, 2008, 03:26 AM
#1
Thread Starter
Hyperactive Member
-
Dec 8th, 2008, 07:07 AM
#2
Re: VBA Word - GET MERGE FIELD text.
this will work, but i was unable to return the field by name
vb Code:
For i = 1 To d.Fields.Count If d.Fields(i).Code = " MERGEFIELD NAME_2 " Then MsgBox d.Fields(i).Result: Exit For 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
-
Dec 8th, 2008, 07:36 AM
#3
Re: VBA Word - GET MERGE FIELD text.
Moved To Office Development
-
Dec 8th, 2008, 08:31 AM
#4
Fanatic Member
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
-
Dec 8th, 2008, 09:46 AM
#5
Thread Starter
Hyperactive Member
Re: VBA Word - GET MERGE FIELD text.
Originally Posted by westconn1
this will work, but i was unable to return the field by name
vb Code:
For i = 1 To d.Fields.Count
If d.Fields(i).Code = " MERGEFIELD NAME_2 " Then MsgBox d.Fields(i).Result: Exit For
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.
-
Dec 8th, 2008, 09:48 AM
#6
Thread Starter
Hyperactive Member
Re: VBA Word - GET MERGE FIELD text.
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.
-
Dec 8th, 2008, 11:03 AM
#7
Fanatic Member
Re: VBA Word - GET MERGE FIELD text.
Are you sure it's a mailmerge document?
-
Dec 8th, 2008, 11:25 AM
#8
Thread Starter
Hyperactive Member
Re: VBA Word - GET MERGE FIELD text.
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.
-
Dec 8th, 2008, 11:47 AM
#9
Fanatic Member
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.
-
Dec 8th, 2008, 12:04 PM
#10
Thread Starter
Hyperactive Member
Re: VBA Word - GET MERGE FIELD text.
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.
-
Dec 8th, 2008, 12:25 PM
#11
Fanatic Member
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.
-
Dec 8th, 2008, 03:38 PM
#12
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:
For c = 1 To d.Sections(1).Headers(1).Range.Fields.Count Debug.Print d.Sections(1).Headers(1).Range.Fields(c).Code 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
-
Dec 9th, 2008, 02:35 AM
#13
Thread Starter
Hyperactive Member
Re: VBA Word - GET MERGE FIELD text.
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:
For c = 1 To d.Sections(1).Headers(1).Range.Fields.Count
Debug.Print d.Sections(1).Headers(1).Range.Fields(c).Code
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.
-
Dec 9th, 2008, 03:15 AM
#14
Thread Starter
Hyperactive Member
-
Dec 9th, 2008, 03:21 AM
#15
Thread Starter
Hyperactive Member
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.
-
Dec 9th, 2008, 03:35 AM
#16
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
-
Dec 9th, 2008, 08:21 AM
#17
Fanatic Member
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.
-
Dec 9th, 2008, 03:36 PM
#18
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
-
Mar 11th, 2015, 09:11 AM
#19
Registered User
Re: VBA Word - GET MERGE FIELD text.
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
cutepraba, you great! Your code solved mine!
-
May 10th, 2022, 03:23 AM
#20
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|