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 <> "" Then
' WordDoc.Fields(i).Result.Text = NewResult
'End If
End If
Next
' lock the document to stop changes
'WordDoc.Protect wdAllowOnlyComments, , "jd837djh82"
'WordDoc.SaveAs App.Path & "\despatchnote.doc"
WordDoc.Close
WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing
MsgBox "Finished!"
Exit Sub
ErrHandler:
MsgBox "Unhanled Error: " & Err.Description
End Sub
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 <> "" Then
' WordDoc.Fields(i).Result.Text = NewResult
'End If
End If
Next
' lock the document to stop changes
'WordDoc.Protect wdAllowOnlyComments, , "jd837djh82"
'WordDoc.SaveAs App.Path & "\despatchnote.doc"
WordDoc.Close
WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing
MsgBox "Finished!"
Exit Sub
ErrHandler:
MsgBox "Unhanled Error: " & Err.Description
End Sub