Excel宏-打开特定的word文件

发布于 2024-10-22 04:14:51 字数 1019 浏览 1 评论 0原文

我还没有找到任何可以帮助我的东西。

我正在尝试打开某个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 文件,将其粘贴到那里并以新名称保存。

非常感谢!

I haven't found anything that can help me.

I'm trying to open a certain word file, have some data written in it and saved under a different name. This is what I have so far:

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

At the moment all it does is; open the Standaard.docx file, open a new file and paste everything in the new file and saves. It should open the Standaard.docx file, paste it in there and save under a new name.

Many thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠北森屋 2024-10-29 04:14:51

它打开一个新文档的原因是因为您

appWD.Documents.Add

的代码中在该行之前有一行:

appWD.Selection.Paste

如果删除 appWD.Documents.Add Word 将粘贴到您的活动文档中(即“Standaard.txt”)。文档”)。

还有一点,您不需要该行:

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

因为您立即在其下面的行中使用以下命令初始化一个新的 Word 应用程序:

Set appWD = New Word.Application

The reason that it opens a new document is because you have the line:

appWD.Documents.Add

in your code before the line:

appWD.Selection.Paste

if you remove the appWD.Documents.Add Word will paste into your active document (i.e. "Standaard.docx").

Just one other point, you do not need the line:

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

as you immediately initialise a new Word application in the line below it with:

Set appWD = New Word.Application
话少心凉 2024-10-29 04:14:51

此宏打开一个文件,然后根据 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

This macro opens a file then saves it as a new file name in a different folder based on info updated in the sheet1 of the Excel file

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文