用户登录
用户注册

分享至

vb读word

  • 作者: 你在弄啥咧
  • 来源: 51数据库
  • 2020-04-21

1.求助:VB怎么读取Word中的内容显示出来

假设 D 盘下有一名为 1.docx 的 Word 文档,其内容如下:

则通过以下代码即可读取到该文档中的内容:

Public Sub GetWordContent()

Dim objWordDoc As Object

Set objWordDoc = GetObject("D:\1.docx")

MsgBox objWordDoc.Content.Text, vbInformation, "Word"

Set objWordDoc = Nothing

End Sub

运行效果:

2.怎么让vb用word的运行库读word文件,详细的代码

用WORD运行库读就行了一个form,button,students.mdb 从数据库中读取数据并输入到word中,代码如下,但是运行的时候在声明变量的时候出错,由于是日文的os所以不清楚保错的内容,请大家帮忙!! -------------------------------------------------------------------- Private Sub cmdword_Click() Dim sqlstr As String Dim rpTitle As String Set db = OpenDatabase("students.mdb") sqlstr = "select * from Info" rpTitle = "通讯录" Call GenerateReport(sqlstr, rpTitle) End Sub --------------------------------------------------------------- Private Sub GenerateReport(sqlstr As String, rpTitle As String) Dim app As New Word.Application Dim doc As Word.Document Dim sel As Word.Selection Dim tbl As Word.Table Dim RD As Recordset Dim i, j As Integer app.Visible = True app.Documents.Add docname = app.ActiveDocument.Name Set doc = app.Documents(docname) Set courseinfo = db.OpenRecordset(sqlstr, dbOpenSnapshot, ReadOnly) Set sel = app.Selection With sel .Font.Size = 30 .Font.Bold = True .ParagraphFormat.Alignment = wdAlignParagraphCenter .InsertAfter rpTitle .InsertParagraphAfter .InsertParagraphAfter .EndOf End With sel.Font.Size = 10 If courseinfo.RecordCount > 0 Then courseinfo.MoveLast sel.MoveEnd Set tbl = sel.Tables.Add(sel.Range, courseinfo.RecordCount + 1, courseinfo.Fields.Count) tbl.AutoFormat (36) tbl.AllowAutoFit = True tbl.Columns.AutoFit With tbl courseinfo.MoveFirst For j = 1 To .Columns.Count .Cell(1, j).Range.Font.Bold = True .Cell(1, j).Range.Text = courseinfo.Fields(j - 1).Name Next j For i = 2 To .Rows.Count For j = 1 To .Columns.Count .Cell(i, j).Range.Text = courseinfo.Fields(j - 1).Value Next j courseinfo.MoveNext Next i End With Else MsgBox "no record!!!", vbOKOnly End If sel.GoToNext (wdGoToTable) sel.Document.Range.InsertParagraphAfter sel.Document.Range.InsertAfter Date courseinfo.Close End Sub。

3.VB怎样读text文件并输出结果到word文档中

dim xx(1000) as string '假设最多1000行

open "c;\1.txt" for input as #1

i=1

while not eof(1)

input #1, xx(i)

xx(i)=mid(xx(i),2) '把开始的=号去掉

i=i+1

wend

close #1

dim aa as new word.application

aa.ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=i, NumColumns:= _

2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _

wdAutoFitFixed

for j= 1 to i-1

Selection.TypeText Text:="xx"

Selection.MoveRight Unit:=wdCharacter, Count:=1

Selection.TypeText Text:=xx(i)

Selection.MoveDown Unit:=wdLine, Count:=1

Selection.MoveLeft Unit:=wdCell, Count:=1

next

aa.visibel=true

4.VB怎么读取Word.doc文档

Private Sub Command1_Click()

Dim WDAPP As Word.Application

Set WDAPP = CreateObject("Word.Application")

WDAPP.Documents.Open ("D:\vc6.0\反汇编\第1节 准备工作.doc")

WDAPP.Visible = False

RichTextBox1.Text = " " & Replace(WDAPP.Documents(1).Content.Text, vbCrLf, vbCrLf & " ")

WDAPP.Quit

Set WDAPP = Nothing

End Sub

5.vb 调用Word打开文档

引用CommonDialog和Microsoft Word 11.0 object Libary(其他版本也行,看机器情况)

Private Sub Command1_Click()

Dim DocPath As String

Dim wApp As New Word.Application

CommonDialog1.ShowOpen

DocPath = CommonDialog1.FileName

If DocPath = "" Then wApp.Documents.Close: Set wApp = Nothing

wApp.Documents.Open DocPath

wApp.Visible = True

End Sub

Private Sub Form_Load()

CommonDialog1.Filter = "Word文档 (*.dco)|*.doc"

CommonDialog1.DialogTitle = "选择要打开的文档"

CommonDialog1.CancelError = False

End Sub

6.vb读取word文件显示在RichTextBox中

首先你要理解的是在word也是定格写的。之所以你看起来首行空两个格,是整个文档设置啦首行缩进属性 为两个格。所以这两个格要你自己添加

可以这样改写代码

RichTextBox1.Text = WDAPP.Documents(1).Content.Text

RichTextBox1.Text = " " & replace(WDAPP.Documents(1).Content.Text,vbcrlf,vbcrlf & " ")

'就是把每行结尾的回车换行 后面加两个格,同时第一行加两个空格

转载请注明出处51数据库 » vb读word

软件
前端设计
程序设计
Java相关