Mailmerge macro doesn't work in WORD 2007

J

j.nijkamp

Ive been using a macro to perform a mailmerge in wordxp. We're
planning to go to office 2007, so im testing that version at the
moment. However, the macro in word xp doesnt work for 2007, hope any1
can help me.

This is the macro

Sub SamenVoegen()

Dim fFieldText() As String
Dim iCount As Integer
Dim fField As FormField
Dim sWindowMain, sWindowMerge As String

With ActiveDocument
ChangeFileOpenDirectory "K:\Clienten"
ActiveDocument.SaveAs FileName:="u:\a.dot", FileFormat:=wdFormatDOT

On Error GoTo ErrHandler

sWindowMain = ActiveWindow.Caption
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect
End If

For Each afield In ActiveDocument.FormFields
If afield.Type = wdFieldFormTextInput Then
ReDim Preserve fFieldText(1, iCount + 1)
fFieldText(0, iCount) = afield.Result
fFieldText(1, iCount) = afield.Name
afield.Select
Selection.TypeText "<" & fFieldText(1, iCount) & "PlaceHolder>"
iCount = iCount + 1

End If

Next afield

ActiveDocument.MailMerge.Destination = wdSendToNewDocument
ActiveDocument.MailMerge.Execute

doFindReplace iCount, fField, fFieldText()

ActiveDocument.Protect Password:="", Noreset:=True, _
Type:=wdAllowOnlyFormFields

sWindowMerge = ActiveWindow.Caption

Windows(sWindowMain).Activate

doFindReplace iCount, fField, fFieldText()

ActiveDocument.Protect Password:="", Noreset:=True, _
Type:=wdAllowOnlyFormFields

Windows(sWindowMerge).Activate

ErrHandler:

End With
End Sub

Sub doFindReplace(iCount As Integer, fField As FormField, _
fFieldText() As String)

ActiveDocument.Close

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting

With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False


For i = 0 To iCount


Do While .Execute(FindText:="<" & fFieldText(1, i) _
& "PlaceHolder>") = True


Set fField = Selection.FormFields.Add _
(Range:=Selection.Range, Type:=wdFieldFormTextInput)


fField.Result = fFieldText(0, i)
fField.Name = fFieldText(1, i)
Loop


Selection.HomeKey Unit:=wdStory

Next
End With

With ActiveDocument
.Protect Type:=wdAllowOnlyFormFields, Noreset:=True, Password:=""

Dim sDatum As String
Dim sBetreft As String

With ActiveDocument
If ActiveDocument.Bookmarks.Exists("Datum") Then
sDatum = ActiveDocument.FormFields("Datum").Result
With Dialogs(wdDialogFileSummaryInfo)
.Execute
End With
End If
End With

With ActiveDocument
If ActiveDocument.Bookmarks.Exists("Betreft") Then
sBetreft = ActiveDocument.FormFields("Betreft").Result
With Dialogs(wdDialogFileSummaryInfo)
.Execute
End With
End If
End With

Dim ffItem As Word.FormField
Dim lngIndex As Long

With ActiveDocument
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect

For lngIndex = ActiveDocument.Content.FormFields.Count To 1 Step
-1

Set ffItem = ActiveDocument.Content.FormFields(lngIndex)
ffItem.Range.Text = ffItem.Result
Next
End If
End With

With ActiveDocument
.Protect Type:=wdAllowOnlyFormFields, Noreset:=True, Password:=""
End With

With ActiveDocument
ChangeFileOpenDirectory "K:\Clienten"
With Dialogs(wdDialogFileSaveAs)
.Name = Format(Date, "yymmdd") & " - " & sBetreft
.Show
End With
End With

ActiveDocument.Close

End With

ActiveDocument.Close

End Sub


Thanks in advance,
Jasper
 
D

Doug Robbins - Word MVP

When you say it "doesnt work" exactly what do you mean? Does it produce and
error message that you can debug?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

j.nijkamp

well actually i found out that the mailmergedatafields from our CRM
weren't there.

I made the template all over from scratch, but i cant get the safety
on now...:)

kinda sucks, cause i need the safety on to show the content of
themergefields from our crm and not the fieldcodes.

Any ideas?
 
D

Doug Robbins - Word MVP

What do you mean by "safety on"?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

j.nijkamp

What do you mean by "safety on"?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP









- Tekst uit oorspronkelijk bericht weergeven -

Protection on certain sections of the template
 
D

Doug Robbins - Word MVP

You do not need to protect the document to hide the display of mergefields.
Use:

If ActiveWindow.View.ShowFieldCodes = True Then
ActiveWindow.View.ShowFieldCodes = False
End If


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

j.nijkamp

You do not need to protect the document to hide the display of mergefields..
Use:

    If ActiveWindow.View.ShowFieldCodes = True Then
        ActiveWindow.View.ShowFieldCodes = False
    End If

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP







- Tekst uit oorspronkelijk bericht weergeven -

That command shows the MERGEFIELDS.
the problem is that i get my mergefields from a CRM application.
When i protect that section of the document, the mailmergedatafields
automaticly update.

For Example:
The clientname is Johnson.
In the template i have the datafield <<Clientname>>. when i load the
template without protection, ill get <<clientname>>. When i open with
protection, I'll get Johnson.
 
