2011-03-15 189 views
1

我還沒有找到任何可以幫到我的東西。Excel宏 - 打開特定的word文件

我試圖打開某個文件,將一些數據寫入並保存在不同的名稱下。這是我到目前爲止:

Dim appWD As Word.Application 
Set appWD = CreateObject("Word.Application.8") 
Set appWD = New Word.Application 
Dim docWD As Word.Document 
Set docWD = appWD.Documents.Open("C:\Documents and Settings\Excel macro\Standaard.docx") 
appWD.Visible = True 
' 
' Data is selected and copied into "Design" 
' 
Copy all data from Design 
Sheets("Design").Select 
Range("A1:G50").Copy 
' Tell Word to create a new document 
appWD.Documents.Add 
' Tell Word to paste the contents of the clipboard into the new document 
appWD.Selection.Paste 
' Save the new document with a sequential file name 
Sheets("Sheet1").Select 
appWD.ActiveDocument.SaveAs Filename:=ThisWorkbook.Path & "/" & "TEST" & Range("C8").Text 
' Close this new word document 
appWD.ActiveDocument.Close 
' Close the Word application 
appWD.Quit 

目前它所做的只是;打開Standaard.docx文件,打開一個新文件並將所有內容粘貼到新文件中並保存。它應該打開Standaard.docx文件,將其粘貼到那裏並以新名稱保存。

非常感謝!

回答

2

,它會打開一個新的文件,是因爲你有行:

appWD.Documents.Add 
在你的代碼

前行:

appWD.Selection.Paste 

如果去掉appWD.Documents.Add Word將粘貼到您的活動文檔(即「Standaard.docx」)。

只是另一點,則無需行:

Set appWD = CreateObject("Word.Application.8") 

,你馬上初始化它下面的行一個新的Word應用程序具有:

Set appWD = New Word.Application 
+0

Wo rks精彩!非常感謝你 – CustomX 2011-03-15 11:40:33

0

這個宏打開一個文件然後根據Excel文件Sheet1中更新的信息將其另存爲新文件名在不同文件夾中

Sub OpenDocFileNewName() 


' 
' OpenDocFileNewName Macro 

' 
' 
    Set WordApp = CreateObject("Word.Application.8") 

Set WordDoc = WordApp.Documents.Open("C:\Users\mmezzolesta\Documents\_TestDataMerge\STANDARD.docx") 

WordApp.Visible = True 

' 
' 
'Save as new file name 

Sheets("Sheet1").Select 

WordApp.ActiveDocument.SaveAs Filename:=("C:\Users\mmezzolesta\Documents\_TestMailMergeAuto") & "/" & Range("A2") & "Standard-Grounding-" & Range("e2").Text 

WordApp.ActiveDocument.Close 

WordApp.Quit 

' 
' 
End Sub