Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pleasehelp me: copy document fields from on edoc to another?

Status
Not open for further replies.

998199

Programmer
Oct 22, 2001
2
US
I have a project, copy one doc(template like a.doc) to another doc ( give a new name like a_new.doc), and replace some variable, here is some code, please help me !


here is the code
' 1.I got the filename from dlgCMD1 object
' 2.also i want to insert some page break, how to do it

Private Sub FillTemplates()

Dim WordApp, WordAppDest As Word.Application
Dim WordDoc, WordDocDest As Word.Document
Dim i As Integer, j As Integer
Dim NewResult, TempStr As String


On Error GoTo ErrHandler

ReDim UsedVariables(0)

Set WordApp = CreateObject("Word.Application")
Set WordDoc = WordApp.Documents.Open(dlgCMD1.FileName)

TempStr = dlgCMD1.FileName
i = InStrRev(TempStr, ".")
If i > 1 Then
TempStr = Left(TempStr, i - 1) & "DEST." & Right(TempStr, Len(TempStr) - i)
Else
' filename is empty , i should quit here
End If


WordDoc.SaveAs TempStr


Set WordDocDest = WordApp.Documents.Add(TempStr)



For i = 1 To WordDoc.Fields.Count

'3.*******************error *******************
WordDocDest.Fields.Add WordDoc.Fields(i)
'3.please tell me how to copy fields from WordDoc to WordDocDest

' *******************error here*************

If WordDoc.Fields(i).Type = wdFieldDocVariable Then

' Get the text for the field from the user
'NewResult = GetNewResult(WordDoc.Fields(i), WordDoc)
'Insert New Text into the field
'If NewResult <> &quot;&quot; Then
' WordDoc.Fields(i).Result.Text = NewResult
'End If

End If

Next

' lock the document to stop changes
'WordDoc.Protect wdAllowOnlyComments, , &quot;jd837djh82&quot;
'WordDoc.SaveAs App.Path & &quot;\despatchnote.doc&quot;

WordDoc.Close
WordApp.Quit

Set WordDoc = Nothing
Set WordApp = Nothing
MsgBox &quot;Finished!&quot;

Exit Sub
ErrHandler:

MsgBox &quot;Unhanled Error: &quot; & Err.Description

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top