在Excel中创建一个宏,将打印Word文档而不打开它。

时间:2022-10-30 23:41:42

I have a document, mydocument.docx, and I want to print that from a button in an Excel sheet. Both are in the same folder.

我有一个文档mydocument.docx,我想从Excel工作表中的按钮打印它。两者都在同一个文件夹中。

I don't want users to see the word document. They just click the button in Excel and the printing of the Word file starts.

我不希望用户看到word文档。他们只需单击Excel中的按钮,即可开始打印Word文件。

This is actually very urgent and I am totally new to this, so if you can explain this in steps that would be so awesome.

这实际上非常紧迫,我对此完全是新手,所以如果你能以非常棒的步骤解释这一点。

I can create a button in Excel and make it open an empty vb. This is as much as I know.

我可以在Excel中创建一个按钮,使其打开一个空的vb。这就像我所知道的那样。

Thanks very much for your time.

非常感谢你花时间陪伴。

1 个解决方案

#1


5  

You can use the Word automation object model to gain programmatic access to Word.

您可以使用Word自动化对象模型获得对Word的编程访问。

In almost all cases, you'd be following these steps:

几乎在所有情况下,您都将遵循以下步骤:

  1. Create the Word application object.
  2. 创建Word应用程序对象。

  3. Open a document.
  4. 打开文档。

  5. Do something with the document.
  6. 对文档做一些事情。

  7. Close the document.
  8. 关闭文档。

  9. Quit the Word application.
  10. 退出Word应用程序。

Here is what the basic VBA code looks like:

以下是基本的VBA代码:

' Step 1
Dim objWord
Set objWord = CreateObject("Word.Application")
' Hidden window!
objWord.Visible = False
' Save the original printer, otherwise you will reset the system default!
Dim previousPrinter
Set previousPrinter = objWord.ActivePrinter
objWord.ActivePrinter = "My Printer Name"

' Step 2
Dim objDoc
Set objDoc = objWord.Documents.Open("C:\Test\SomeDocument.docx")

' Step 3 -- in this case, print out the document without any prompts
objDoc.PrintOut
' Restore the original printer
objWord.ActivePrinter = previousPrinter

' Step 4
objDoc.Close

' Step 5
objWord.Quit

#1


5  

You can use the Word automation object model to gain programmatic access to Word.

您可以使用Word自动化对象模型获得对Word的编程访问。

In almost all cases, you'd be following these steps:

几乎在所有情况下,您都将遵循以下步骤:

  1. Create the Word application object.
  2. 创建Word应用程序对象。

  3. Open a document.
  4. 打开文档。

  5. Do something with the document.
  6. 对文档做一些事情。

  7. Close the document.
  8. 关闭文档。

  9. Quit the Word application.
  10. 退出Word应用程序。

Here is what the basic VBA code looks like:

以下是基本的VBA代码:

' Step 1
Dim objWord
Set objWord = CreateObject("Word.Application")
' Hidden window!
objWord.Visible = False
' Save the original printer, otherwise you will reset the system default!
Dim previousPrinter
Set previousPrinter = objWord.ActivePrinter
objWord.ActivePrinter = "My Printer Name"

' Step 2
Dim objDoc
Set objDoc = objWord.Documents.Open("C:\Test\SomeDocument.docx")

' Step 3 -- in this case, print out the document without any prompts
objDoc.PrintOut
' Restore the original printer
objWord.ActivePrinter = previousPrinter

' Step 4
objDoc.Close

' Step 5
objWord.Quit