为您推荐:
精华内容
最热下载
问答
  • 9KB xxhuang0002 2014-01-31 11:36:02
  • 4星
    42KB gaorui68 2010-06-07 09:33:13
  • 10KB weixin_39840387 2019-07-09 15:18:08
  • -、用DAO控件连接数据库 1...." Data1.DatabaseName = App....pwd=123456" 'Data1.RecordSource = "耕地资源管理单元属性数据表2004" Data1.RecordSource = "select * ...http://www.qqview.com/Dev/VB/200742412648.Html

    -、用DAO控件连接数据库

    1.与Access2000数据库连接

    Private Sub Command1_Click()

    '也可直接在控件属性中设置以下各项但在控件属性中不能写入密码

    '只有在数据数没有密码的情况下可以省略

    Data1.Refresh

    Data1.Connect = "Access 2000;"

    Data1.DatabaseName = App. Path +

    "\chncmadb.mdb"

    '数据库没有密码此句可省

    Data1.Connect = ";pwd=123456"

    'Data1.RecordSource =

    "耕地资源管理单元属性数据表2004"

    Data1.RecordSource = "select * from

    耕地资源管理单元属性数据表2004"

    Data1.Refresh

    ’move后才能正确显示记录个数

    End Sub

    2.与没有密码的DBF文件数据库连接

    Private Sub Command2_Click()

    Data1.Connect = "dBASE III;"

    Data1.DatabaseName = App. Path

    ' Data1.RecordSource ="DBF"

    Data1.RecordSource = "select * from

    dbf"

    Data1.Refresh

    ’move后才能正确显示记录个数

    End Sub

    3.与没有密码的Excel文件数据库连接

    Private Sub Command3_Click()

    Data1.Connect = "Excel 8.0;"

    Data1.DatabaseName = App.Path &

    "\EXcel.xls"

    Data1.RecordSource = "select *

    from [EXcel.xls]"

    Data1.Refresh

    ’move后才能正确显示记录个

    End Sub

    二、用DAO代码连接数据库

    '在使用DAO对象前应选定Visual

    Basic菜单下的[工程]中的引用了菜单中的[Microsoft DAO 3.6 Object

    Library]选项,或其它版本

    1.DAO代码与Access数据库连接

    Private Sub Command1_Click()

    Dim Db As Database

    Dim Rs As Recordset

    '以共享、读写方式打开'如果无密码最后一个参数可以不要

    Set Db= OpenDatabase(App.Path &

    "\chncmadb.mdb", False, False, ";pwd=123456")

    '不需要move来更新记录个数

    'Set Rs =

    Db.OpenRecordset("耕地资源管理单元属性数据表2004")

    '需要move来更新记录个数

    Set Rs = Db.OpenRecordset("select * from

    [耕地资源管理单元属性数据表2004]")

    If Rs.RecordCount > 0 Then

    Rs.MoveLast

    Rs.MoveFirst

    End If

    End Sub

    2.DAO代码与没有密码的DBF文件数据库连接

    Private Sub Command2_Click()

    Dim Db As Database

    Dim Rs As Recordset

    '以共享、读写方式打开

    Set Db = OpenDatabase(App.Path, False, False,

    "dbase

    III;")

    '不需要move来更新记录个数

    'Set Rs = Db.OpenRecordset("DBF")

    ’需要move来更新记录个数

    Set Rs = Db.OpenRecordset("select * from

    [DBF]")

    If Rs.RecordCount > 0 Then

    Rs.MoveLast

    Rs.MoveFirst

    End If

    End sub

    3.

    'DAO代码与没有密码的Excel文件数据库连接

    Private Sub Command3_Click()

    Dim Db As Database

    Dim Rs As Recordset

    '以共享、读写方式打开'如果无密码最后一个参数可以不要

    Set Db = OpenDatabase(App.Path &

    "\EXcel.xls", False, False, "Excel 8.0;")

    '不需要move来更新记录个数

    ' Set Rs = Db.OpenRecordset("EXcel.xls")

    '表格中的工作目录sheet

    '需要move来更新记录个数

    Set Rs = Db.OpenRecordset("select * from

    [EXcel.xls]") '表格中的工作目录sheet'

    If Rs.RecordCount > 0 Then

    Rs.MoveLast

    Rs.MoveFirst

    End If

    End Sub

    三、用ADO控件连接数据库

    '也可直接在控件属性中设置以下各项

    1.ADO控件与Access2000数据库连接

    Private Sub Command1_Click()

    '连接有密码的Access数据库

    'Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data

    Source=" & App.Path & "\chncmadb1.mdb;Jet OLEDB:DataBase

    PASSWORD=123456"

    '连接没有密码的Access数据库

    Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data

    Source=" & App.Path & "\chncmadb.mdb;Persist Security

    Info=False"

    'Adodc1.RecordSource =

    "[耕地资源管理单元属性数据表2004]"

    Adodc1.RecordSource = "select * from

    [耕地资源管理单元属性数据表2004]"

    Adodc1.Refresh

    Set

    DataGrid1.DataSource = Adodc1

    DataGrid1.Refresh

    End Sub

    2.'ADO控件与DBF表连接

    Private Sub Command2_Click()

    'Adodc1.ConnectionString = "Provider=MSDASQL.1;Persist Security

    Info=False;Data Source=dBASE Files;DBQ=" & App.Path &

    ";SourceType=DBF;"

    'Adodc1.ConnectionString = "Provider=MSDASQL.1;Persist Security

    Info=False;Extended Properties=DSN=Visual FoxPro

    Tables;UID=;SourceDB=”& app.path

    &”;SourceType=DBF;Exclusive=No;BackgroundFetch=Yes;Collate=Machine;Null=Yes;Deleted=Yes;"

    'Adodc1.ConnectionString = "Provider=MSDASQL.1;Persist Security

    Info=False;Extended Properties=DSN=dBASE Files;DBQ=”& app.path

    &”;;DefaultDir=”& app.path

    &”;DriverId=533;MaxBufferSize=2048;PageTimeout=5;"

    '能使表名长度不受限制

    Adodc1.ConnectionString = "Provider=MSDASQL.1;Driver=Microsoft

    Visual Foxpro Driver;SourceDB=" & App.Path &

    ";SourceType=DBF;Locale Identifier=2052"

    'Adodc1.RecordSource = "[DBF1]"

    Adodc1.RecordSource = "select * from DBF1"

    Adodc1.Refresh

    Set

    DataGrid1.DataSource = Adodc1

    DataGrid1.Refresh

    End Sub

    3.'ADO控件与Excel表连接

    Private Sub Command3_Click()

    '下面一句测试未能通过

    'Adodc1.ConnectionString = "Data

    Provider=MSDASQL.1;driver=Microsoft Excel Driver *.xls);DBQ=" &

    App.Path & "\EXcel.xls"

    'Adodc1.ConnectionString="Provider=MSDASQL.1;Persist Security

    Info=False;Extended Properties=DSN=Excel Files;DBQ=" & App.Path

    & "\EXcel.xls;DefaultDir=”&app.path

    &”;DriverId=790;MaxBufferSize=2048;PageTimeout=5;"

    Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist

    Security Info=False;Data Source=" & App.Path &

    "\EXcel.xls;Extended Properties='Excel 8.0;HDR=Yes'"

    'Adodc1.RecordSource = "[EXcel.xls]"

    Adodc1.RecordSource = "select * from [EXcel.xls]"

    Adodc1.Refresh

    Set

    DataGrid1.DataSource = Adodc1

    DataGrid1.Refresh

    End Sub

    4.'ADO控件与Oracle数据库连接

    Private Sub Command4_Click()

    'Adodc1.ConnectionString =

    "Provider=MSDAORA.1;Password=chncmadb;User ID=chncmadb;Data

    Source=towebserver;Persist Security Info=True"

    Adodc1.ConnectionString="Provider=OraOLEDB.Oracle.1;Password=chncmadb;Persist

    Security Info=True;User ID=chncmadb;Data

    Source=towebserver"

    'Adodc1.RecordSource = "T320481TR012004"

    '表名不能加方括号

    Adodc1.RecordSource = "select * from T320481TR012004"

    Adodc1.Refresh

    Set

    DataGrid1.DataSource = Adodc1

    DataGrid1.Refresh

    End Sub

    5.'ADO控件与SQLserver数据库连接

    '未测试

    Private Sub Command5_Click()

    Adodc1.ConnectionString = "Provider=SQLOLEDB.1;Password=111;Persist

    Security Info=True;User ID=111;Initial Catalog=111;Data

    Source=111"

    'Adodc1.RecordSource = "T320481TR012004"

    Adodc1.RecordSource = "select * from T320481TR012004"

    Adodc1.Refresh

    Set

    DataGrid1.DataSource = Adodc1

    DataGrid1.Refresh

    End Sub

    四、用ADO代码连接数据库

    '在使用ADO对象前应选定Visual

    Basic菜单下的[工程]中的引用了菜单中的[Microsoft.ActiveX Data Object

    2.5 Library]选项,或其它版本

    1.'ADO代码与Access2000数据库连接

    Private Sub Command1_Click()

    Dim

    AdoCnn As ADODB.Connection

    Dim

    AdoRs As ADODB.Recordset

    Set

    AdoCnn = New ADODB.Connection

    Set

    AdoRs = New ADODB.Recordset

    AdoCnn.CursorLocation = adUseClient

    '.open后面的字符串可以参考ADO控件连接.ConnectionString后面的的字符串

    AdoCnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &

    App.Path & "\chncmadb1.mdb;Jet OLEDB:DataBase

    PASSWORD=123456"

    AdoRs.Open "select * from

    [耕地资源管理单元属性数据表2004]", AdoCnn, adOpenDynamic,

    adLockPessimistic, adCmdText

    Set

    DataGrid1.DataSource = AdoRs

    Set

    AdoRs = Nothing

    Set

    AdoCnn = Nothing

    End Sub

    2.'ADO代码与DBF表连接

    Private Sub Command2_Click()

    Dim

    AdoCnn As ADODB.Connection

    Dim

    AdoRs As ADODB.Recordset

    Set

    AdoCnn = New ADODB.Connection

    Set

    AdoRs = New ADODB.Recordset

    AdoCnn.CursorLocation = adUseClient

    '.open后面的字符串可以参考ADO控件连接.ConnectionString后面的的字符串

    AdoCnn.Open "Provider=MSDASQL.1;Driver=Microsoft Visual Foxpro

    Driver;SourceDB=" & App.Path & ";SourceType=DBF;Locale

    Identifier=2052"

    AdoRs.Open "select * from [DBF1]", AdoCnn,

    adOpenDynamic, adLockPessimistic, adCmdText

    Set

    DataGrid1.DataSource = AdoRs

    Set

    AdoRs = Nothing

    Set

    AdoCnn = Nothing

    End Sub

    3.'ADO代码与Excel表连接

    Private Sub Command3_Click()

    Dim

    AdoCnn As ADODB.Connection

    Dim

    AdoRs As ADODB.Recordset

    Set

    AdoCnn = New ADODB.Connection

    Set

    AdoRs = New ADODB.Recordset

    AdoCnn.CursorLocation = adUseClient

    '.open后面的字符串可以参考ADO控件连接.ConnectionString后面的的字符串

    AdoCnn.Open"Provider=Microsoft.Jet.OLEDB.4.0;Persist Security

    Info=False;Data Source=" & App.Path & "\EXcel.xls;Extended

    Properties='Excel 8.0;HDR=Yes'"

    AdoRs.Open "select * from [EXcel.xls]",

    AdoCnn, adOpenDynamic, adLockPessimistic, adCmdText

    Set

    DataGrid1.DataSource = AdoRs

    Set

    AdoRs = Nothing

    Set

    AdoCnn = Nothing

    End Sub

    4.'ADO代码与Oracle数据库连接

    Private Sub Command4_Click()

    Dim

    AdoCnn As ADODB.Connection

    Dim

    AdoRs As ADODB.Recordset

    Set

    AdoCnn = New ADODB.Connection

    Set

    AdoRs = New ADODB.Recordset

    AdoCnn.CursorLocation = adUseClient

    '.open后面的字符串可以参考ADO控件连接.ConnectionString后面的的字符串

    AdoCnn.Open "Provider=OraOLEDB.Oracle.1;Password=chncmadb;Persist

    Security Info=True;User ID=chncmadb;Data

    Source=towebserver"

    AdoRs.Open "select * from T320481TR012004",

    AdoCnn, adOpenDynamic, adLockPessimistic, adCmdText

    Set

    DataGrid1.DataSource = AdoRs

    Set

    AdoRs = Nothing

    Set

    AdoCnn = Nothing

    End Sub

    5.'ADO代码与SQLserver数据库连接

    '未测试

    Private Sub Command5_Click()

    Dim

    AdoCnn As ADODB.Connection

    Dim

    AdoRs As ADODB.Recordset

    Set

    AdoCnn = New ADODB.Connection

    Set

    AdoRs = New ADODB.Recordset

    AdoCnn.CursorLocation = adUseClient

    '.open后面的字符串可以参考ADO控件连接.ConnectionString后面的的字符串

    AdoCnn.Open "Provider=SQLOLEDB.1;Password=111;Persist Security

    Info=True;User ID=111;Initial Catalog=111;Data

    Source=111"

    AdoRs.Open "select * from T320481TR012004",

    AdoCnn, adOpenDynamic, adLockPessimistic, adCmdText

    Set

    DataGrid1.DataSource = AdoRs

    Set

    AdoRs = Nothing

    Set

    AdoCnn = Nothing

    End Sub

    该文章转载自网络大本营:http://www.qqview.com/Dev/VB/200742412648.Html

    展开全文
    weixin_39821718 2021-01-21 07:53:07
  • 5星
    7KB buaazk 2016-08-01 13:12:25
  • 4星
    3KB hzgang811856054 2012-12-19 16:42:37
  • 6KB weixin_39840650 2019-07-10 19:43:12
  • 5星
    7KB kuolun168 2014-08-18 11:55:55
  • VB6+Mo图层顺序的调整(MoveToTop、MoveTo) object.MoveToTop 要移动图层的索引 移动图层顺序为第一层 object.MoveToBottom 要移动图层的索引 移动图层顺序为最后一层 object.MoveTo value1, value2 (value1...

    VB6+Mo图层顺序的调整(MoveToTop、MoveTo)

    object.MoveToTop 要移动图层的索引
    移动图层顺序为第一层
    object.MoveToBottom 要移动图层的索引
    移动图层顺序为最后一层
    object.MoveTo value1, value2
    (value1是移动图层的索引,value2 是要移动的图层顺序)

    示例:
    1代码:

    '在窗体加载时时,给lstLayers列表添加选择集
    Private Sub Form_Load()
        Dim lyr As MapObjects2.MapLayer
        '清空lstLayers选择集中的列表
        lstLayers.Clear
        '遍历当前地图中图层, 将图层名称添加至 lstLayers选择集合中
         For Each lyr In Map1.Layers
           lstLayers.AddItem lyr.Name
         Next lyr
    End Sub
    '置顶图层
    Private Sub Command1_Click()
        '定义一个矢量图层
        Dim lyr As MapObjects2.MapLayer
        If lstLayers.ListIndex <> -1 Then
           '移动当前选中图层至第一个
            Map1.Layers.MoveToTop lstLayers.ListIndex
            Map1.Refresh
            lstLayers.Clear
            For Each lyr In Map1.Layers
                lstLayers.AddItem lyr.Name
            Next lyr
            'lstLayers列表默认选中第一个图层
            lstLayers.Selected(0) = True
       End If
    End Sub
    
    '上移图层
    Private Sub Command2_Click()
        Dim i As Integer
        Dim lyr As MapObjects2.MapLayer
        If lstLayers.ListIndex <> -1 And lstLayers.ListIndex > 0 Then
           i = lstLayers.ListIndex - 1
           Map1.Layers.MoveTo lstLayers.ListIndex, i
           Map1.Refresh
           lstLayers.Clear
           For Each lyr In Map1.Layers
             lstLayers.AddItem lyr.Name
           Next lyr
           lstLayers.Selected(i) = True
         End If
    End Sub
    
    '下移图层
    Private Sub Command3_Click()
        Dim i As Integer
        Dim lyr As MapObjects2.MapLayer
        If lstLayers.ListIndex <> -1 And lstLayers.ListIndex < lstLayers.ListCount - 1 Then
           i = lstLayers.ListIndex + 1
           Map1.Layers.MoveTo lstLayers.ListIndex, i
           Map1.Refresh
           lstLayers.Clear
           For Each lyr In Map1.Layers
             lstLayers.AddItem lyr.Name
           Next lyr
          lstLayers.Selected(i) = True
         End If
    End Sub
    
    '置底图层
    Private Sub Command4_Click()
        If lstLayers.ListIndex <> -1 Then
            Map1.Layers.MoveTo lstLayers.ListIndex, Map1.Layers.Count - 1
            Map1.Refresh
               lstLayers.Clear
               For Each lyr In Map1.Layers
                 lstLayers.AddItem lyr.Name
               Next lyr
              lstLayers.Selected(Map1.Layers.Count - 1) = True
         End If
    End Sub
    
    
    展开全文
    qq_41441896 2019-12-19 21:24:13
  • 1KB tianlongbang 2011-03-31 17:11:10
  • w131313s 2013-06-09 01:27:36
  • 4星
    4KB allan5204 2008-10-08 17:17:14
  • 利用VB操作WORD的基本方法通过查阅资料,自我实践,经实验通过。先引用word Application:Dim MyWord As Word.ApplicationDim MyWordBook As Word.DocumentSet MyWord = New Word.ApplicationSet MyWordBook =MyWord...

    利用VB操作WORD的基本方法

    通过查阅资料,自我实践,经实验通过。

    先引用word Application:

    Dim MyWord As Word.Application

    Dim MyWordBook As Word.Document

    Set MyWord = New Word.Application

    Set MyWordBook =

    MyWord.Documents.Add(ModelPath & "test.dot")

    '打开test.dot用户自定义的WORD模版

    'MyWord =

    CreateObject("Word.Application")

    ' MyWord.Visible = False

    ' MyWord.Documents.Open

    ("test.doc")

    '

    MyWord.ActiveDocument.Bookmarks.Item("MM").Select

    ' MyWord.Selection.Text =

    "从资料库取出的相应资料"

    ' MyWord.ActiveDocument.SaveAs

    ("test1.doc")

    MyWordBook.Activate

    With MyWordBook

    ' .Paragraphs(.Paragraphs.Count).Range.Font.Size = 30

    '设置文字字体大小(其它设置类推) 如果去除.Paragraphs(.Paragraphs.Count)则表示所有行

    ' .Paragraphs(.Paragraphs.Count).Alignment = wdAlignParagraphCenter

    '设置文字居中等

    ' .Content.InsertAfter "hello" &

    vbCrLf '向WORD写入一行文字并回车

    ' .Content.InsertAfter "hello"

    ' .Paragraphs(.Paragraphs.Count).Range.Font.Size = 10

    ' .Content.InsertAfter "hello"

    .Bookmarks("mm").Range.InsertAfter

    "name" '在WORD指定位置(标签)写入文本,指定位置指WORD标签

    .Tables(1).Cell(2, 1).Range.InsertAfter "nameFDSAFDSAFDSA"

    '在WORD第一个表格的第2行第1列插入文本

    .Tables(1).Cell(2, 2).Range.InsertAfter "MM"

    .Range(Start:=.Paragraphs(2).Range.Start + 3,

    End:=.Paragraphs(2).Range.Start + 8).Font.Size = 30

    '给选定的文本(第二段开始位置加3至8的字体)设置字体,其它的累推

    .Tables(1).Cell(2, 1).Range.Font.Size = 20 '给选定的单元格设置字体,其它的累推

    End With

    MyWord.Visible = True

    Set MyWord = Nothing

    Set MyWordBook = Nothing

    =========================================

    1、对其WORD内容设置字体样式,以及在WORD中插入表格,以及表格单元格融合与填充.

    Option Explicit

    Private Sub Command1_Click()

    Dim filename As String

    CD.ShowSave

    filename = CD.filename

    OutWord filename

    MsgBox "OK"

    End Sub

    Private Function OutWord(ByVal filePath

    As String) As Boolean

    Dim newDoc As Word.Document

    Set newDoc = New Word.Document

    With newDoc

    .Paragraphs(.Paragraphs.Count).Range.Font.Name = "宋体"

    .Paragraphs(.Paragraphs.Count).Range.Font.Size = 10.5

    .Paragraphs(.Paragraphs.Count).Alignment =

    wdAlignParagraphRight

    .Content.InsertAfter "編号:"

    & vbCrLf

    .Paragraphs(.Paragraphs.Count).Range.Font.Name = "宋体"

    .Paragraphs(.Paragraphs.Count).Range.Font.Size = 26

    .Paragraphs(.Paragraphs.Count).Range.Font.Bold = True

    .Paragraphs(.Paragraphs.Count).Alignment =

    wdAlignParagraphCenter

    .Content.InsertAfter vbCrLf

    & "XXXXXXXXX報告" & vbCrLf

    & vbCrLf & vbCrLf &

    vbCrLf & vbCrLf

    .Paragraphs(.Paragraphs.Count).Range.Font.Name = "宋体"

    .Paragraphs(.Paragraphs.Count).Range.Font.Size = 15

    .Paragraphs(.Paragraphs.Count).Range.Font.Bold = False

    .Paragraphs(.Paragraphs.Count).Alignment = wdAlignParagraphLeft

    .Content.InsertAfter "项目名称:"

    & vbCrLf

    .Content.InsertAfter "应急类型:"

    & vbCrLf

    .Content.InsertAfter "预警状态:正常/警界/危机"

    & vbCrLf

    .Paragraphs(.Paragraphs.Count).Alignment =

    wdAlignParagraphCenter

    .Tables.Add

    Range:=.Range(Start:=.Range.End - 1, End:=.Range.End), NumRows:=1,

    NumColumns:=3, DefaultTableBehavior:=wdWord9TableBehavior,

    AutoFitBehavior:=wdAutoFitFixed

    With .Tables(1)

    If .Style

    <> "表 (格子)" Then

    .Style = "表 (格子)"

    End If

    .ApplyStyleHeadingRows = True

    .ApplyStyleLastRow = True

    .ApplyStyleFirstColumn = True

    .ApplyStyleLastColumn = True

    .Columns.Width = 50

    .Rows.Height = 20

    End With

    .Paragraphs(.Paragraphs.Count).Range.Font.Name = "宋体"

    .Paragraphs(.Paragraphs.Count).Range.Font.Size = 15

    .Paragraphs(.Paragraphs.Count).Range.Font.Bold = False

    .Paragraphs(.Paragraphs.Count).Alignment = wdAlignParagraphLeft

    .Content.InsertAfter "委 托 人:"

    & vbCrLf

    .Content.InsertAfter "预 警 机 构:"

    & vbCrLf

    .Content.InsertAfter "报告负责人:"

    & vbCrLf

    .Content.InsertAfter "时 间:"

    & vbCrLf

    .Paragraphs(.Paragraphs.Count).Alignment = wdAlignParagraphLeft

    .Tables.Add

    Range:=.Range(Start:=.Range.End - 1, End:=.Range.End), NumRows:=8,

    NumColumns:=2, DefaultTableBehavior:=wdWord9TableBehavior,

    AutoFitBehavior:=wdAutoFitFixed

    With .Tables(2)

    If .Style

    <> "表 (格子)" Then

    .Style = "表 (格子)"

    End If

    .ApplyStyleHeadingRows = True

    .ApplyStyleLastRow = True

    .ApplyStyleFirstColumn = True

    .ApplyStyleLastColumn = True

    .Cell(2, 1).Range.Text = "项目名称"

    .Range.Cells(3).Row.Cells.Merge

    .Range.Cells(3).Range.Font.Size =

    15

    .Range.Cells(3).Range.Text =

    "信息来源/文献检索范围:" & vbCrLf & vbCrLf

    & vbCrLf

    .Range.Cells(4).Row.Cells.Merge

    .Range.Cells(4).Range.Text =

    "情况描述/检索结果:" & vbCrLf & vbCrLf

    & vbCrLf

    .Range.Cells(5).Row.Cells.Merge

    .Range.Cells(5).Range.Text = "影响分析:"

    & vbCrLf & vbCrLf &

    vbCrLf & vbCrLf

    .Range.Cells(6).Row.Cells.Merge

    .Range.Cells(6).Range.Text = "建议:"

    & vbCrLf & vbCrLf &

    vbCrLf & vbCrLf & vbCrLf

    & vbCrLf

    .Range.Cells(7).Row.Cells.Merge

    .Range.Cells(7).Range.Text = "专家组成员:"

    & vbCrLf & vbCrLf &

    vbCrLf & vbCrLf & vbCrLf

    & vbCrLf

    .Range.Cells(8).Row.Cells.Merge

    .Range.Cells(8).Range.Text = "附件目录:"

    & vbCrLf & vbCrLf &

    vbCrLf & vbCrLf & vbCrLf

    & vbCrLf

    .Range.Cells(9).Row.Cells.Merge

    .Range.Cells(9).Range.Text = "报告负责人:"

    & vbCrLf & vbCrLf &

    vbCrLf & vbCrLf & " 年 月 日"

    End With

    End With

    newDoc.SaveAs filePath

    newDoc.Close

    End Function

    2、

    很多人都知道,用vb操作excel的表格非常简单,但是偏偏项目中碰到了VB操作word表格的部分,google、baidu搜爆了,都没有找到我需要的东西。到是搜索到了很多问这个问题的记录。没办法,索性只有自己去尝试了。下面把一些代码发上来,给需要的朋友一点提示

    a4c26d1e5885305701be709a3d33442f.png

    打开一个已经存在的wrod文件(这个文件包含了表格)

    Dim WordApp

    Dim Word

    Set WordApp =

    CreateObject("Word.Application")

    WordApp.Visible = True

    Set Word =

    WordApp.Documents.Open("c:\record.dot")

    知道了就很简单了,下面是选定某一个表格的一个单元格,并修改其内容

    Word.Tables(1).cell(1, 2)="内容"

    VBA中的这些数组元素下标都是从1开始的,比如excel的第一行一列也是ExSheet.Cells(1,1),而不是ExSheet.Cells(0,0),WORD的表格也是这样,不信自己试一下就知道了

    a4c26d1e5885305701be709a3d33442f.png。所以上面那句话的意思就是对整个word文档中的第一个表格的第一行第二列的内容改变为“内容”。很简单吧?网上有些人在问是不是

    Word.Tables(1).cell(1,

    2).range.text或者Word.Tables(1).cell(1,

    2).text。试一下就发现这2种都不对

    a4c26d1e5885305701be709a3d33442f.png

    插入图片其实也很简单,代码如下:

    Word.Tables(1).cell(1,

    3).Range.InlineShapes.AddPicture ("c:\photo.jpg")

    微软的那一套东西集成得很不错,其之间任意调用非常方便,大家如果想用VB对WORD做更多的应用,却又不知道怎么实现,我想最好的办法就是录制宏了,你把你想完成的功能操作一遍,然后查看宏,一目了然了吧!

    ------------------

    问题:

    我在vb中调用word打印报表,代码是在word中录制的宏拷贝过来的,但是在生成表格时编译通不过,代码如下:

    Dim wdApp As Word.Application

    Dim wdBook As Word.Document

    Dim Range As Range

    Dim NumRows As Long

    Dim NumColumns As Long

    Set wdApp =

    CreateObject("Word.Application")

    Set wdBook = wdApp.Documents.Add

    wdApp.Visible = True

    ....

    ....

    --〉wdApp.ActiveDocument.Tables.Add

    Range:=Selection.Range, NumRows:=16, NumColumns _

    :=5,

    DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _

    wdAutoFitWindow

    运行时上面这句报实时错误'91'

    对象变量或with块变量未设置

    该如何写?

    对策:

    看你的代码是想增加一个表格,VBA的代码和VB的代码是不一样的,需要转换一下,多用几次就清楚了。

    应该这样写

    Call

    wdBook.Tables.Add(wdBook.application.Selection.Range, 16, 5, 1,

    0)

    问题:如果我要增加一个复杂的表格,举例说就是在表格中我要合并、拆分一些单元格,还要绘制表头(带斜线的),但是在word中录制宏时无法对表格进行这些操作,我该怎么办?能不能给出一些代码?

    对策:

    接着上次那个程序来。。。

    Set mySelection =

    wdApp.Documents.Application.Selection

    mySelection.Cells.Borders(-7).LineStyle

    = 1

    '选中表格的第2行第3列

    table.Cell(2, 3).Select

    '向下移动6格,第1个参数和第3个是常数

    Call

    wdBook.Application.Selection.MoveDown(5, 6, 1)

    '合并

    wdBook.Application.Selection.Cells.Merge

    '拆分成7行2列

    Call

    wdBook.Application.Selection.Cells.Split(7, 2,

    True)

    问题:我按你如上所说的方法去做,但是在执行table.cell(2,3).select时报错,错误为实时错误'424',要求对象,我不知道是不是在table前需要加上wdapp还是wdbook,或者是myselection,不过我都试过了,还是不行,后面的就写不下去了。我主要是想生成一个7列27行的表格,第1列的1、2两行合并为一个单元格,第1列的第5行和第6行合并做一个带斜线的表头,斜线上下要分别输入“压力计”和“测试点”作为表头分类,第4行的第2、3、4列单元格合并为一个单元格,5、6、7列合并为一个单元格,望楼主能详细写一下代码。非常感谢!我的代码如下:

    Dim wdApp As Word.Application

    Dim wdBook As Word.Document

    Set wdApp =

    CreateObject("Word.Application")

    Set wdBook = wdApp.Documents.Add

    wdApp.Visible = True

    wdApp.Selection.Font.Name = "黑体"

    wdApp.Selection.Font.Size = 22

    wdApp.Selection.ParagraphFormat.Alignment =

    wdAlignParagraphCenter

    wdApp.Selection.TypeText

    Text:="通风机调试报告"

    wdApp.Selection.TypeParagraph

    wdApp.Selection.TypeParagraph

    wdApp.Selection.Font.Name =

    "仿宋_GB2312"

    wdApp.Selection.Font.Size = 12

    Call

    wdApp.ActiveDocument.Tables.Add(wdApp.Application.Selection.Range,

    NumRows:=27, NumColumns _

    :=7,

    DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _

    wdAutoFitFixed)

    Set mySelection =

    wdApp.Documents.Application.Selection

    mySelection.Cells.Borders(-7).LineStyle

    = 1

    '选中表格的第2行第3列

    --> Table.Cell(2,

    3).Select

    '向下移动6格,第1个参数和第3个是常数

    Call

    wdBook.Application.Selection.MoveDown(5, 6, 1)

    '合并

    wdBook.Application.Selection.Cells.Merge

    '拆分成7行2列

    Call

    wdBook.Application.Selection.Cells.Split(7, 2, True)

    Set wdBook = Nothing

    对策:

    不好意思啊,上次在自己机器上测试了一下,没有拷全部代码,不过就这个应该也能理解到呀,table就是要操作的那个table,一个word里面有可能有多个table,我们首先要选中要操作的那个table,我们这个table是自己用代码生成的,所以有一个方便的代码就是

    dim Table

    set Table =

    wdApp.ActiveDocument.Tables.Add(wdApp.Application.Selection.Range,

    NumRows:=27, NumColumns _

    :=7,

    DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _

    wdAutoFitFixed)

    修改你的程序里面插入table的那句话即可

    ---------------------------------------------

    顺便提一下,像你这样的程序,我个人觉得是否改成模板要方便些呢?你用word做一个模板,把表格什么的全都先写好,然后保存成模板文件。然后你再用程序加载这个模板,然后往模板里填写数据。这样难度要低一些。不过具体情况具体分析。(个人意见)

    之前我不是说了如果是自己创建的表格可以很方便的得到表格对象吗?就在创建时直接取得了。其实还有另外一种办法就是:你的其他程序都不变,只把出错的那句话改成:

    wdApp.ActiveDocument.Tables(1).Cell(2,

    3).Select

    就像我开篇说的,word、excel这些集合的下标都是从1开始,然后只要找到表格那个集合,然后选取第一个表格就是要操作的表格了(因为程序只创建了一个表格)。

    如果是模板的话,就应该是对已经存在的表格进行操作了,就只有用这中办法弄了。不知道我说清楚没有。

    问题:

    我想通过vb在word里添加多个表格,因为数据的列数较多,希望能

    分成多个表格显示数据,方便查看。

    部分代码如下:

    For I = 1 To INT_COL

    Call

    MyWord.ActiveDocument.Tables.Add(MyWord.Application.Selection.Range,

    NumRows:=30, _

    NumColumns:=8,

    DefaultTableBehavior:=wdWord9TableBehavior, _

    AutoFitBehavior:=wdAutoFitFixed)

    With

    MyWord.Application.Selection.Tables(1)

    If .Style

    <> "网格型" Then

    .Style = "网格型"

    End If

    .ApplyStyleHeadingRows = True

    .ApplyStyleLastRow = True

    .ApplyStyleFirstColumn = True

    .ApplyStyleLastColumn = True

    End With

    MyWord.Application.Selection.MoveDown Unit:=wdLine,Count:=30

    MyWord.Application.Selection.TypeParagraph

    Next I

    但是,每次程序执行到MyWord.Application.Selection.MoveDown Unit:=wdLine,Count:=30这一句,就跳出程序。为什么?

    另:您有什么好的建议,处理这种多行多列数据吗?

    对策:

    程序修改如下,可以出现多个表格,但是在给每一个表格输入数据时,我就不知道怎么处理了,盼回复

    For I = 1 To INT_COL

    Call

    MyWord.ActiveDocument.Tables.Add(MyWord.Selection.Range,

    NumRows:=30, _

    NumColumns:=8,

    DefaultTableBehavior:=wdWord9TableBehavior, _

    AutoFitBehavior:=wdAutoFitFixed)

    MyWord.Selection.Tables(1).cell(1, 1) =

    "Year"

    Call MyWord.Selection.MoveDown(5,

    30)

    MyWord.Selection.TypeParagraph

    Next

    I

    你的代码基本上都差不多了,我没测试,看样子是你的思路有点问题(循环不对)

    你可以先生成1个表格,再处理表格的数据。

    也可以把所有表格全部生成,再处理表格数据,

    看你的代码,你选择的是第2种,不管是哪种,肯定是多重循环的,你的代码只有1重循环。

    思路如下:

    for 表格数

    生成这个表格,得到表格对象

    for 表格的行

    for 表格行的单元格

    单元格="单元格的内容"

    next

    next

    next

    呵呵,不知道说清楚没有

    你的问题并非上述原因,是我没有把光标移出表格。

    楼主实在是高

    有个问题一直很奇怪,怎么把光标移到文档末尾,比如我要把剪切板上的东西复制到文件尾。我写的是

    ActiveDocument.Range(ActiveDocument.End,ActiveDocument.End).Select

    Selection.Paste

    这样是不对的

    如何用vb在word 中生成多个表格,尤其是在很多时

    涉及到换页的操作时,

    大家各我看看,我的邮箱是999999999liguang@.163.com

    qq:26749732

    下面是我的代码 :

    For n = 1 To 6

    ActiveDocument.Tables.Add

    Range:=Selection.Range, NumRows:=10+n, NumColumns:= _

    6,

    DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _

    wdAutoFitFixed

    With wdDoc.Tables(n)

    .Cell(1, 1).Merge .Cell(1, 6)

    End With

    With Selection.Tables(1)

    If .Style

    <> "网格型" Then

    .Style = "网格型"

    End If

    .ApplyStyleHeadingRows = True

    .ApplyStyleLastRow = True

    .ApplyStyleFirstColumn = True

    .ApplyStyleLastColumn = True

    End With

    ActiveWindow.ActivePane.VerticalPercentScrolled = 45

    Selection.MoveDown Unit:=wdLine,

    Count:=10+n + 1

    Selection.TypeParagraph

    Next n

    展开全文
    weixin_36074271 2021-06-16 09:04:29
  • 4星
    5KB u011726400 2013-10-12 17:18:33
  • 其语法格式为: object.Cls Move方法:用于移动窗体或控件,并可以改变其尺寸大小,其语法格式为: object.Move left, top, width, height 其中: object:可选项。表示移动窗体或控件。一个对象表达式,其值为...

    15cff1bb0b12d3e15c2d1b91e2ef36da.png

    4、命令按钮(Command Button)

    作用:用于开始、中断或结束一个按钮。

    (1)常用属性

    Caption属性

    Style属性

    visible =false ' 按钮不可见

    enabled=true ' 按钮无效

    default =true ' 使按钮成为缺省的“活动按钮”,可用enter键选中

    cancel =true ' 使按钮成为缺省的“取消按钮”,可用esc键选中

    注意:在一个窗体中,只能有一个命令按钮可以设为缺省的“活动按钮”,也只能有一个命令按钮可以设为缺省的“取消按钮”。

    Picture '加载一幅图片

    有时.我们可能还需要在命令按钮上显示某个图案以使界面显得活泼生动,要制作这样的按钮,需要设置它的两个属性;styLe属性和Picture属性。我们先将该命令按钮的style属性设置为1(GraPhical),再通过其Picture属性加载一幅图片即可,如图显示了一个带图案的命令按钮。

    (2)主要事件

    最主要的事件是单击(Click)事件。

    (3)常用方法

    Print方法:用于在窗体、图片框和打印机上显示文本。其语法格式为:

    object.Print [outputlist]

    Cls方法:用来清除运行时窗体或图片框所生成的图形和文本。其语法格式为:

    object.Cls

    Move方法:用于移动窗体或控件,并可以改变其尺寸大小,其语法格式为:

    object.Move left, top, width, height

    其中:

    object:可选项。表示移动窗体或控件。一个对象表达式,其值为“应用于”列表中的一个对象。如果省略object,带有焦点的窗体缺省为 object。

    Left:必需项。指示 object 左边的水平坐标 (x-轴)。

    Top:可选项。指示 object 顶边的垂直坐标 (y-轴)。

    Width:可选项。指示 object 新的宽度。

    Height:可选项。指示 object 新的高度。

    (4)例子:

    见教材P109。计算器应用程序。

    代码如下:

    Public a, b, result As Double

    Public c As Integer

    Dim op As String

    Dim index As Integer

    --------------------------------------------------------------------------------

    Private Sub Form_Load()

    a = 0

    b = 0

    Text1.Text =""

    End Sub

    --------------------------------------------------------------------------------

    Private Sub a1_Click()

    Text1.Text = Text1.Text & a1.Caption

    End Sub

    --------------------------------------------------------------------------------

    Private Sub a2_Click()

    Text1.Text = Text1.Text & a2.Caption

    End Sub

    --------------------------------------------------------------------------------

    Private Sub a3_Click()

    Text1.Text = Text1.Text & a3.Caption

    End Sub

    --------------------------------------------------------------------------------

    Private Sub a4_Click()

    Text1.Text = Text1.Text & a4.Caption

    End Sub

    --------------------------------------------------------------------------------

    Private Sub a5_Click()

    Text1.Text = Text1.Text & a5.Caption

    End Sub

    --------------------------------------------------------------------------------

    Private Sub a6_Click()

    Text1.Text = Text1.Text &a6.Caption

    End Sub

    --------------------------------------------------------------------------------

    Private Sub a7_Click()

    Text1.Text = Text1.Text & a7.Caption

    End Sub

    --------------------------------------------------------------------------------

    Private Sub a8_Click()

    Text1.Text = Text1.Text & a8.Caption

    End Sub

    --------------------------------------------------------------------------------

    Private Sub a9_Click()

    Text1.Text = Text1.Text & a9.Caption

    End Sub

    --------------------------------------------------------------------------------

    Private Sub a10_Click()

    Text1.Text = Text1.Text & a10.Caption

    End Sub

    --------------------------------------------------------------------------------

    Private Sub a11_Click()

    Text1.Text =Text1.Text & a11.Caption

    End Sub

    --------------------------------------------------------------------------------

    Private Sub a12_Click()

    b = Text1.Text

    index = 0

    Select Case op

    Case +

    result = CDbl(a) + CDbl(b)

    Text1.Text = CStr(result)

    Case -

    result = CDbl(a) - CDbl(b)

    Text1.Text = CStr(result)

    Case *

    result = CDbl(a) * CDbl(b)

    Text1.Text = CStr(result)

    Case /

    If CDbl(b) = 0

    Then Text1.Text = error

    Else

    result = CDbl(a) / CDbl(b)

    Text1.Text = CStr(result)

    End If

    End Select

    If Text1.Text = error Then Text1.Text = CStr(result)

    End Sub

    --------------------------------------------------------------------------------

    Private Sub b1_Click()

    If index = 0 Then a = Text1.Text

    index = index + 1

    2829f2ca24f0d0090cbba57f30759f23.png

    VB教程:命令按钮(CommandButton).doc

    下载Word文档到电脑,方便收藏和打印[全文共2409字]

    编辑推荐:

    8b95f2eb3d3f7ce4dc3bf1178c74941e.png

    8b95f2eb3d3f7ce4dc3bf1178c74941e.png

    8b95f2eb3d3f7ce4dc3bf1178c74941e.png

    8b95f2eb3d3f7ce4dc3bf1178c74941e.png

    8b95f2eb3d3f7ce4dc3bf1178c74941e.png

    下载Word文档

    展开全文
    weixin_39776239 2021-07-16 00:28:38
  • 3KB nmanszx 2018-01-12 14:00:11
  • hljqfl 2019-01-07 17:39:12
  • qq_39586925 2019-05-03 13:32:49
  • bskls 2019-10-13 08:56:16
  • 4KB weixin_39840914 2019-07-10 09:34:37
  • GKQQQ 2017-06-22 21:30:44
  • 5星
    39KB weitian68 2010-06-07 14:13:35
  • TXH0001 2014-07-29 12:38:02
  • laocooon 2021-06-17 07:44:55
  • 2KB zhping_8109 2011-04-23 16:29:55

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 8,194
精华内容 3,277
关键字:

move方法vb