Install the macros in your normal template - see
http://www.gmayor.com/installing_macro.htm (don't forget to change the path
in the line
Open "D:\My Documents\Test\Versions\filename.txt" For Input As 1
to match the path to your ascii file.)
Create a pair of toolbar buttons to run the macros as shown in that web
page.
With the document open that you wish to include the data from your ascii
file, first run the TransferTextRowsToVariables macro which creates the
variables in your document from the ascii file Next locate the positions
that you wish to insert the data in your document. Put the cursor at each
position in turn and run the macro InsertLineVariable (repeated below). This
inserts the next available numbered variable (and consequently the matching
text from your ascii file) each time the macro is run. ie { DocVariable
Line1 } { DocVariable Line2 }{ DocVariable Line3 } until all the variables
are used up. The following version of the InsertLineVariable macro has a
couple of extra lines to update the field as it is inserted and ensure the
data is displayed rather than the field construction. A limitation of the
macro as it stands is that you can only insert one field for each variable.
Sub InsertLineVariable()
Dim oVars As Variables
Dim vVar As Variant
Dim aNum As Integer
Dim bNum As Integer
Dim bExists As Boolean
Dim varExists As Boolean
Set oVars = ActiveDocument.Variables
bExists = False
varExists = False
For Each vVar In oVars
If vVar.name = "numVars" Then
varExists = True
Exit For
End If
Next vVar
For Each vVar In oVars
If vVar.name = "varCount" Then
bExists = True
Exit For
End If
Next vVar
If varExists = False Then
MsgBox "There are no variables in the document" & _
" associated with this macro", vbCritical, "Error"
Exit Sub
End If
If bExists = False Then
oVars("varCount").Value = 0
End If
oVars("varCount").Value = oVars("varCount").Value + 1
aNum = oVars("varCount").Value
bNum = oVars("numVars").Value
If aNum > bNum Then
MsgBox "There are no more variables to insert", _
vbInformation, "Insert Variables"
Exit Sub
End If
Selection.Fields.Add Selection.Range, _
wdFieldDocVariable, "line" & _
oVars("varCount").Value, False
Selection.Fields.Update
ActiveWindow.View.ShowFieldCodes = False
End Sub
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>