I didn't test my code with an actual XML file, but with just a text file that I threw together with some bracketed nodes, because I didn't expect a *.prtl file to have the line
<?xml version="1.0" encoding="utf-8"?>
at the top. When you use Word's Open command to open any file that starts with that line, Word automatically strips out all the XML coding. (This is a result of a patent lawsuit that Microsoft lost, http://www.zdnet.com/blog/microsoft/...soft-word/3712.)
It took me a few false starts to figure out how to get the coding to stay. Using the Range object's InsertFile method didn't do it. I wound up using what are perhaps the oldest functions in VBA, the Open #, Line Input #, and Close # functions, in the subroutine you'll find at the end of this version. The subroutine inserts the text line by line into a new blank document. And I've tested this one with a real XML file renamed as a .prtl file...
Option Explicit
Sub EditVideoFile()
Dim dataFile As Document ' this data doc or template
Dim baseFile As Document ' file being modified
Dim baseFileFN As String ' path\name of file from Adobe
Dim rgData As Range ' range of data file containing replacements
Dim para As Paragraph ' paragraph in rgData
Dim rgPattern As Range ' a range used in baseFile
Dim replaceText As String ' current line from this data file
Dim iterNum As Long ' number of current line / saved file
Dim saveFN As String ' path\base name of saved file
Set dataFile = ActiveDocument
MsgBox "Select the data file (*.prtl) to be processed."
With Dialogs(wdDialogFileOpen)
.Name = "*.prtl"
If .Display = -1 Then ' just get the path\name, don't actually open
baseFileFN = WordBasic.FileNameInfo(.Name, 1) ' full path\name
Else
Exit Sub
End If
End With
MsgBox "Select the folder and filename (without numbers) to save as."
With Dialogs(wdDialogFileSaveAs)
If .Display = -1 Then ' just get the path\name, don't actually save
saveFN = WordBasic.FileNameInfo(.Name, 5) & WordBasic.FileNameInfo(.Name, 4) & "-"
'MsgBox saveFN
Else
Exit Sub
End If
End With
'On Error GoTo Errhdl
iterNum = 1
Set rgData = dataFile.Range
rgData.MoveStart unit:=wdParagraph, Count:=1 ' exclude paragraph containing macro button
For Each para In rgData.Paragraphs
replaceText = Left(para.Range.Text, Len(para.Range.Text) - 1) ' exclude paragraph mark
Set baseFile = Documents.Add(Visible:=False)
InsertAsNotXML FN:=baseFileFN, baseFile:=baseFile
' force all-caps XYZ to lower-case xyz
' to avoid inserting text from data file in all caps
Set rgPattern = baseFile.Range
With rgPattern.Find
.Text = "XYZ"
.Wrap = wdFindStop
While .Execute
rgPattern.Text = LCase(rgPattern.Text)
Wend
End With
' do the replacement
baseFile.Range.Find.Execute _
FindText:="xyz", _
MatchCase:=False, _
ReplaceWith:=replaceText, _
Replace:=wdReplaceAll
baseFile.SaveAs _
FileName:=saveFN & Format(iterNum, "0000") & ".prtl", _
FileFormat:=wdFormatText, _
AddToRecentFiles:=False
' close the modified file without saving
baseFile.Close SaveChanges:=wdDoNotSaveChanges
iterNum = iterNum + 1
Next para
MsgBox "Done"
Exit Sub
Errhdl:
MsgBox "Error " & Err.Number & " at line " & iterNum & vbCr & Err.Description
End Sub
Sub InsertAsNotXML(FN As String, baseFile As Document)
Dim aLine As String
Dim pos As Long
Open FN For Input As #1
Do While Not EOF(1)
Line Input #1, aLine
' There may be other characters at the start of the line,
' so remove them.
pos = InStr(LCase(aLine), "<?xml")
If pos > 0 Then
aLine = Right(aLine, Len(aLine) - pos + 1)
End If
baseFile.Range.InsertAfter aLine & vbCr
Loop
Close #1
End Sub