2009-04-17 57 views
1

我正在写一个宏的MS词。
我需要宏来解析文件名,页码和注释的列表,并只过滤掉文件名和页码。文档中的每个段落(行)引用了不同的文件,因此我正在循环访问For/Next语句。如何附加到Word文档与VBA宏

对于每一个新行,我拉出文件名和pagenumbers并将其放入一个字符串。除此之外,我还在每个文件名的字符串中添加了一些注释。

在转到文档的下一行之前,我想输出我已经构建到word文档中的字符串。

我目前拥有的word文档,此代码开放:

Dim oWord as Object 
Set oWord = CreateObject("Word.Application") 
oWord.Documents.Open "C:\document.doc" 
oWord.visible = true 

这让我成功地打开该文档,但我需要一些帮助,如何输出搞清楚这个文件。

从概念上讲,我知道我需要先将它变为活动文档,然后转到文档的末尾,然后追加到文档中。

任何帮助,将不胜感激。谢谢!

回答

5

这是怎么回事...更多here

Sub test() 
    Dim app As Word.Application 
    Dim doc As Word.Document 

    Set app = CreateObject("Word.Application") 
    app.Visible = True 
    Set doc = app.Documents.Open("C:\test.doc") 
    doc.Content.InsertAfter "Hello World" 
    doc.Save 
    doc.Close 
    app.Quit 
End Sub 
0

这将帮助您遍历文件列表中给定的目录

Sub ProcessDocs() 
    Dim rng As Range 
    Dim MainDoc As Document 
    Dim strFile As String 
    Const strFolder = "d:\Userfiles\yourname\testFiles\" 'change to suit 
    Set MainDoc = Documents.Add 
    strFile = Dir$(strFolder & "*.doc") ' can change to .docx 
    Do Until strFile = "" 
     'Extract your filename, pagenum here, and build a string 
     'write string into the file 
     strFile = Dir$() 
    Loop 

虽然循环,您可以提取文件名等; 建立你的字符串并将其写入文件。 我希望它有帮助

+0

请注意解释downvote! – Mehz 2016-08-10 10:55:00