Access交流中心

北京 | 上海 | 天津 | 重庆 | 广州 | 深圳 | 珠海 | 汕头 | 佛山 | 中山 | 东莞 | 南京 | 苏州 | 无锡 | 常州 | 南通 | 扬州 | 徐州 | 杭州 | 温州 | 宁波 | 台州 | 福州 | 厦门 | 泉州 | 龙岩 | 合肥 | 芜湖 | 成都 | 遂宁 | 长沙 | 株洲 | 湘潭 | 武汉 | 南昌 | 济南 | 青岛 | 烟台 | 潍坊 | 淄博 | 济宁 | 太原 | 郑州 | 石家庄 | 保定 | 唐山 | 西安 | 大连 | 沈阳 | 长春 | 昆明 | 兰州 | 哈尔滨 | 佳木斯 | 南宁 | 桂林 | 海口 | 贵阳 | 西宁 | 乌鲁木齐 | 包头 |

怎样把多个子窗体的数据插入到WORD文档中指定的行标题下面

随心飞去  发表于:2015-04-27 00:35:40  
复制

现请教各位一个问题
1、窗体上有2个子窗体,一个是A,一个是B
2、现在想把A和B子窗体中的数据经过筛选后,先把A子窗体的数据插入到WORD文档AB.DOC中指定的标题行
   A数据下面,然后再把B子窗体数据插入WORD文档AB.DOC中指定的标题行
   b数据下面。
怎样才能办到?要求在窗体中的按钮点击中实现.

 

Top
竹笛 发表于:2015-04-27 11:41:48
若是将数据写入到EXCEL模板就方便多了,EXCEL报表方式看能否符符合你的需求。

煮江品茶 发表于:2015-04-27 15:53:20
http://www.accessoft.com/article-show.asp?id=4353

随心飞去 发表于:2015-05-02 12:38:19

谢谢,2楼的是最佳答案,协助已完成了。但要加上了判断才不会出错,由于本人掌握得不多,只能借用GOTO来转行判断完成正确插入WORD相应的行标题下面数据,谁有更好的方法请给予放上来。

插入WORD文件格式

点击下载此附件

 

Dim doc As New Word.Application
Dim Tb As Word.Table
Dim i As Long
Dim ii As Long
Dim Ⅲ As Long
Dim V As Long

If Me.Text8 <> "45万吨汽油加氢处理装置" And Me.Text8 <> "临氢降凝处理装置" And Me.Text8 <> "加氢脱酸处理装置" And Me.Text8 <> "二套常减压蒸馏装置" Then

Cancel = False
MsgBox "请选择正确的值"
Exit Sub

Else

doc.Documents.Open FileName:=CurrentProject.Path & "\物资周报.doc"
doc.Visible = True

If Me.Text8 <> "45万吨汽油加氢处理装置" Then
Cancel = False
GoTo 100
 Else
 GoTo 50
50:
Set Tb = doc.ActiveDocument.Tables(1)

 GoTo 500
  End If

100:
If Me.Text8 <> "临氢降凝处理装置" Then

Cancel = False
GoTo 200
Else
 GoTo 150
150:
Set Tb = doc.ActiveDocument.Tables(2)
GoTo 500
 End If
200:
 If Me.Text8 <> "加氢脱酸处理装置" Then

Cancel = False
GoTo 300
Else
 GoTo 260
260:
Set Tb = doc.ActiveDocument.Tables(3)
GoTo 500
 End If
 
300:
 If Me.Text8 <> "二套常减压蒸馏装置" Then

Cancel = False
Exit Sub
Else
 GoTo 350
350:
Set Tb = doc.ActiveDocument.Tables(4)
GoTo 500
 End If

 



随心飞去 发表于:2015-05-02 12:56:07


500:
Tb.Cell(1, 1).Range = "工艺材料到货数据表"
Tb.Cell(2, 2).Range = "统计日期:"
Tb.Cell(2, 3).Range = Now()
Tb.Cell(3, 1).Range = "序号"
Tb.Cell(3, 2).Range = "名称"
Tb.Cell(3, 3).Range = "单位"
Tb.Cell(3, 4).Range = "计划数量"
Tb.Cell(3, 5).Range = "到货数量"
Tb.Cell(3, 6).Range = "缺口"
Tb.Cell(3, 7).Range = "到货百分比"

