不知道你要利用VB还是Word的VBA实现,最方便的方法是利用Word的宏,首先打开模板,然后
开始录制宏(alt+f8),执行你需要程序做的操作,然后停止,打开VBA编辑器,就可以看到
你录制的宏相对应得VBA代码,这些代码如果是在VBA中使用,基本不需要做修改,在VB中使用
也只需要做小的改动,例如Word初始化等等。

解决方案 »

  1.   

    这个问题我曾经遇到过,我们实现的方法是,在你要插入的地方打上一些特殊的字符。然后在word中建立宏,这个宏的作用就是做替换。
    我想剩下的你就应当知道了吧!!不同的东西对应不同的背替换内容。怎么样,但愿以上内容对你有帮助!
      

  2.   

    关键问题是在模板的建立上,可以采用以下的方法:
    通过在模板中建立书签的方式标记所有用户可能需要设置的文本插入点,在wizard中由用户给标签设置插入的文本,字体等等,然后在程序打开模板,控制在书签中写入对应的内容并设置格式。
      

  3.   

    我们需要考虑不曾安装MS word的用户 chsl918(二雷)说的方法是我们正使用的,但是工作量太大,有没有更简便好用的算,请高手指点!!!
      

  4.   

    其实你的想法有点过了,你想如果不安装 Word但是你还想进行Word的应用这个复合Ms的销售政策吗?
    不过可以给你想一个折中的方案其实也是工作量不小的。前提是不用安装Word!
    就是将所有需要生成的文章生成一个文本文件当然这个文本文件中还有一些只有你的程序能读懂的信息,比如字体、字号等等。然后再到其他有Word的机器上进行合成,这样就可以实现你的想法了,不过你要在没有Word的机器上应用Word我想这不是一个好主意!小心Ms的人和你拼命!在我写以上方案时,我想可以借助HTML语言。因为word支持HTML,而HTML又是文本的东东,所以我想你可以先生成一个网页文件(其中那些控制不用说了吧,其实我也不会反正就是加上那些 <html></html>东西)然后再用Word打开就可以,怎么样是不是想请我当你的顾问了?哈哈!!祝你好运!!!!
      

  5.   

    to chsl918(二雷)
    怎么样是不是想请我当你的顾问了?哈哈!! ok, 没问题!
    有没有例子代码什么的? 看你这顾问够不够资格!! hehe 开玩笑,多谢!
    我的文档是RTF格式,不是word,所以不怕MS, hehe
      

  6.   

    哈哈当然没有资格了,如果有资格我就不会不给你贴代码了!!
    不过我想如果用html格式还真的能解决你的问题,因为那些东西都是文本操作,也不用到处找写法,最重要的是Word可以直接打开,字体、字号都可以设置,什么都是现成的,而且还不用Word的宏,越想越合适!
    难道文本操作的代码还要?去找msdn吧,因为我这里也没有,唉见笑了!
      

  7.   

    我作了一个test 是关于RTF的 HTML的还没考虑 如下:Dim CurrentFile As String
    Dim FoundIt As Boolean
    Dim Xstart As Integer
    Dim Xend As Integer
    Dim textSel As String
    Dim txtRate As String
    Dim txtNo As String
    Dim Rating(4) As Integer
    Dim Senten(7) As StringPrivate Sub ButtonEx1_Click()    ButtonEx1.Value = DownEnd SubPrivate Sub Command1_Click()
    On Error GoTo woops
    Dim sfile As String
    With CommonDialog1
        .Flags = cdlOFNAllowMultiselect + cdlOFNExplorer + cdlOFNFileMustExist + cdlOFNHideReadOnly
        .DialogTitle = "Open File"
        .CancelError = True 'if the user cancels sends us to woops
        .Filter = "Text files (*.rtf;*.txt)|*.rtf;*.txt|All files|*.*"
        .ShowOpen
        If Len(.FileName) = 0 Then Exit Sub
        sfile = .FileName
    End With
    RTB.LoadFile sfile
    CurrentFile = sfile 'used by the mod to save changes made
    woops: Exit Sub
    End SubFunction FindIt(RTF As RichTextBox, txt As String) As Integer
        RTF.Refresh
        'RTF.Find (Txt,0)
        textfound = RTF.Find(txt, 0)
        If textfound <> -1 Then
            FoundIt = True 'lets the form know we found it
            'RTF.SetFocus 'OK we've found it so select it to show the user
            FindIt = RTF.SelStart
            Exit Function 'lets' get out of here
        Else
            FoundIt = False 'lets the form know we cant find it
            Exit Function
        End If
    End Function
    Function FinditNext(RTF As RichTextBox, txt As String) As Integer
    'the text has already been found once. We want the next occurrence
      
      textfound = RTF.Find(txt, RTF.SelStart + Len(txt))
        If textfound <> -1 Then 'Found it !
            FoundIt = True 'let the form know we succeeded
            'RTF.SetFocus 'show it to the user
            FinditNext = RTF.SelStart
            Exit Function 'lets bail out now
        Else
            FoundIt = False 'let the form know we cant find it
            Exit Function 'lets bail out now
        End IfEnd FunctionPrivate Sub Command2_Click()
    Xstart = FindIt(RTB, txtFind.Text)End SubPrivate Sub Command3_Click()
    Xend = FinditNext(RTB, txtFind.Text)
    End SubPrivate Sub Command4_Click()
    RTB.SelStart = Xstart
    RTB.SelLength = Xend + 4 - Xstart
    RTB.SelBold = True
    RTB.SelColor = &HFFC0FF
    RTB.SelUnderline = True
    RTB.SelText = "I have changed the comapny Name"
    'RTB.Visible = True
    End SubPrivate Sub Command5_Click()
    Label2.Caption = "Processing Please wait........."
    Dim tempTxt As String
    Dim temSent As String
    Dim i As Integer
    Dim j As Integer
    For j = 1 To 6
    tempTxt = "/PCI" & j & "R#" & txtFind.Text & "\"
    BoldOpt RTB, tempTxt
    tempTxt = "/PCI" & j & "R#"
    ClearMark RTB, 5, tempTxt
    tempTxt = "/PCI" & j & "N#" & Rating(txtFind.Text - 1) & "\"
    BoldOpt RTB, tempTxt
    tempTxt = "/PCI" & j & "N#"
    ClearMark RTB, 10, tempTxt
    For i = 1 To 8
        tempTxt = "/PCI" & j & "S#" & i & "\"
        temSent = Senten(i - 1)
        FillSent RTB, tempTxt, temSent
    Next i
    Next j
    DoEvents
    RTB.Visible = True
    End Sub
    Sub ClearMark(RTF As RichTextBox, TotlNo As Integer, txt As String)
        Dim txtTemp As String
        Dim txtSel As String
        Dim StartP As Long
        Dim EndP As Long
        Dim L As Long
        Dim i As Integer
        For i = 1 To TotlNo
            txtTemp = txt & i & "\"
            L = Len(txtTemp)
            StartP = FindIt(RTF, txtTemp)
            If FoundIt = True Then
                EndP = FinditNext(RTF, txtTemp)
                RTF.SelStart = StartP + L
                RTF.SelLength = EndP - L - StartP
                txtSel = RTF.SelText
                RTF.SelStart = StartP
                RTF.SelLength = EndP + L - StartP
                RTF.SelText = txtSel
                
            End If
        Next i
    End SubPrivate Sub Command6_Click()
    On Error GoTo woops
    Dim ssfile As String
    With CommonDialog1
        .DialogTitle = "Save File"
        .CancelError = True 'if the user cancels sends us to woops
        .Filter = "Text files (*.rtf;*.doc)|*.rtf;*.doc|All files|*.*"
        .ShowOpen
        If Len(.FileName) = 0 Then Exit Sub
        ssfile = .FileName
    End With
    RTB.SaveFile ssfile
    'CurrentFile = sfile 'used by the mod to save changes made
    woops: Exit Sub
    End SubPrivate Sub Form_Load()
    Timer1.Enabled = False
    'RTB.Visible = False
    Rating(0) = 10
    Rating(1) = 8
    Rating(2) = 6
    Rating(3) = 3
    Rating(4) = 1
    'Senten(0) = "  "
    'Senten(1) = "  "
    'Senten(2) = "  "
    'Senten(3) = " 6 "
    'Senten(4) = "  "
    'Senten(5) = " 4.2 "
    'Senten(6) = " 4.2 "
    'Senten(7) = "  "End SubSub BoldOpt(RTF As RichTextBox, txt As String)
        Dim StartP As Long
        Dim EndP As Long
        Dim L As Integer
        Dim txtSel As String
        StartP = FindIt(RTF, txt)
        EndP = FinditNext(RTF, txt)
        If FoundIt = False Then Exit Sub
        L = Len(txt)
        RTF.SelStart = StartP + L
        RTF.SelLength = EndP - L - StartP
        txtSel = RTF.SelText
        
        RTF.SelStart = StartP
        RTF.SelLength = EndP + L - StartP
        RTF.SelBold = True
        
        RTF.SelText = txtSel
        
    End Sub
    Sub FillSent(RTF As RichTextBox, txt As String, txtChange As String)
        Dim StartP As Long
        Dim EndP As Long
        Dim L As Integer
        
        StartP = FindIt(RTF, txt)
        If FoundIt = False Then Exit Sub
        EndP = FinditNext(RTF, txt)
        L = Len(txt)
        RTF.SelStart = StartP
        RTF.SelLength = EndP + L - StartP
        'RTF.SelBold = True
        RTF.SelUnderline = True
        RTF.SelText = txtChange
               
    End Sub
      

  8.   

    对不起,我对RTF一点都不了解,但是程序看了一下,实现起来好像要比HTML简单一点,也可能是我看得粗,我没有看到可以字体、字号的部分,如果这个方法可行我想应当比HTML来的快点。不过你都做成这个样子了再改成HTML有点可惜。尤其是替换都有了!真是天不从人愿啊。
    还是祝你成功,以你的功底改成HTML不成问题的!!祝你成功!!
    另外顾问我是没有脸当了!!你另请高明吧! *_* 
      

  9.   

    to chsl918(二雷)
    没写字体,字号的部份,就是插入,和将特定句子变成粗体。顾问当不当无所谓,是朋友就行了, 还是帮我考虑考虑。大家讨论讨论吗?给我点启发。
      

  10.   

    你那个东东我又看了看,好像是先读取一个RTF文件,这一步是不是有点不太合适?如果这个RTF文件读取成别的文件了,还要进行相关的判断比较麻烦,所以我想能不能那个东西作在VB里面,干脆全部生成,因为我想剩下的东西就是一些标题之类的固定东西了,所以我想在文件不多的情况下作在VB中便于管理,我想那些部分都是由Word之类的软件生成的直接赋值在字符程序的字符串中就成了!!这样也不用什么替换了!!直接用字符串加就得了!
    还是因为程序看得粗,不一定对,见谅!!
      

  11.   

    我的模板太大了-有一本书呢。 只有事先作成RTF,然后调用处理。
    but Thank U anyway.
      

  12.   

    另一个问题:
    http://www.csdn.net/Expert/topic/497/497652.shtm
      

  13.   

    TO:Mike_sun(漠风) 
    我刚刚生成了一个RTF文件的文档分析了一下,当然分析的不是特别透彻
    但是感觉上不如HTML来得简单,而且好像有VB下有控制HTML文件的控件,你可以找一找(VB本身好像就有)祝你早日解决问题