2009-04-17 61 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