M
mark
Advice required for an existing document macro that works in 2003 but not in
2007 (running compatibility mode) New document merges OK with data but ends
with (path/file access error). The new document is not renamed to
DAT_FILE_NAME ( P:\Documents\09\0901582\149415) & source document does not
close.
Module in macro below
Public Sub merge()
Dim sFileName As String
Dim iFreeFile As Integer
Const MERGE_FIELD_TEXT_SPACE As String = " {NONE}"
Const MERGE_FIELD_TEXT_NO_SPACE As String = "{NONE}"
Const EMPTY_STRING As String = ""
Const DAT_FILE_NAME As String = "x:\postname.dat"
Const SPACE_NONBREAKING_SPACE As String = "^s "
Const NONBREAKING_SPACE As String = "^s"
On Error GoTo ErrHandler
iFreeFile = FreeFile
Open DAT_FILE_NAME For Input As #iFreeFile
If EOF(iFreeFile) = False Then
Line Input #iFreeFile, sFileName
End If
Close #iFreeFile
If Len(Trim$(sFileName)) > 0 Then
ActiveDocument.MailMerge.Execute
Set myRange = ActiveDocument.Content
With myRange
.Find.Execute FindText:=MERGE_FIELD_TEXT_SPACE, _
ReplaceWith:=EMPTY_STRING, Replace:=wdReplaceAll
.Find.Execute FindText:=MERGE_FIELD_TEXT_NO_SPACE, _
ReplaceWith:=EMPTY_STRING, Replace:=wdReplaceAll
.Find.Execute FindText:=SPACE_NONBREAKING_SPACE, _
ReplaceWith:=EMPTY_STRING, Replace:=wdReplaceAll
.Find.Execute FindText:=NONBREAKING_SPACE, _
ReplaceWith:=EMPTY_STRING, Replace:=wdReplaceAll
End With
Open DAT_FILE_NAME For Output As #iFreeFile
Close #iFreeFile
ActiveDocument.SaveAs (sFileName)
Windows("FaceSheetforFile.doc").Activate
ActiveDocument.Close (wdDoNotSaveChanges)
End If
Success:
Exit Sub
ErrHandler:
MsgBox Err.Description
End Sub
2007 (running compatibility mode) New document merges OK with data but ends
with (path/file access error). The new document is not renamed to
DAT_FILE_NAME ( P:\Documents\09\0901582\149415) & source document does not
close.
Module in macro below
Public Sub merge()
Dim sFileName As String
Dim iFreeFile As Integer
Const MERGE_FIELD_TEXT_SPACE As String = " {NONE}"
Const MERGE_FIELD_TEXT_NO_SPACE As String = "{NONE}"
Const EMPTY_STRING As String = ""
Const DAT_FILE_NAME As String = "x:\postname.dat"
Const SPACE_NONBREAKING_SPACE As String = "^s "
Const NONBREAKING_SPACE As String = "^s"
On Error GoTo ErrHandler
iFreeFile = FreeFile
Open DAT_FILE_NAME For Input As #iFreeFile
If EOF(iFreeFile) = False Then
Line Input #iFreeFile, sFileName
End If
Close #iFreeFile
If Len(Trim$(sFileName)) > 0 Then
ActiveDocument.MailMerge.Execute
Set myRange = ActiveDocument.Content
With myRange
.Find.Execute FindText:=MERGE_FIELD_TEXT_SPACE, _
ReplaceWith:=EMPTY_STRING, Replace:=wdReplaceAll
.Find.Execute FindText:=MERGE_FIELD_TEXT_NO_SPACE, _
ReplaceWith:=EMPTY_STRING, Replace:=wdReplaceAll
.Find.Execute FindText:=SPACE_NONBREAKING_SPACE, _
ReplaceWith:=EMPTY_STRING, Replace:=wdReplaceAll
.Find.Execute FindText:=NONBREAKING_SPACE, _
ReplaceWith:=EMPTY_STRING, Replace:=wdReplaceAll
End With
Open DAT_FILE_NAME For Output As #iFreeFile
Close #iFreeFile
ActiveDocument.SaveAs (sFileName)
Windows("FaceSheetforFile.doc").Activate
ActiveDocument.Close (wdDoNotSaveChanges)
End If
Success:
Exit Sub
ErrHandler:
MsgBox Err.Description
End Sub