Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.mailmerge.fields
[email protected] j.nijkamp@kroesewevers.nl is offline
external usenet poster
 
Posts: 6
Default Mailmerge macro doesn't work in WORD 2007

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

  #2   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Doug Robbins - Word MVP Doug Robbins - Word MVP is offline
external usenet poster
 
Posts: 8,832
Default Mailmerge macro doesn't work in WORD 2007

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

wrote in message
...
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



  #3   Report Post  
Posted to microsoft.public.word.mailmerge.fields
[email protected] j.nijkamp@kroesewevers.nl is offline
external usenet poster
 
Posts: 6
Default Mailmerge macro doesn't work in WORD 2007

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?

  #4   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Doug Robbins - Word MVP Doug Robbins - Word MVP is offline
external usenet poster
 
Posts: 8,832
Default Mailmerge macro doesn't work in WORD 2007

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

wrote in message
...
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?



  #5   Report Post  
Posted to microsoft.public.word.mailmerge.fields
[email protected] j.nijkamp@kroesewevers.nl is offline
external usenet poster
 
Posts: 6
Default Mailmerge macro doesn't work in WORD 2007

On 30 mei, 00:36, "Doug Robbins - Word MVP"
wrote:
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

wrote in message

...



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?- Tekst uit oorspronkelijk bericht niet weergeven -


- Tekst uit oorspronkelijk bericht weergeven -


Protection on certain sections of the template


  #6   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Doug Robbins - Word MVP Doug Robbins - Word MVP is offline
external usenet poster
 
Posts: 8,832
Default Mailmerge macro doesn't work in WORD 2007

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

wrote in message
...
On 30 mei, 00:36, "Doug Robbins - Word MVP"
wrote:
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

wrote in message

...



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?- Tekst uit oorspronkelijk bericht niet weergeven -


- Tekst uit oorspronkelijk bericht weergeven -


Protection on certain sections of the template



  #7   Report Post  
Posted to microsoft.public.word.mailmerge.fields
[email protected] j.nijkamp@kroesewevers.nl is offline
external usenet poster
 
Posts: 6
Default Mailmerge macro doesn't work in WORD 2007

On 30 mei, 12:01, "Doug Robbins - Word MVP"
wrote:
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

wrote in message

...



On 30 mei, 00:36, "Doug Robbins - Word MVP"
wrote:
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


wrote in message


....


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?- Tekst uit oorspronkelijk bericht niet weergeven -


- Tekst uit oorspronkelijk bericht weergeven -


Protection on certain sections of the template- Tekst uit oorspronkelijk bericht niet weergeven -


- 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.
  #8   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Doug Robbins - Word MVP Doug Robbins - Word MVP is offline
external usenet poster
 
Posts: 8,832
Default Mailmerge macro doesn't work in WORD 2007

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

wrote in message
...
On 30 mei, 12:01, "Doug Robbins - Word MVP"
wrote:
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

wrote in message

...



On 30 mei, 00:36, "Doug Robbins - Word MVP"
wrote:
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


wrote in message


...


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?- Tekst uit oorspronkelijk bericht niet weergeven -


- Tekst uit oorspronkelijk bericht weergeven -


Protection on certain sections of the template- Tekst uit oorspronkelijk
bericht niet weergeven -


- 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.


  #9   Report Post  
Posted to microsoft.public.word.mailmerge.fields
[email protected] j.nijkamp@kroesewevers.nl is offline
external usenet poster
 
Posts: 6
Default Mailmerge macro doesn't work in WORD 2007

On 31 mei, 01:10, "Doug Robbins - Word MVP"
wrote:
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

wrote in message

...
On 30 mei, 12:01, "Doug Robbins - Word MVP"
wrote:





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


wrote in message


...


On 30 mei, 00:36, "Doug Robbins - Word MVP"
wrote:
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


wrote in message


....


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?- Tekst uit oorspronkelijk bericht niet weergeven -


- Tekst uit oorspronkelijk bericht weergeven -


Protection on certain sections of the template- Tekst uit oorspronkelijk
bericht niet weergeven -


- 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.- 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 ;-)





  #10   Report Post  
Posted to microsoft.public.word.mailmerge.fields
Doug Robbins - Word MVP Doug Robbins - Word MVP is offline
external usenet poster
 
Posts: 8,832
Default Mailmerge macro doesn't work in WORD 2007

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

wrote in message
...
On 31 mei, 01:10, "Doug Robbins - Word MVP"
wrote:
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

wrote in message

...
On 30 mei, 12:01, "Doug Robbins - Word MVP"
wrote:





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


wrote in message


...


On 30 mei, 00:36, "Doug Robbins - Word MVP"
wrote:
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


wrote in message


...


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?- Tekst uit oorspronkelijk bericht niet weergeven -


- Tekst uit oorspronkelijk bericht weergeven -


Protection on certain sections of the template- Tekst uit
oorspronkelijk
bericht niet weergeven -


- 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.- 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 ;-)






Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Mailmerge from Access 2007 to Word 2007 .docx document Russell Profitt[_3_] Mailmerge 0 March 20th 08 12:16 PM
Word 2007 and Outlook 2007 e-mail MailMerge does not complete Account Manager Mailmerge 1 July 2nd 07 09:13 AM
hyperlink does not work in email prepared in word for mailmerge bayside Mailmerge 1 March 28th 07 07:24 AM
Word mailmerge to PDF macro Martin Mailmerge 2 April 13th 06 06:31 AM
WORD mailmerge field switchs do not work George R Mailmerge 3 May 27th 05 05:19 PM


All times are GMT +1. The time now is 04:07 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"