从VB向Word写入多个表 [英] Writing multiple Tables to Word from VB

查看:143
本文介绍了从VB向Word写入多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

有人可以如何解决这个问题.我试图将多个表写入Word.我可以创建文档和应用程序并添加表,当我想将它们分开时会出现问题.我相信这与范围"有关.第一个表设置为范围(0,0),然后尝试重置该范围,或尝试插入一些空行并重置范围.我还尝试为每个表创建一个新范围,但是每次将新表插入第一个单元格时,都会得到相同的结果.
我做过一些严重的脑部损伤,阅读了有关范围的MS帮助,以及如何重置它们,以及上下移动选择-似乎无济于事.大概我有8张桌子,我想在8个单独的页面上放一张桌子.
感谢您提供的任何帮助


导入Microsoft.Office.Interop.Word
导入System.Runtime.InteropServices
公共垃圾作为新数据表
私有子WriteTables ()
将oApp视作应用程序
将oDoc视作文档
''在Word中启动新文档oApp = CType(CreateObject("Word.Application"),Application) oDoc = oApp.Documents.Add()
''清除所有现有信息.
''oDoc.Range.Delete()
''使当前文档成为ActiveDocument
oDoc.Activate()


Dim rng As Range = oDoc.Range(0 ,0)
rng.Font.Name ="Verdana"
rng.Font.Size = 16
Dim tlb如表= oDoc.Tables.Add(Range:= rng,NumRows:= junk.Rows .Count,NumColumns:=垃圾列数)
tlb.Range.Font.Name ="Arial" tlb.Range.Font.Size = 20

带有oApp.Selection
.SetRange(tlb.Range.End + 1,tlb.Range.End + 1)
.Collapse(WdCollapseDirection.wdCollapseEnd)
End With
''Dim i As Integer
''对于i = 1到5
''oApp.Selection.MoveDown()
''下一步


''rng = oApp.Selection.Range
''rng.SetRange(开始:= 0,结束:= 8)
''rng.SetRange(开始:= rng.End + 3,结束:= rng.End + 3)
Dim tlb1 as Table = oDoc.Tables.Add (范围:= rng,NumRows:= 6,NumColumns:= 6)
oApp.Visible = True

End Sub

hi all,

How someone can help with this question. I am trying to write a number of tables to Word. I can create the document and application and add a table, my problem arises when I want to seperate them. I believe this is something to do with the "range". The first table is set at range (0, 0), I then try to reset the range, or try to insert some blank lines and reset the range. I have also tried to create a new range for each table but I get the same result everytime which is the new table gets inserted into the first cell.
I have done some serious brain damage reading MS help on ranges and how to reset them and also moving selections up on and down- nothing seem to work. Utimately I have 8 tables and I want one table on 8 seperate pages.
thanks for any help you can give


Imports Microsoft.Office.Interop.Word
Imports System.Runtime.InteropServices
Public junk As New DataTable
Private Sub WriteTables()
Dim oApp As Application
Dim oDoc As Document
''Start a new document in Word
oApp = CType(CreateObject("Word.Application"), Application)
oDoc = oApp.Documents.Add()
'' Clear out any existing information.
'' oDoc.Range.Delete()
'' make the current documents the ActiveDocument
oDoc.Activate()


Dim rng As Range = oDoc.Range(0, 0)
rng.Font.Name = "Verdana"
rng.Font.Size = 16
Dim tlb As Table = oDoc.Tables.Add(Range:=rng, NumRows:= junk.Rows.Count, NumColumns:= junk.Columns.Count)
tlb.Range.Font.Name = "Arial"
tlb.Range.Font.Size = 20

With oApp.Selection
.SetRange(tlb.Range.End + 1, tlb.Range.End + 1)
.Collapse(WdCollapseDirection.wdCollapseEnd)
End With
'' Dim i As Integer
'' For i = 1 To 5
'' oApp.Selection.MoveDown()
'' Next


'' rng = oApp.Selection.Range
'' rng.SetRange(Start:=0, End:=8)
'' rng.SetRange(Start:=rng.End + 3, End:=rng.End + 3)
Dim tlb1 As Table = oDoc.Tables.Add(Range:=rng, NumRows:=6, NumColumns:=6)
oApp.Visible = True

End Sub

推荐答案

此页面 [ ^ ]显示了如何在Word文档的末尾.

还要尝试为每个新表添加一个新段落-此VBA代码使用两个表创建一个新文档:

This page[^] shows how to add a new table at the end of a word document.

And also try adding a new paragraph for each new table - this VBA code creates a new document with two tables:

Sub Test()<br />    Dim NewDoc As Document<br />    Dim NewTable As Table<br />    Set NewDoc = Application.Documents.Add(, , , True)<br />    <br />    Set MyRange = NewDoc.Content<br />    MyRange.Collapse Direction:=wdCollapseEnd<br />    Set NewTable = NewDoc.Tables.Add(Range:=MyRange, NumRows:=6, NumColumns:=10)<br />    NewTable.Borders.OutsideLineStyle = wdLineStyleTriple<br />    <br />    NewDoc.Paragraphs.Add<br />    Set MyRange = NewDoc.Content<br />    MyRange.Collapse Direction:=wdCollapseEnd<br />    Set NewTable = NewDoc.Tables.Add(Range:=MyRange, NumRows:=6, NumColumns:=4)<br />    NewTable.Borders.OutsideLineStyle = wdLineStyleTriple<br />End Sub






这篇关于从VB向Word写入多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