Excelmacros – 打开特定的文件
我还没有find任何可以帮助我的东西。
我试图打开一个特定的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文件,粘贴在那里,并保存在一个新的名称。
非常感谢!
它打开一个新文件的原因是因为你有一行:
appWD.Documents.Add
在你的代码行之前:
appWD.Selection.Paste
如果删除appWD.Documents.Add
Word将粘贴到您的活动文档(即“Standaard.docx”)。
还有一点,你不需要这条线:
Set appWD = CreateObject("Word.Application.8")
当您立即在下面的行中初始化一个新的Word应用程序时:
Set appWD = New Word.Application
该macros将打开一个文件,然后根据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