For i = 1 To Me.管配计划到货缺口表.Form.RecordsetClone.RecordCount
    If i > Tb.Rows.Count Then Exit Sub
    Me.管配计划到货缺口表.Form.SelTop = i
    Tb.Cell(i + 3, 1).Range = IIf(Me.管配计划到货缺口表.Controls("序号").Value <> "", Me.管配计划到货缺口表.Controls("序号").Value, "")
    Tb.Cell(i + 3, 2).Range = IIf(Me.管配计划到货缺口表.Controls("名称").Value <> "", Me.管配计划到货缺口表.Controls("名称").Value, "")
    Tb.Cell(i + 3, 3).Range = IIf(Me.管配计划到货缺口表.Controls("单位").Value <> "", Me.管配计划到货缺口表.Controls("单位").Value, "")
    Tb.Cell(i + 3, 4).Range = IIf(Me.管配计划到货缺口表.Controls("计划数量").Value <> "", Me.管配计划到货缺口表.Controls("计划数量").Value, "")
    Tb.Cell(i + 3, 5).Range = IIf(Me.管配计划到货缺口表.Controls("到货数量").Value <> "", Me.管配计划到货缺口表.Controls("到货数量").Value, "")
    Tb.Cell(i + 3, 6).Range = IIf(Me.管配计划到货缺口表.Controls("缺口").Value <> "", Me.管配计划到货缺口表.Controls("缺口").Value, "")
    Tb.Cell(i + 3, 7).Range = IIf(Me.管配计划到货缺口表.Controls("到货百分比").Value <> "", Round(Me.管配计划到货缺口表.Controls("到货百分比").Value, 4) * 100 & "%", "")
Next

Tb.Cell(16, 1).Range = "钢材材料到货数据表"
Tb.Cell(17, 2).Range = "统计日期:"
Tb.Cell(17, 3).Range = Now()

Tb.Cell(18, 3).Range = Now()
Tb.Cell(18, 1).Range = "序号"
Tb.Cell(18, 2).Range = "名称"
Tb.Cell(18, 3).Range = "单位"
Tb.Cell(18, 4).Range = "计划数量"
Tb.Cell(18, 5).Range = "到货数量"
Tb.Cell(18, 6).Range = "缺口"
Tb.Cell(18, 7).Range = "到货百分比"

For ii = 1 To Me.钢材计划到货缺口表.Form.RecordsetClone.RecordCount
    If ii > Tb.Rows.Count Then Exit Sub
    Me.钢材计划到货缺口表.Form.SelTop = ii
    Tb.Cell(ii + 18, 1).Range = IIf(Me.钢材计划到货缺口表.Controls("序号").Value <> "", Me.钢材计划到货缺口表.Controls("序号").Value, "")
    Tb.Cell(ii + 18, 2).Range = IIf(Me.钢材计划到货缺口表.Controls("名称").Value <> "", Me.钢材计划到货缺口表.Controls("名称").Value, "")
    Tb.Cell(ii + 18, 3).Range = IIf(Me.钢材计划到货缺口表.Controls("单位").Value <> "", Me.钢材计划到货缺口表.Controls("单位").Value, "")
   Tb.Cell(ii + 18, 4).Range = IIf(Me.钢材计划到货缺口表.Controls("计划数量").Value <> "", Me.钢材计划到货缺口表.Controls("计划数量").Value, "")
 Tb.Cell(ii + 18, 5).Range = IIf(Me.钢材计划到货缺口表.Controls("到货数量").Value <> "", Me.钢材计划到货缺口表.Controls("到货数量").Value, "")
    Tb.Cell(ii + 18, 6).Range = IIf(Me.钢材计划到货缺口表.Controls("缺口").Value <> "", Me.钢材计划到货缺口表.Controls("缺口").Value, "")
    Tb.Cell(ii + 18, 7).Range = IIf(Me.钢材计划到货缺口表.Controls("到货百分比").Value <> "", Round(Me.钢材计划到货缺口表.Controls("到货百分比").Value, 4) * 100 & "%", "")
Next



随心飞去 发表于:2015-05-02 12:56:47

Tb.Cell(37, 1).Range = ".仪表电气材料到货数据表"
Tb.Cell(38, 2).Range = "统计日期:"
Tb.Cell(38, 3).Range = Now()


Tb.Cell(39, 1).Range = "序号"
Tb.Cell(39, 2).Range = "名称"
Tb.Cell(39, 3).Range = "单位"
Tb.Cell(39, 4).Range = "计划数量"
Tb.Cell(39, 5).Range = "到货数量"
Tb.Cell(39, 6).Range = "缺口"
Tb.Cell(39, 7).Range = "到货百分比"


