怎样在WORD模板指定位置中添加文字
  
 用VB编程实现在界面上输入的内容输出到WORD文档中设定好的指定位置上。
dim appwd As Word.Application
dim mydoc As Word.Document
dim arange As Word.Range
dim aselection As Word.SelectionSet appwd = CreateObject("word.application") '创建 word application 对象
With appwd 
.Visible = False
Set mydoc = .Documents.Open(fpath) '调用word文档
mydoc.Activate
Set aselection = .Selection
Set arange = .ActiveDocument.Range(0, 1)
'Range(0, 1)为选取的插入点,须根据情况而定
arange.Select
arange.InsertAfter (mytext)
'或 arange.Insertbefore (mytext)'mytext为用VB编程实现在界面上输入的内容
  

解决方案 »

  1.   

    楼上兄弟有这样的例子么,给我发一分,[email protected],如果真的有帮助的话,分数全部送上,楼下的也一样呀
      

  2.   

    我关键的是想知道,word文档模版怎么定义呀,
    是类似这样的么 :
           文档定义:{     } 
    大括号对应一个域还有比如我要实现某一个域可能输入可能不输入,某一些域还可能存在循环,这都是值得分析的点
      

  3.   

    我现在急需一个word文档模版定义的例子,请达人指点,切切
      

  4.   

    这里有一个函数'*******************************
    '在表格的特定行的特定列插入特定域
    '*******************************Public Sub addFormFieldinTable(ByRef objDoc, ByRef addTable As Object, intRow As Integer, lngPosition As Long, lngClass As Long, _
                                         lngType As Long, _
                                   Optional strName As String = "", _
                                   Optional strDefault As String = "", _
                                   Optional strFormat As String = "", _
                                   Optional strFunction As String = "", _
                                   Optional lngWidth As Long = 0, _
                                   Optional exitMacro As String, _
                                   Optional bolIfEnable As String = "true", Optional strResult As String = "")
         
         Dim fidAdd As Object
         Dim inttype As Integer
         Dim bolEnable As Boolean
         Dim objSelection As Object
         Dim objRange As Object
         Dim intClass As Integer
         
         Const c_RegularText = 0
         Const c_NumberText = 1
         Const c_CalculationText = 5
         Const c_DateText = 2
         
         Const c_TextFormField = 70
         Const c_SelectFormField = 71
         
         Dim m_Strfunction
         
         
         bolEnable = True
         
         Select Case lngClass
              Case 1
                  intClass = c_TextFormField
              Case 2
                  intClass = c_SelectFormField
         End Select
         
         Select Case lngType
             Case 1
               inttype = c_RegularText
             Case 2
               inttype = c_NumberText
             Case 3
               inttype = c_CalculationText
               bolEnable = False
               If strFunction = "" Then
               
               Else
                  strDefault = strFunction
               End If
             Case 4
               inttype = c_DateText
         End Select
         
         If bolIfEnable = "false" Then
            bolEnable = False
         End If
            
            
            
            addTable.Cell(intRow, 1).Select
            objDoc.Application.Selection.Move Unit:=12, Count:=lngPosition - 1  '12 -->wdcell
            Set objRange = objDoc.Application.Selection.range
         
         Set fidAdd = objDoc.FormFields.Add(range:=objRange, _
                                                  Type:=intClass)   '70 -->wdFieldFormTextInput
             With fidAdd
                  
                  If strName = "" Then
                  
                  Else
                      .Name = strName
                  End If
                  
                  If lngClass = 1 Then
                     .TextInput.EditType Type:=inttype, Default:=strDefault, _
                                         Format:=strFormat, Enabled:=bolEnable
                                        
                     If lngWidth <> 0 Then
                         .TextInput.Width = lngWidth
                     End If
                     .CalculateOnExit = True
                  
                     If exitMacro <> "" Then
                        .exitMacro = exitMacro
                     End If
                  
                     If strResult <> "" Then
                        .Result = strResult
                     End If
                  
                  ElseIf lngClass = 2 Then
                     If exitMacro <> "" Then
                        .exitMacro = exitMacro
                     End If
                     If strResult <> "" Then
                        If strResult = "1" Then
                           .CheckBox.Value = True
                        Else
                           .CheckBox.Value = False
                        End If
                     End If
                     .Enabled = bolEnable
                  End If
                  
                  
             End With
       
    End Sub
      

  5.   

    我的程序中不使用表格,另外,word中的那个域功能能用来定制模版么,怎么样和
    vba关联,好像word的书签功能能够用来插入数据单现在不知道怎么用,谁能提供这两种方法任意一种都可以,不过谢谢楼上
      

  6.   

    我的程序中不使用表格,另外,word中的那个域功能能用来定制模版么?怎么样和
    vba关联,好像word的书签功能能够用来插入数据,但现在不知道怎么用,谁能提供这两种方法任意一种都可以,不过谢谢楼上
      

  7.   

    我以上所说的,只是一种类型的函数,如果你没用表格,用了书签,你可以
    用如下代码,
    call InsertOneBook objdoc,"aaa","bbb",strerror
    其中aaa是书签名,bbb是书签内容
    函数如下:
    Public Function InsertOneBook(ByRef objDoc, ByVal strBookMark As String, _
                                     ByVal strText As String, ByRef strError As String) As Boolean
        Dim objMarks As Object
        Dim BookRange As Object
        
        InsertOneBook = False
            
        Set objMarks = objDoc.Books
        
        If Not objMarks.Exists(strBookMark) Then
            strError = "没有找到BOOKMARK:" & strBookMark
            GoTo ExitHandle
        End If
        
        Set BookRange = objMarks(strBookMark).range
        BookRange.Text = strText
        objMarks.Add strBookMark, BookRange
                    
        InsertOneBook = True
    ExitHandle:
        Set objMarks = Nothing
        Set BookRange = NothingEnd Function
    我写的一个通用组件,简直就是给你量体裁衣做的
    如果你想了解较全面的内容,可以看一下我今天发的关于WORD通用组件的贴子