View Single Post
 
Old 04-02-2013, 12:05 PM
iiiiifffff iiiiifffff is offline Windows XP Office 2010 32bit
Novice
 
Join Date: Apr 2012
Posts: 5
iiiiifffff is on a distinguished road
Default macro works on windows, not on mac [solved]

EDIT: This was not a macro issue, but an issue with the data source. Re-saving as a xslx file fixed the problem. Thanks for the help, though.

------------------

Hi. I have a document that I have been using for a number of years in word for windows (both 2003 and 2010). This document contains legacy form fields and I have been successfully using this macro to run a mail merge to create a new document with the form fields intact:

Code:
Sub PreserveMailMergeFormFieldsNewDoc()

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

On Error GoTo ErrHandler

' Store Main merge document window name.
sWindowMain = ActiveWindow.Caption

' Because the document contains form fields,
' it should be protected, so unprotect document.
If ActiveDocument.ProtectionType <> wdNoProtection Then
   ActiveDocument.Unprotect
End If

' Loop through all text form fields
' in the main mail merge document.
For Each aField In ActiveDocument.FormFields

   ' If the form field is a text form field...
   If aField.Type = wdFieldFormTextInput Then

      ' Redim array to hold contents of text field.
      ReDim Preserve fFieldText(2, iCount + 1)

      ' Place content and name of field into array.
      fFieldText(0, iCount) = aField.Result
      fFieldText(1, iCount) = aField.Name
      fFieldText(2, iCount) = aField.TextInput.Default

      ' Select the form field.
      aField.Select

      ' Replace it with placeholder text.
      Selection.TypeText "<" & fFieldText(1, iCount) & "PlaceHolder>"

      ' Increment icount
      iCount = iCount + 1

   End If

Next aField

' Perform mail merge to new document.
ActiveDocument.MailMerge.Destination = wdSendToNewDocument
ActiveDocument.MailMerge.Execute

' Find and Replace placeholders with form fields.
doFindReplace iCount, fField, fFieldText()

' Protect the merged document.
ActiveDocument.Protect Password:="xxxx", NoReset:=True, _
   Type:=wdAllowOnlyFormFields

' Set zoom to 100 on merged document
ActiveWindow.ActivePane.View.Zoom.Percentage = 100

' Get name of final merged document.
sWindowMerge = ActiveWindow.Caption

' Reactivate the main merge document.
Windows(sWindowMain).Activate

' Find and replace placeholders with form fields.
doFindReplace iCount, fField, fFieldText()

' Switch back to the merged document.
Windows(sWindowMerge).Activate

ErrHandler:

End Sub

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

' Go to top of document.
Selection.HomeKey Unit:=wdStory

' Initialize Find.
Selection.Find.ClearFormatting

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

   ' Loop form fields count.
    For i = 0 To iCount

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

         ' Replace the placeholder with the form field.
         Set fField = Selection.FormFields.Add _
            (Range:=Selection.Range, Type:=wdFieldFormTextInput)

         ' Restore form field contents and bookmark name.
         fField.Result = fFieldText(0, i)
         fField.Name = fFieldText(1, i)
         fField.TextInput.Default = fFieldText(2, i)
      Loop

      ' Go to top of document for next find.
      Selection.HomeKey Unit:=wdStory

   Next
End With

End Sub
At work, I have been moved to a Mac running Word 2011, and this does not seem to work any more. It looks like it runs the merge and then doesn't reinsert the fields into either of the documents. Also, the new document does not get locked. I think it is just giving up after it runs the merge.

Really, I am out of my league here, so any help would be appreciated. Please note that I did not write the original macro, but got it from doing some googling.

Thanks in advance for any help that anyone can give me.

Last edited by iiiiifffff; 04-03-2013 at 12:44 PM. Reason: Add note to solution...
Reply With Quote