For Ⅲ = 1 To Me.仪表电气计划到货缺口表.Form.RecordsetClone.RecordCount
    If Ⅲ > Tb.Rows.Count Then Exit Sub
    Me.仪表电气计划到货缺口表.Form.SelTop = Ⅲ
    Tb.Cell(Ⅲ + 39, 1).Range = IIf(Me.仪表电气计划到货缺口表.Controls("序号").Value <> "", Me.仪表电气计划到货缺口表.Controls("序号").Value, "")
    Tb.Cell(Ⅲ + 39, 2).Range = IIf(Me.仪表电气计划到货缺口表.Controls("名称").Value <> "", Me.仪表电气计划到货缺口表.Controls("名称").Value, "")
    Tb.Cell(Ⅲ + 39, 3).Range = IIf(Me.仪表电气计划到货缺口表.Controls("单位").Value <> "", Me.仪表电气计划到货缺口表.Controls("单位").Value, "")
   Tb.Cell(Ⅲ + 39, 4).Range = IIf(Me.仪表电气计划到货缺口表.Controls("计划数量").Value <> "", Me.仪表电气计划到货缺口表.Controls("计划数量").Value, "")
 Tb.Cell(Ⅲ + 39, 5).Range = IIf(Me.仪表电气计划到货缺口表.Controls("到货数量").Value <> "", Me.仪表电气计划到货缺口表.Controls("到货数量").Value, "")
    Tb.Cell(Ⅲ + 39, 6).Range = IIf(Me.仪表电气计划到货缺口表.Controls("缺口").Value <> "", Me.仪表电气计划到货缺口表.Controls("缺口").Value, "")
    Tb.Cell(Ⅲ + 39, 7).Range = IIf(Me.仪表电气计划到货缺口表.Controls("到货百分比").Value <> "", Round(Me.仪表电气计划到货缺口表.Controls("到货百分比").Value, 4) * 100 & "%", "")
Next

Tb.Cell(80, 1).Range = "设备材料到货数据表"
Tb.Cell(81, 2).Range = "统计日期:"
Tb.Cell(81, 3).Range = Now()
Tb.Cell(82, 1).Range = "序号"
Tb.Cell(82, 2).Range = "名称"
Tb.Cell(82, 3).Range = "单位"
Tb.Cell(82, 4).Range = "计划数量"
Tb.Cell(82, 5).Range = "到货数量"
Tb.Cell(82, 6).Range = "缺口"
Tb.Cell(82, 7).Range = "到货百分比"

For V = 1 To Me.设备计划到货缺口表.Form.RecordsetClone.RecordCount
    If V > Tb.Rows.Count Then Exit Sub
    Me.设备计划到货缺口表.Form.SelTop = V
    Tb.Cell(V + 82, 1).Range = IIf(Me.设备计划到货缺口表.Controls("序号").Value <> "", Me.设备计划到货缺口表.Controls("序号").Value, "")
    Tb.Cell(V + 82, 2).Range = IIf(Me.设备计划到货缺口表.Controls("名称").Value <> "", Me.设备计划到货缺口表.Controls("名称").Value, "")
    Tb.Cell(V + 82, 3).Range = IIf(Me.设备计划到货缺口表.Controls("单位").Value <> "", Me.设备计划到货缺口表.Controls("单位").Value, "")
   Tb.Cell(V + 82, 4).Range = IIf(Me.设备计划到货缺口表.Controls("计划数量").Value <> "", Me.设备计划到货缺口表.Controls("计划数量").Value, "")
 Tb.Cell(V + 82, 5).Range = IIf(Me.设备计划到货缺口表.Controls("到货数量").Value <> "", Me.设备计划到货缺口表.Controls("到货数量").Value, "")
    Tb.Cell(V + 82, 6).Range = IIf(Me.设备计划到货缺口表.Controls("缺口").Value <> "", Me.设备计划到货缺口表.Controls("缺口").Value, "")
    Tb.Cell(V + 82, 7).Range = IIf(Me.设备计划到货缺口表.Controls("到货百分比").Value <> "", Round(Me.设备计划到货缺口表.Controls("到货百分比").Value, 4) * 100 & "%", "")
Next

doc.ActiveDocument.Save
Set doc = Nothing
Set Tb = Nothing

End If

End Sub



随心飞去 发表于:2015-05-02 13:17:23

为了直观的说明为什么这样做,现放上窗体图

 



总记录:6篇  页次:1/1 9 1 :