D

Doug Robbins - Word MVP

I haven't studied your macro that closely - it seems a bit convoluted to
me - but I gather that what you are trying to do is reinstate formfields
into the documents created by executing the merge.

If I had to create multiple documents from data in a data source that had to
contain formfields I would take a different approach.

Rather than using mail merge, I would create a template in which I had
docvariable fields in place of merge fields and then I would have code
iterate through the data source, creating a new document from that template
for each record and setting the values of document variables in that new
document to the data from the current record, and then the code would update
the fields in the document, all of which can be done while the document is
protected for forms, the template having been protected in that way, and
then save the document before going on to the next record.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

You do not need to protect the document to hide the display of
mergefields.
Use:

If ActiveWindow.View.ShowFieldCodes = True Then
ActiveWindow.View.ShowFieldCodes = False
End If

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP







- Tekst uit oorspronkelijk bericht weergeven -

That command shows the MERGEFIELDS.
the problem is that i get my mergefields from a CRM application.
When i protect that section of the document, the mailmergedatafields
automaticly update.

For Example:
The clientname is Johnson.
In the template i have the datafield <<Clientname>>. when i load the
template without protection, ill get <<clientname>>. When i open with
protection, I'll get Johnson.
 
J

j.nijkamp

I haven't studied your macro that closely - it seems a bit convoluted to
me - but I gather that what you are trying to do is reinstate formfields
into the documents created by executing the merge.

If I had to create multiple documents from data in a data source that had to
contain formfields I would take a different approach.

Rather than using mail merge, I would create a template in which I had
docvariable fields in place of merge fields and then I would have code
iterate through the data source, creating a new document from that template
for each record and setting the values of document variables in that new
document to the data from the current record, and then the code would update
the fields in the document, all of which can be done while the document is
protected for forms, the template having been protected in that way, and
then save the document before going on to the next record.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP








That command shows the MERGEFIELDS.
the problem is that i get my mergefields from a CRM application.
When i protect that section of the document, the mailmergedatafields
automaticly update.

For Example:
The clientname is  Johnson.
In the template i have the datafield <<Clientname>>. when i load the
template without protection, ill get <<clientname>>. When i open with
protection, I'll get Johnson.- Tekst uit oorspronkelijk bericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -

I'll try to explain the complete situation I'm in.

I click on a client in the CRM apllication, i select a word-template
to merge to.
Word opens and the datafields get filled.
The macro is just there to get rid of the connection between the CRM
application and the document.
Without the macro the connection with the system would stay, which
could result in the following problem.

I've made a letter for client A, called documentA.
I dont run the macro, ill just save the document.

The next thing i do, is create a new letter for client B, called
documentB.
The second i do this, documentA would get the clientinformation of
client B.

The macro works fine for office XP, but i doesnt in office 2007.

When i create a new template, i can protect it when its filled with
plain text and formfields.
The second I put in a datafield (from the crm application that is) i
cant protect the document, the protection-options turn grey.

Thats actually the issue i have.
Its a long read, dunno if everything is clear, my englisch aint that
good ;-)
 
D

Doug Robbins - Word MVP

I think you should fix the way in which the information is being transferred
into the document created from the template rather than attempting to fix a
macro that should not have been needed in the first place.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

I haven't studied your macro that closely - it seems a bit convoluted to
me - but I gather that what you are trying to do is reinstate formfields
into the documents created by executing the merge.

If I had to create multiple documents from data in a data source that had
to
contain formfields I would take a different approach.

Rather than using mail merge, I would create a template in which I had
docvariable fields in place of merge fields and then I would have code
iterate through the data source, creating a new document from that
template
for each record and setting the values of document variables in that new
document to the data from the current record, and then the code would
update
the fields in the document, all of which can be done while the document is
protected for forms, the template having been protected in that way, and
then save the document before going on to the next record.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP








That command shows the MERGEFIELDS.
the problem is that i get my mergefields from a CRM application.
When i protect that section of the document, the mailmergedatafields
automaticly update.

For Example:
The clientname is Johnson.
In the template i have the datafield <<Clientname>>. when i load the
template without protection, ill get <<clientname>>. When i open with
protection, I'll get Johnson.- Tekst uit oorspronkelijk bericht niet
weergeven -

- Tekst uit oorspronkelijk bericht weergeven -

I'll try to explain the complete situation I'm in.

I click on a client in the CRM apllication, i select a word-template
to merge to.
Word opens and the datafields get filled.
The macro is just there to get rid of the connection between the CRM
application and the document.
Without the macro the connection with the system would stay, which
could result in the following problem.

I've made a letter for client A, called documentA.
I dont run the macro, ill just save the document.

The next thing i do, is create a new letter for client B, called
documentB.
The second i do this, documentA would get the clientinformation of
client B.

The macro works fine for office XP, but i doesnt in office 2007.

When i create a new template, i can protect it when its filled with
plain text and formfields.
The second I put in a datafield (from the crm application that is) i
cant protect the document, the protection-options turn grey.

Thats actually the issue i have.
Its a long read, dunno if everything is clear, my englisch aint that
good ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top