vb6.0 数据库中access表中的题目以word输出出现了问题
access中有表选择题信息表,是用来保存选择题的,这样的话我想让选择题以这样的形式输出:本来设想让num=表中的记录数fori=1tonumDoWhileNotAdod...
access中有表 选择题信息表,是用来保存选择题的,这样的话我想让选择题以这样的形式输出:
本来设想
让num=表中的记录数for i=1 to num
Do While Not Adodc3.Recordset.EOF Set wordparagraph = worddoc.Paragraphs.Add worddoc.Paragraphs.Item(i).Range.Font.Name = "宋体" worddoc.Paragraphs.Item(i).Range.Font.Size = "五号" worddoc.Paragraphs.Item(i).Range.Text = "sql * from 选择题信息表 where 题目" ***这块貌似不太对。
下面我要是想输出A选项,就又得 Set wordparagraph = worddoc.Paragraphs.Add
但是这时候,我想设置A选项时 这个i 。。就加一了。。。不对
自己不知道该怎么弄,求助!
选择题信息表 中的字段
对了,我生成完word后,还想要把 选择题信息表 清空 ,就是把数据都清没,该怎么写代码 展开
本来设想
让num=表中的记录数for i=1 to num
Do While Not Adodc3.Recordset.EOF Set wordparagraph = worddoc.Paragraphs.Add worddoc.Paragraphs.Item(i).Range.Font.Name = "宋体" worddoc.Paragraphs.Item(i).Range.Font.Size = "五号" worddoc.Paragraphs.Item(i).Range.Text = "sql * from 选择题信息表 where 题目" ***这块貌似不太对。
下面我要是想输出A选项,就又得 Set wordparagraph = worddoc.Paragraphs.Add
但是这时候,我想设置A选项时 这个i 。。就加一了。。。不对
自己不知道该怎么弄,求助!
选择题信息表 中的字段
对了,我生成完word后,还想要把 选择题信息表 清空 ,就是把数据都清没,该怎么写代码 展开
1个回答
展开全部
你如果整篇word字体都是一样的,则没有必要把word操作部分放入循环体内。
而应该通过循环获取一个字符串,当然换行格式都是在这个循环中完成的。
循环之前访问数据库。然后对访问数据库得到的数据集进行遍历循环。
我以字符串连接数据库创建Recordset数据集为例:
工程引用:(你要自己引用你的office word)
micrsoft activeX data objects 2.8 library
--------------
'声明数据操作变量
Private conn As New ADODB.Connection
Private rs As New ADODB.Recordset
Private WordApp As Object
'程序加载时候创建你的数据库连接
Private Sub Form_Load()
If TypeName(WordApp) = "Nothing" Then
Set WordApp = CreateObject("Word.Application")
End If
Dim Str
Str = App.Path
If Right(Str, 1) <> "\" Then
Str = Str + "\"
End If
Str = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & Str & "你的数据库名.mdb"
conn.Open Str
End Sub
'自定义对象销毁过程
Private Sub myQuit(myobject As Object)
If TypeName(myobject) <> "Nothing" Then
Set myboject = Nothing
End If
End Sub
'程序退出时销毁创建的对象
Private Sub Form_Unload(Cancel As Integer)
If TypeName(WordApp) <> "Nothing" Then
WordApp.Quit
Set WordApp = Nothing
End If
Call myQuit(rs)
Call myQuit(conn)
End Sub
'你生成word的过程
//////////////////////
‘1、打开数据库,创建数据集,这里假设全部数据。如果 有选择条件,请加入where比较。
Dim sqlstr as string
sqlstr = "select * from 你的数据表名称 "
rs.Open sqlstr, conn, adOpenDynamic, adLockPessimistic
If Not rs.EOF And Not rs.BOF Then
dim mystr as string
mystr = ""
do while Not rs.EOF And Not rs.BOF
mystr = mystr & rs("你的数据列名") '多道项目选择如果有换行请根据数据表内容修改
rs.movenext
loop
end if
rs.close
'2、创建word
Dim myDoc As Object
'注释代码演示你可以在之前按特定规律连接字符串,此时使用数组调整格式
' Dim a
' a = Split(mystr, vbCrLf)
' ss = a(0)
WordApp.Application.Visible = 1
Set myDoc = WordApp.Documents.Add()
With myDoc
.Content.Font.Name = "黑体"
.SaveAs FileName:=App.Path & "\☆" & ss & "☆.doc"
End With
Selection.TypeParagraph
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
'Selection.Font.Bold = wdToggle
Selection.Font.Size = 24
Selection.TypeText Text:=mystr
'数组调整word格式演示,这里可以考虑对数组循环
' Selection.Font.Size = 36
' Selection.TypeText Text:=a(0) & vbCrLf
' Selection.Font.Size = 18
' Selection.TypeText Text:="——" & a(1) & vbCrLf & vbCrLf
'退出
WordApp.ActiveDocument.Close True
Call myQuit(myDoc)
////////////////////////////
-----------------
word格式设置示范:
' Selection.TypeParagraph
' Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
' Selection.ParagraphFormat.SpaceBefore = 10 '段前
' Selection.ParagraphFormat.SpaceAfter = 20 '段后
' Selection.ParagraphFormat.LineSpacingRule = wdLineSpaceExactly '固定值
' Selection.ParagraphFormat.LineSpacing = 36 '18磅
' Selection.ParagraphFormat.FirstLineIndent = Word.MillimetersToPoints(18) '首行缩进0.75厘米
' 'Selection.Font.Name = "宋体"
' Selection.Font.Size = 24
最后,要说明的是由于office版本问题,如果你的程序需要移植,则你本机的office引用可能失效,解决方案是采用打开模板word的方式创建一个word对象,参照我的另一篇文章,vb操作excel:http://hi.baidu.com/archlen/item/0156a214071756f89c778acc
我给出的代码都是现成调试运行成功的源代码截取而来,你只要自己稍加改动,一定可以成功的。
而应该通过循环获取一个字符串,当然换行格式都是在这个循环中完成的。
循环之前访问数据库。然后对访问数据库得到的数据集进行遍历循环。
我以字符串连接数据库创建Recordset数据集为例:
工程引用:(你要自己引用你的office word)
micrsoft activeX data objects 2.8 library
--------------
'声明数据操作变量
Private conn As New ADODB.Connection
Private rs As New ADODB.Recordset
Private WordApp As Object
'程序加载时候创建你的数据库连接
Private Sub Form_Load()
If TypeName(WordApp) = "Nothing" Then
Set WordApp = CreateObject("Word.Application")
End If
Dim Str
Str = App.Path
If Right(Str, 1) <> "\" Then
Str = Str + "\"
End If
Str = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & Str & "你的数据库名.mdb"
conn.Open Str
End Sub
'自定义对象销毁过程
Private Sub myQuit(myobject As Object)
If TypeName(myobject) <> "Nothing" Then
Set myboject = Nothing
End If
End Sub
'程序退出时销毁创建的对象
Private Sub Form_Unload(Cancel As Integer)
If TypeName(WordApp) <> "Nothing" Then
WordApp.Quit
Set WordApp = Nothing
End If
Call myQuit(rs)
Call myQuit(conn)
End Sub
'你生成word的过程
//////////////////////
‘1、打开数据库,创建数据集,这里假设全部数据。如果 有选择条件,请加入where比较。
Dim sqlstr as string
sqlstr = "select * from 你的数据表名称 "
rs.Open sqlstr, conn, adOpenDynamic, adLockPessimistic
If Not rs.EOF And Not rs.BOF Then
dim mystr as string
mystr = ""
do while Not rs.EOF And Not rs.BOF
mystr = mystr & rs("你的数据列名") '多道项目选择如果有换行请根据数据表内容修改
rs.movenext
loop
end if
rs.close
'2、创建word
Dim myDoc As Object
'注释代码演示你可以在之前按特定规律连接字符串,此时使用数组调整格式
' Dim a
' a = Split(mystr, vbCrLf)
' ss = a(0)
WordApp.Application.Visible = 1
Set myDoc = WordApp.Documents.Add()
With myDoc
.Content.Font.Name = "黑体"
.SaveAs FileName:=App.Path & "\☆" & ss & "☆.doc"
End With
Selection.TypeParagraph
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
'Selection.Font.Bold = wdToggle
Selection.Font.Size = 24
Selection.TypeText Text:=mystr
'数组调整word格式演示,这里可以考虑对数组循环
' Selection.Font.Size = 36
' Selection.TypeText Text:=a(0) & vbCrLf
' Selection.Font.Size = 18
' Selection.TypeText Text:="——" & a(1) & vbCrLf & vbCrLf
'退出
WordApp.ActiveDocument.Close True
Call myQuit(myDoc)
////////////////////////////
-----------------
word格式设置示范:
' Selection.TypeParagraph
' Selection.ParagraphFormat.Alignment = wdAlignParagraphJustify
' Selection.ParagraphFormat.SpaceBefore = 10 '段前
' Selection.ParagraphFormat.SpaceAfter = 20 '段后
' Selection.ParagraphFormat.LineSpacingRule = wdLineSpaceExactly '固定值
' Selection.ParagraphFormat.LineSpacing = 36 '18磅
' Selection.ParagraphFormat.FirstLineIndent = Word.MillimetersToPoints(18) '首行缩进0.75厘米
' 'Selection.Font.Name = "宋体"
' Selection.Font.Size = 24
最后,要说明的是由于office版本问题,如果你的程序需要移植,则你本机的office引用可能失效,解决方案是采用打开模板word的方式创建一个word对象,参照我的另一篇文章,vb操作excel:http://hi.baidu.com/archlen/item/0156a214071756f89c778acc
我给出的代码都是现成调试运行成功的源代码截取而来,你只要自己稍加改动,一定可以成功的。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询