1.VB如何读入Word的内容
Set oApp = CreateObject("Word。
Application") oApp。Visible = False Set oDoc = oApp。
Documents。Open(strFile)'strFile为文件路径 '获得最后一段的内容 Dim length As Long '整个文档的长度 Dim i As Long length = oDoc。
Content。StoryLength Content = oDoc。
Content。Text Dim s As String For i = 1 To length If Mid$(Content, i, 1) = Chr$(13) Then s = s + vbCrLf Else s = s + Mid$(Content, i, 1) End If Next i oDoc。
Close True oApp。Quit False txtMx = s。
2.如何在VB中建立word文档,并写入文字和图片
首先说明,你这样是写TXT,不是DOC,虽然Word可以打开,但根本就不是Word文件。当然不能存储图片了。请参考我以下的代码。建一个Command1。
================
Dim ap As Word.Application, doc As Document
Private Sub Form_Load() '这个过程不必做修改,是建立一个新的Word文档
Set ap = CreateObject("word.application")
ap.Visible = True
Set doc = ap.Documents.Add
End Sub
Private Sub Command1_Click()
doc.Content.InsertAfter Text:="请输入你要输入的内容" '插入文字,请使用这种格式
doc.Shapes.AddPicture FileName:="D:\a.jpg" '插入图片,请使用这种格式,路径自己写
End Sub
其他问题再找我。
3.vb如何用宏写word
宏操作是在打开word文档以后录制的,也就是说运行宏也要在打开word文档后进行,所以要把宏复制到vb环境中去运行的话,就得先在vb中操作打开word文档,参考下面代码:(演示前请先c;下建一个文件名为test.doc的word空文档,操作完成后,你会发现C盘里有个aa.doc文件,你打开就会发现宏操作写的一则通知)Private Sub Command1_Click()Dim MyWord As Word.ApplicationDim MyWordBook As Word.DocumentSet MyWord = New Word.ApplicationSet MyWordBook = MyWord.Documents.Add("c:\test.doc") '打开test.doc用户自定义的空白WORD文档MyWordBook.Activate'3.放置宏代码With MyWordBook'此处就可以插入宏代码了' Macro1 Macro' 宏在 2009-9-20 由 adsl 录制' Selection.Font.Size = 30 Selection.FormattedText.Bold = True Selection.Font.Color = wdColorBlack Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter Selection.TypeText Text:=" 通知" Selection.Font.Color = wdColorRed Selection.TypeParagraph Selection.Font.Size = 22 Selection.TypeText Text:=" 今天下午三一)" Selection.MoveLeft Unit:=wdCharacter, Count:=2 Selection.TypeText Text:="(" Selection.MoveRight Unit:=wdCharacter, Count:=2 Selection.TypeText Text:="班 全体同学四点钟到操场进行体育课考试。
" Selection.TypeParagraph Selection.TypeParagraph Selection.TypeParagraph Selection.TypeText Text:= _ " " Selection.InsertDateTime DateTimeFormat:="yyyy-MM-dd", InsertAsField:= _ False, DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _ InsertAsFullWidth:=False Selection.MoveUp Unit:=wdLine, Count:=1 Selection.Font.Size = 22 Selection.MoveRight Unit:=wdCharacter, Count:=1 Selection.TypeParagraph Selection.Font.Size = 16 Selection.TypeText Text:=" " Selection.Font.Color = wdColorRedEnd With'4.保存,关闭文档,退出MyWord.Visible = False '设置WORD可见MyWordBook.SaveAs FileName:="c:\aa.doc"MyWordBook.CloseMyWord.QuitSet MyWordBook = NothingSet MyWord = NothingMsgBox "操作完毕", vbOKOnly, "提醒"unload meEnd Sub。
4.用VB 往word里写数据
Dim cn As New ADODB.Connection '定义数据库
Dim rs As New ADODB.Recordset
Dim scan As String '存储查找数据库
Dim Appword As Word.Application '定义WORD模型变量
Dim Newword As Word.Document
Set Appword = New Word.Application
Set Newword = Appword.Documents.Add(App.Path + "/stencil" + "/stencil.doc") '这里是打开模版文档。stencil是模板的意思。可根据自己的需要替换。
Appword.Visible = False '隐藏WORD。导出时不在任务栏出现WORD文档。
Appword.WindowState = wdWindowStateMinimize
scan = text2(0).Text '按编号搜索需要导出word的记录,一次只能导出一条记录
rs.CursorLocation = adUseClient
ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Path + "/data.mdb;Jet OLEDB:Database Password=harry2000"
cn.Open ConnectionString
rs.Open "select * from ADMIN where 编号 = '" & scan & "'", cn, adOpenKeyset, adLockOptimistic'查找需要导出的记录
If rs.RecordCount = 0 Then '如果不存在该记录
MsgBox "请在左边选择需要导出的记录"
Appword.Documents.Close
Appword.Quit
Exit Sub
Else '如果存在记录则运行以下代码
With Newword '设置模版表格和在表格中填入数据库内容。
.Tables(1).Cell(1, 1).Range.Text = (Format(rs!日期, "yyyy年mm月dd日"))
.Tables(2).Cell(3, 4).Range.Text = (rs!时间) '可以根据自己的需要设置填写内容。
End With
Appword.ChangeFileOpenDirectory (App.path+ "/导出WORD文件夹")
Appword.ActiveDocument.SaveAs FileName:=(App.path+ "/导出WORD文件夹/" & rs!姓名 & Format(Now, "yyyy-mm-dd") & ".doc"), FileFormat:=wdFormatDocument, LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:=False
Appword.Documents.Close
Appword.Quit
MsgBox "导出成功," & rs!姓名 & Format(Now, "yyyy-mm-dd") &; "的资料保存于" & vbCrLf & vbCrLf & App.path + "/导出WORD文件夹"
End If
Set Appword = Nothing '交还控制权
Set Newword = Nothing
Newword.Close
rs.Close '关闭数据库
5.vb调用word
源代码在办公室电脑上
明天上班贴给你
工程先引用word
Dim objWord As Object
Const CLASSOBJECT = "Word.Application"
Set objWord = CreateObject(CLASSOBJECT)
objWord.Visible = False '隐藏word界面
Dim win As Object
Set win = objWord.Documents.Add(App.Path & "\V-2.dot") '打开word模版把记录替换到模版中
Set MyTable = win.Tables(1) '将数据写入word 表中
MyTable.Cell(5, 4) = Adodc1.Recordset.Fields("l1") & ""
MyTable.Cell(6, 4) = Adodc1.Recordset.Fields("l2") & ""
MyTable.Cell(7, 4) = Adodc1.Recordset.Fields("l3") & ""
MyTable.Cell(8, 4) = Adodc1.Recordset.Fields("l16") & ""
MyTable.Cell(9, 4) = Adodc1.Recordset.Fields("l17") & ""
objWord.Visible = True
objWord.PrintPreview = True
Do
DoEvents
'判断是否在预览状态
If Not objWord.PrintPreview Then
objWord.ActiveDocument.Close (0) '不保存直接退出
'objword.Quit '用它可以直接退出word 但是我用的时候总是卡后来干脆不用了手运关闭word
Exit Do
End If
Loop
我也是刚学还不精
希望对你有帮助
6.如何用vb调用word,画出表格,然后向表格中写入东西
为什么不用VB在Excell里写东西呢?也可以打印并作出相应设置的。如果可以在Excell中使用的话,请与俺联系,俺给你操作代码。
----------------------------
在“工程”菜单-->;"引用"选项内选中“Microsoft Excel 11.0 Objiect library”
----------------------------
Dim xlApp As Excel.Application '定义Excel对象
Dim xlBook As Excel.Workbook '定义工作表对象
Dim xlSheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application") '-------------创建excell对象!!!
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1) '指定工作表1
'-------------------------------------
'--------赋值语句---------------------
' xlSheet.Cells(1, 1) = "序号"
xlSheet.Cells(1, 1) = "项目" 'xlSheet.Cells(i, j) ------ i:行定位;j:列定位.
xlSheet.Cells(1, 2) = "单位"
xlSheet.Cells(1, 3) = "数据"
xlSheet.Cells(1, 4) = "备注"
7.vb如何用宏写word
宏操作是在打开word文档以后录制的,也就是说运行宏也要在打开word文档后进行,所以要把宏复制到vb环境中去运行的话,就得先在vb中操作打开word文档,参考下面代码:(演示前请先c;下建一个文件名为test.doc的word空文档,操作完成后,你会发现C盘里有个aa.doc文件,你打开就会发现宏操作写的一则通知) Private Sub Command1_Click() Dim MyWord As Word.Application Dim MyWordBook As Word.Document Set MyWord = New Word.Application Set MyWordBook = MyWord.Documents.Add("c:\test.doc") '打开test.doc用户自定义的空白WORD文档 MyWordBook.Activate'3.放置宏代码 With MyWordBook'此处就可以插入宏代码了' Macro1 Macro' 宏在 2009-9-20 由 adsl 录制' Selection.Font.Size = 30 Selection.FormattedText.Bold = True Selection.Font.Color = wdColorBlack Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter Selection.TypeText Text:=" 通知" Selection.Font.Color = wdColorRed Selection.TypeParagraph Selection.Font.Size = 22 Selection.TypeText Text:=" 今天下午三一)" Selection.MoveLeft Unit:=wdCharacter, Count:=2 Selection.TypeText Text:="(" Selection.MoveRight Unit:=wdCharacter, Count:=2 Selection.TypeText Text:="班 全体同学四点钟到操场进行体育课考试。
" Selection.TypeParagraph Selection.TypeParagraph Selection.TypeParagraph Selection.TypeText Text:= _ " " Selection.InsertDateTime DateTimeFormat:="yyyy-MM-dd", InsertAsField:= _ False, DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _ InsertAsFullWidth:=False Selection.MoveUp Unit:=wdLine, Count:=1 Selection.Font.Size = 22 Selection.MoveRight Unit:=wdCharacter, Count:=1 Selection.TypeParagraph Selection.Font.Size = 16 Selection.TypeText Text:=" " Selection.Font.Color = wdColorRed End With'4.保存,关闭文档,退出 MyWord.Visible = False '设置WORD可见 MyWordBook.SaveAs FileName:="c:\aa.doc" MyWordBook.Close MyWord.Quit Set MyWordBook = Nothing Set MyWord = Nothing MsgBox "操作完毕", vbOKOnly, "提醒" unload me End Sub。
大波浪_