问题需要:
(1)现有word模板,word模板上是固定的书信格式,已经有了文字,只有些空白需要添写
(2)点按钮,调用已有的word模版,弹出一个和模板一样的word新建文档,可以在新文档的空白处填写.但模版固定有的格式,文字不能改变. 满足上述问题有决方案吗?能给出相关的代码吗????谢谢各位高手啦
(1)现有word模板,word模板上是固定的书信格式,已经有了文字,只有些空白需要添写
(2)点按钮,调用已有的word模版,弹出一个和模板一样的word新建文档,可以在新文档的空白处填写.但模版固定有的格式,文字不能改变. 满足上述问题有决方案吗?能给出相关的代码吗????谢谢各位高手啦
' 输出一个表单的内容到一个WORD文档
Dim i As Integer, j As Integer, k As Integer, l As Integer, m As Integer
Dim Wa As Word.Application, TempStr As String
'定义WORD对象
Dim Wd As Word.Document
Dim Wp As Word.Paragraph
Dim Wt As Word.Table
Dim Wr As Word.Range
Set Wa = New Word.Application
'进度完成程度
ProgressBar1.Value = 15
'添加新文档
Set Wd = Wa.Documents.Add
ProgressBar1.Value = 20
'添加新段
Set Wp = Wd.Paragraphs.Add
Set Wr = Wp.Range '写入标题并黑体居中
Wr.Text = TitleStr
ProgressBar1.Value = 23
Wr.Bold = True
Wr.Style = wdStyleHeading1
Wr.ParagraphFormat.Alignment = wdAlignParagraphCenter
Wr.InsertParagraphAfter
'写入单位并黑体居右
'Set Wp = Wd.Paragraphs(Wd.Paragraphs.Count)
'Set Wr = Wp.Range
'Wr.Text = .lblUnit
'Wr.ParagraphFormat.Alignment = wdAlignParagraphRight
'Wr.InsertParagraphAfter
'ProgressBar1.Value = 27
'写入表单
Set Wp = Wd.Paragraphs(Wd.Paragraphs.Count)
Set Wr = Wp.Range
Wr.ParagraphFormat.Alignment = wdAlignParagraphCenter
ProgressBar1.Value = 30
Set Wt = Wd.Tables.Add(Range:=Wr, NumRows:=FG.Rows, NumColumns:=FG.Cols)
For i = 0 To FG.Rows - 1
For j = 0 To FG.Cols - 1
Set Wr = Wt.Cell(i + 1, j + 1).Range
If FG.TextMatrix(i, j) = "" Then
Wr.Text = ""
Else
Wr.Text = FG.TextMatrix(i, j)
End If
ProgressBar1.Value = 30 + 30 / ((FG.Rows) * (FG.Cols)) * (i * (FG.Cols) + j)
Next j
Next i
'处理合并格问题
For i = 1 To DeelRows
j = 1
TempStr = Wt.Rows(i).Cells(j).Range.Text
Do While True
If j + 1 <= Wt.Rows(i).Cells.Count Then
If TempStr = Wt.Rows(i).Cells(j + 1).Range.Text Then
Wt.Cell(Row:=i, Column:=j).Merge MergeTo:=Wt.Cell(Row:=i, Column:=j + 1)
Wt.Cell(Row:=i, Column:=j).Range.Text = TempStr
Else
j = j + 1
TempStr = Wt.Rows(i).Cells(j).Range.Text
End If
Else
Exit Do
End If
Loop
ProgressBar1.Value = 70 + 35 / DeelRows
Next i
l = 0
Dim a() As Integer
ReDim a(DeelRows)
For i = 1 To DeelRows
a(i) = Wt.Rows(i).Cells.Count
Next i
m = Wt.Rows(1).Cells.Count
TempStr = Wt.Cell(Row:=1, Column:=m).Range.Text
For i = 2 To DeelRows
If TempStr = Wt.Cell(Row:=i, Column:=a(i)).Range.Text Then
Wt.Cell(Row:=i, Column:=a(i)).Merge MergeTo:=Wt.Cell(Row:=1, Column:=a(1))
Wt.Cell(Row:=1, Column:=a(1)).Range.Text = TempStr
End If
ProgressBar1.Value = 95 + 5 / DeelRows
Next i
Wa.Visible = True
Wa.Activate
Unload Me
End Sub
http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/dv_wrcore/html/wrconwordobjectmodeloverview.asp