use warnings;
use strict;

use Win32::OLE;

my $word = CreateObject Win32::OLE 'Word.Application' or die $!;
$word->{'Visible'} = 1;

my $document = $word->Documents->Add;

my $selection = $word->Selection;

$selection -> TypeText("大家好");
$selection -> TypeParagraph;
$selection -> TypeText("How do you feel today");
$selection -> TypeParagraph;

$document->saveas('D:\深未来\测试\xxxxx.doc')   ;
$word->quit();

具体说明:

Saving Word 2007 Documents to PDF and XPS Formats<!---->

<!--Content type: DocStudio. Transform: devdiv2mtps.xslt.-->

<!----><!--src=[..\local\2b72d016-a0b3-49b4-ad23-0e4925c20222.gif]-->

Applies to: 2007 Microsoft Office System, Microsoft Office Word 2007

Joel Krist, Akona Systems

April 2007

<!---->

<!--src=[..\local\97421a3c-b771-4007-988f-496df6b850d3.gif]-->

The 2007 Microsoft Office Add-in: Microsoft Save as PDF and 2007 Microsoft Office Add-in: Microsoft Save as XPS allow a Microsoft Office Word 2007 to export and save documents in the PDF and XPS formats. This article illustrates how to use the Microsoft Word 12.0 Object Library to access the Word 2007 Document.ExportAsFixedFormat method to programmatically convert an existing Word 2007 document to either the PDF format or the XPS format.

<!--src=[..\local\ea6729f8-0f4e-41ff-bbec-a743cfa2929c.gif]-->

Download the Code Sample

To illustrate how to programmatically save a Word 2007 document to either the PDF format or the XPS format, this section walks through five key steps.

To programmatically save a Word 2007 document to either the PDF format or the XPS format

  1. Add a reference to the Word 12.0 Object Library.

  2. Import the Word 2007 interop assembly namespace.

  3. Create an instance of the ApplicationClass object.

  4. Declare the appropriate variables

  5. Implement the conversion code.

1. Add a Reference to the Word 12.0 Object Library

First, add a reference to the Microsoft Word 12.0 Object Library to the Visual Studio project. To do this, right-click the project in the Visual Studio Solution Explorer and select the Add Reference… menu item. Select the COM tab in the Add Reference dialog box, then scroll down to the Microsoft Word 12.0 Object Library component, select it, and then click OK to add the reference.

Figure 1. Adding a Reference

<!--src=[..\local\ea56098a-41b8-461f-8970-0c0def5c26dc.gif]-->

2. Import the Word Interop Namespace

Next, import the Microsoft.Office.Interop.Word namespace. This allows objects that are defined in that namespace to be referred to without having to specify the fully qualified namespace path. To import the namespace, add the following line to the top of the source file.

Visual Basic
Copy Code

Imports Microsoft.Office.Interop.Word

C#
Copy Code

using Microsoft.Office.Interop.Word;

For Microsoft Visual Basic projects, you can also import the namespace by right-clicking the project in the Visual Studio Solution Explorer and selecting the Properties menu item. On the project properties page, select the References tab and then select the check box next to the Microsoft.Office.Interop.Word entry in the list of imported namespaces.

3. Create an Instance of the Word ApplicationClass Object

To work with the Word 2007 object model, create an instance of the Word 2007 top-level ApplicationClass object and declare a variable to hold the reference to the document.

Visual Basic
Copy Code

Dim wordApplication As ApplicationClass = New ApplicationClass()
Dim wordDocument As Document = Nothing

C#
Copy Code

ApplicationClass wordApplication = new ApplicationClass();
Document wordDocument = null;

4. Declare Appropriate Variables

The following code blocks declare variables that help to make the parameters that are passed to methods used in the conversion code easier to read. The following variables are used with the Documents.Open method and ApplicationClass.Quit method.

Use the paramSourceDocPath variable to specify the path and filename of the Word 2007 document that is to be exported to either the PDF format or the XPS format.

Use the paramMissing variable when calling methods that accept optional parameters. Optional parameters are only optional when you use Microsoft Visual Basic. You must specify a value for optional parameters when you use Microsoft Visual C#. Using Type.Missing as the value for an optional parameter indicates that the parameter is not specified and that the method should use the parameter's default value.

Visual Basic
Copy Code

Dim paramSourceDocPath As String = "C:\Temp\Test.docx"

C#
Copy Code

object paramSourceDocPath = @"C:\Temp\Test.docx";
object paramMissing = Type.Missing;

The following variables are used with the Document.ExportAsFixedFormat method. The paramExportFormat variable is important because it is used to specify the format in which to export the document. The paramExportFormat variable is of the WdExportFormat type. It is an enumerated type which has two values, wdExportFormatXPS and wdExportFormatPDF. The following sample code sets the paramExportFormat variable to the WdExportFormat.wdExportFormatXPS value to export a document to the XPS format. To change the code to export a document in the PDF format, set the variable to the WdExportFormat.wdExportFormatPDF value. For more information on the ExportAsFixedFormat method and the parameters that it accepts, see Docment.ExportAsFixedFormat.

Visual Basic
Copy Code

Dim paramExportFilePath As String = "C:\Temp\Test.xps"
Dim paramExportFormat As WdExportFormat = _WdExportFormat.wdExportFormatXPS
Dim paramOpenAfterExport As Boolean = False
Dim paramExportOptimizeFor As WdExportOptimizeFor = _WdExportOptimizeFor.wdExportOptimizeForPrint
Dim paramExportRange As WdExportRange = _WdExportRange.wdExportAllDocument
Dim paramStartPage As Int32 = 0
Dim paramEndPage As Int32 = 0
Dim paramExportItem As WdExportItem = _WdExportItem.wdExportDocumentContent
Dim paramIncludeDocProps As Boolean = True
Dim paramKeepIRM As Boolean = True
Dim paramCreateBookmarks As WdExportCreateBookmarks = _WdExportCreateBookmarks.wdExportCreateWordBookmarks
Dim paramDocStructureTags As Boolean = True
Dim paramBitmapMissingFonts As Boolean = True
Dim paramUseISO19005_1 As Boolean = False

C#
Copy Code

string paramExportFilePath = @"C:\Temp\Test.xps";
WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatXPS;
bool paramOpenAfterExport = false;
WdExportOptimizeFor paramExportOptimizeFor =WdExportOptimizeFor.wdExportOptimizeForPrint;
WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;
bool paramDocStructureTags = true;
bool paramBitmapMissingFonts = true;
bool paramUseISO19005_1 = false;

5. Implement the Conversion Code

Next add code that opens the source document, exports it to the specified format, and exits Word 2007. Making the call to the Document.ExportAsFixedFormat method throws an exception if the add-in for the format is not currently installed. To handle this situation, the conversion code is wrapped in a Try…Catch block. The code that exits Word 2007, which allows it to unload from memory, is located in a Finally block. The shutdown-related code closes the Word 2007 document, exits the Word 2007 application, releases references to the underlying Word 2007 COM objects, and makes calls to the .NET garbage collector. For more information about how to release COM objects when you use managed code, see Chapter 2: Basics of Office Interoperability (Part 2 of 3) from the book Microsoft .NET Development for Microsoft Office.

Visual Basic
Copy Code

Try' Open the source document.wordDocument = wordApplication.Documents.Open(paramSourceDocPath)' Export it in the specified format.If Not wordDocument Is Nothing ThenwordDocument.ExportAsFixedFormat(paramExportFilePath, _paramExportFormat, paramOpenAfterExport, _paramExportOptimizeFor, paramExportRange, paramStartPage, _paramEndPage, paramExportItem, paramIncludeDocProps, _paramKeepIRM, paramCreateBookmarks, _paramDocStructureTags, paramBitmapMissingFonts, _paramUseISO19005_1)End If
Catch ex As Exception' Respond to the error
Finally' Close and release the Document object.If Not wordDocument Is Nothing ThenwordDocument.Close(False)wordDocument = NothingEnd If' Quit Word and release the ApplicationClass object.If Not wordApplication Is Nothing ThenwordApplication.Quit()wordApplication = nothingEnd IfGC.Collect()GC.WaitForPendingFinalizers()GC.Collect()GC.WaitForPendingFinalizers()
End Try

C#
Copy Code

try
{// Open the source document.wordDocument = wordApplication.Documents.Open(ref paramSourceDocPath, ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing, ref paramMissing,ref paramMissing, ref paramMissing, ref paramMissing,ref paramMissing);// Export it in the specified format.if (wordDocument != null)wordDocument.ExportAsFixedFormat(paramExportFilePath,paramExportFormat, paramOpenAfterExport, paramExportOptimizeFor, paramExportRange, paramStartPage,paramEndPage, paramExportItem, paramIncludeDocProps, paramKeepIRM, paramCreateBookmarks, paramDocStructureTags, paramBitmapMissingFonts, paramUseISO19005_1,ref paramMissing);
}
catch (Exception ex)
{// Respond to the error
}
finally
{// Close and release the Document object.if (wordDocument != null){wordDocument.Close(ref paramMissing, ref paramMissing,ref paramMissing);wordDocument = null;}// Quit Word and release the ApplicationClass object.if (wordApplication != null){wordApplication.Quit(ref paramMissing, ref paramMissing,ref paramMissing);wordApplication = null;}GC.Collect();GC.WaitForPendingFinalizers();GC.Collect();GC.WaitForPendingFinalizers();
}

<!--src=[..\local\3015c8ed-3644-4fc7-b31a-440e0c377ad0.gif]-->

There are a number of scenarios in which you may want to develop a Microsoft Office business application that requires both dynamic and static views of the same document. In Word 2007 you can use the 2007 Microsoft Office Add-in: Microsoft Save as PDF and 2007 Microsoft Office Add-in: Microsoft Save as XPS to save a Word 2007 document as either a PDF document or an XPS document.

The key object that is used to save a Word 2007 document in either the PDF format or the XPS format is the Document object. The Document object has a method called Document.ExportAsFixedFormat, which has a number of key parameters that it accepts to save the target document in the desired format. This article specifically explores how to use the Documents.Open method and Document.ExportAsFixedFormat method to programmatically save a Word 2007 document as either a PDF document or an XPS document.

To programmatically save a Word 2007 document as either a PDF document or an XPS document

  1. Add a reference to the Word 12.0 Object Library to the project. This marks that the project uses the Word 12.0 Object Library.

  2. Import the Microsoft.Office.Interop.Word namespace. This allows code to use the classes and types exposed as part of the Microsoft.Office.Interop.Word namespace without having to specify the fully qualified namespace path.

  3. Create an instance of the Word 2007 ApplicationClass object. This is the top-most class in the Word 2007 object model hierarchy, and is the starting point for working with the other classes in the object model.

  4. Declare variables to help with method calls. This helps to make the parameters that are passed to methods used in the conversion code easier to read.

  5. Implement the conversion code. The final step provides code to implement the programmatic conversion.

<!--src=[../local/note.gif]-->Note:
The preceding example code used the Type.Missing object to specify that an optional parameter was not provided and that the default parameter value should be used. To change the behavior to use something other than the default parameter types and values, specify the appropriate parameter types and values. For more information about the Documents.Open method and Document.ExportAsFixedFormat method and the parameters that they accept, see the Word 2007 Object Model Reference.<!---->

<!---->

Microsoft 提供的编程示例只,用于说明不附带任何明示或暗示保证。这包括,但不限于对适销性或针对特定用途的适用性的暗示的担保。本文假定您熟悉演示了正在使用的编程语言以及用于创建和调试过程的工具。Microsoft 支持工程师可以帮助解释某个特定过程的功能,但他们不会修改这些示例以提供额外的功能或构建过程来满足您的具体要求。

自动化 (以前称为 OLE 自动化) 是程序用来公开它们的对象的开发工具、 宏语言和其他支持自动化的程序功能。例如对于电子表格程序可能会使工作表、 图表、 单元格或区域的单元格,每个为不同类型的对象。字处理器可能会公开对象 (如应用程序、 文档、 段落、 句子、 一个书签或所选内容。

当一个程序支持自动化时可以使用 Visual Basic 应用程序的访问它公开的对象。为应用程序操作这些对象在 Visual Basic 中的,通过调用方法在对象上或通过获取并设置该对象的属性。

可以使用代码示例在这篇文章中来控制 Word 从 Microsoft Access 2000、 Microsoft Excel 2000、 Microsoft PowerPoint 2000、 Microsoft Visual Basic 应用程序,或来控制 Word 支持自动化的任何其他客户端。

回到顶端

入门 》

<script type="text/javascript"></script> 有 Word for Windows 自动执行的四个主要步骤:

  1. 添加到 Microsoft Word 9.0 对象库 的引用。
  2. 将变量声明为一个 Word 对象类型 (应用程序、 文档,和 so on)。
  3. 分配给您在步骤 2 中声明该对象变量 CreateObject 函数返回该对象。
  4. 使用属性和方法的对象变量来使 Word 自动运行。
回到顶端

第 1 步: 添加到 Word 9.0 对象库的引用

<script type="text/javascript"></script> 若要将对使用 Microsoft Access 2000、 Microsoft PowerPoint 2000,或 Microsoft Excel 2000 中,Microsoft Word 9.0 对象库 的引用,请按照下列步骤操作:

  1. 在 Microsoft Access、 PowerPoint,或 Excel 工具 菜单上的指向 ,然后单击 Visual Basic 编辑器
  2. 在 Visual Basic 编辑器中,在 工具 菜单上单击 引用
  3. 可用引用 的列表,单击以选中 Microsoft Word 9.0 对象库 复选框。

: 添加该引用,使用 Microsoft Visual Basic 6.0 中,请在 项目 菜单上单击 引用

添加在 Microsoft Word 9.0 对象库 的引用允许您访问 Microsoft Word 联机帮助和 Microsoft Word vba 的应用程序的常数、 属性和方法的程序。注意若要直接自动化 Word 对象类型所需的 Microsoft Word 9.0 对象库 的引用。

添加到 Microsoft Word 9.0 对象库 的引用称为早期绑定。

有关对象绑定的详细信息请参阅 Microsoft 知识库中下面的文章:

138138  (http://support.microsoft.com/kb/138138/EN-US/ ) 信息: 最晚,ID,早期绑定类型可能在 VB 中为应用程序
回到顶端

步骤 2: 声明对象变量

<script type="text/javascript"></script> 若要声明对象变量,您变量就像维度任何变量,在于您可以声明该对象时指定类型的维度。例如对于 Word.Application文档,和 段落 是单独的 Word 对象。

下面的示例命令行声明为类型 Word.Application 的对象的变量 objWD:

Dim objWD as Word.Application

回到顶端

步骤 3: 设置变量

<script type="text/javascript"></script> 有两个 Visual Basic 函数,您可以使用"绑定"到 Word 的已声明的对象变量: CreateObjectGetObject。主要区别是 word 的 word 的 CreateObject 函数创建一个新的实例,并且 GetObject 函数使用现有,或已经正在运行实例。您还可以使用 GetObject 将您的对象变量绑定到特定的 Word 文档。

下面的示例命令行将 objWD 变量绑定到 Word 使用 CreateObject 函数:

Dim objWD as Word.ApplicationSet objWD = CreateObject("Word.Application")

的下面的示例命令行将 objWdDoc 变量绑定到特定的 Word 文档:

Dim objWdDoc As Word.DocumentSet objWdDoc = GetObject("c:\my documents\doc1.doc")

: 建议您在自动化 Word for Windows 使用仅 CreateObject 函数。如果型 WordMail 正在运行,或如果 Word 文档嵌入在另一个程序内,GetObject 函数可能导致出现不可预知的行为。

有关帮助您使用 vba 的应用程序的详细信息请参阅 Microsoft 知识库中下面的文章:

237338  (http://support.microsoft.com/kb/237338/EN-US/ ) WD2000: 如何使用自动化型 WordMail 检查
回到顶端

步骤 4: 使用属性和方法,可自动进行 Word

<script type="text/javascript"></script> 您完成步骤 1 到步骤 3 您可以使用对象变量来使 Word 自动运行。

下面的示例宏使用自动化创建 Word 对象、 创建新文档、 添加一些的文本和保存该文档。

Sub AutomateWord()' Declare the variable.Dim objWD As Word.Application' Set the variable (runs new instance of Word.)Set objWD = CreateObject("Word.Application")' Add a new document.objWD.Documents.Add' Add some text.objWD.Selection.TypeText "This is some text."' Save the document.objWD.ActiveDocument.SaveAs filename:="mydoc.doc"' Quit Word.objWD.Quit' Clear the variable from memory.Set objWD = Nothing
End Sub

: 下面的示例宏复制过程所述 AutomateWord 宏,并直接在 Word 中运行:

Sub WordMacro()Documents.AddSelection.TypeText "This is some text"ActiveDocument.SaveAs filename:="mydoc.doc"Quit
End Sub

回到顶端

参考

特定于自动化 Word 使用 Visual Basic 应用程序的详细信息,请参阅以下资源。 Microsoft Office 开发人员 Web 站点http:...

<script type="text/javascript"></script>

特定于自动化 Word 使用 Visual Basic 应用程序的详细信息,请参阅以下资源。

回到顶端

Microsoft Office 开发人员 Web 站点

<script type="text/javascript"></script>http://msdn.microsoft.com/office (http://www.msdn.microsoft.com/office)

回到顶端

新闻组

<script type="text/javascript"></script> 下面的对等新闻组是可以帮助您与其他用户的 Visual Basic 应用程序的交互:

microsoft.public.vb.ole.automation
回到顶端

知识库

<script type="text/javascript"></script> 有关帮助您使用 vba 的应用程序的详细信息请参阅 Microsoft 知识库中下面的文章:

226118  (http://support.microsoft.com/kb/226118/EN-US/ ) OFF2000: 为应用程序的 Visual Basic 编程资源

有关如何使用本文中示例代码的详细信息单击下面文章编号,以查看 Microsoft 知识库中相应的文章:

212536  (http://support.microsoft.com/kb/212536/EN-US/ ) OFF2000: 如何运行知识库文章中的示例代码
回到顶端

办公室助理

<script type="text/javascript"></script> 有关自动化,在 Visual Basic 编辑器中的详细信息在 帮助 菜单上单击 Microsoft Visual Basic 帮助、 在 Office 助手或应答向导中,键入 Communicating 与其他应用程序,然后单击 搜索 以查看相关主题。

回到顶端

http://msdn.microsoft.com/en-us/library/default.aspx

大家可以在以上找到方法

perl-操作ole,比如操作word相关推荐

  1. Perl通过WIN32 OLE来操作EXCEL

    最近要用Perl(通过Win32::OLE)来操作EXCEL,在网上没找到类似的内容,好不容易在一本书上找到点入门知识,再加上自己捣鼓了一通,总算有点眉目,在此做个备份,供大家和我一起参考 ^_^ # ...

  2. Perl读写Excel简单操作

    Perl读写Excel简单操作 使用模块 Spreadsheet::ParseExcel Spreadsheet::WriteExcel 读Excel #!/usr/bin/perl -wuse st ...

  3. perl mysql 数据推拉_MySQL_Perl操作mysql数据库的方法,Perl对Mysql的操作。 一、标准 - phpStudy...

    Perl对Mysql的操作. 一.标准操作 1.连接.关闭 my $dbh = DBI->connect("DBI:mysql:database=DBname;host=localho ...

  4. access里的多步oledb错误_多步 OLE DB 操作产生错误,这问题怎么解决啊

    相信大家在调试程序时曾碰到过下面错误 数据库:ACCESS ------------------------------------------------------------------- Mi ...

  5. 多步OLE DB操作产生错误

    "多步OLE DB操作产生错误.如果可能,请检查每个OLE DB状态值.没有工作完成." 多步:数据类型不符合 调整的数据库里的表.先选择学生表,右键"设计", ...

  6. Delphi OLE方法操作Excel

    Delphi OLE方法操作Excel  来源:http://www.ltesting.net/ceshi/ruanjianceshikaifajishu/rjcskfyy/2008/0519/154 ...

  7. JAVA hbase groupby_window操作和groupBy操作

    window操作 import spark.implicits._ val words = ... // streaming DataFrame of schema { timestamp: Time ...

  8. 悟透delphi 第十章 操作界面与操作逻辑

    本书原著李战(leadzen)大牛,由tingsking18整理,本人blog发布的版本经过战哥同意,转载请著名出处和原作者!第十章 操作界面与操作逻辑 我们在前面的曾经讨论过,用户界面与商业逻辑分离 ...

  9. 悟透delphi 第十章 操作界面与操作逻辑

    第十章操作界面与操作逻辑 我们在前面的曾经讨论过,用户界面与商业逻辑分离的好处.这样的分离可以让软件体系结构更加合理,结构易于理解,从而增强软件的灵活性和可维护性. 正如我谈到过,我们讨论的目的是为了 ...

  10. 实验1:熟悉常用的Linux操作和Hadoop操作

    注:完整实验报告word文件在末尾 --------------------------------- "大数据技术原理与应用"课程实验报告 题目:实验1:熟悉常用的Linux操作 ...

最新文章

  1. 最新发现6个高质量网站,让人眼前一亮!
  2. C++学习笔记35:函数模板
  3. cv::Mat 与 float 互换,实现 argmax 得到像素点分类
  4. 如何打造智能化的员工出行方式?阿里自研出行神器首次曝光
  5. 自己搭建的邮件系统不能发往gmail、hotmail等问题解决
  6. 荷兰牛栏 荷兰售价_荷兰的公路货运是如何发展的
  7. [vue] 你有看过vue的源码吗?如果有那就说说看
  8. CentOS6.9下ssh密钥登录配置步骤(免密码登录)和ssh-keygen 命令常用参数
  9. 在pycharm中使用docker安装的python3作为运行环境
  10. Acrobat Pro DC 教程,如何将 PDF 导出为 Microsoft Office 格式?
  11. 好搜独立,三分天下成历史
  12. 30套最实用JAVA学习视频教程合集 2012
  13. phpstudy php乱码,PHP_完美解决PHP中文乱码,一.首先是PHP网页的编码 1. php - phpStudy...
  14. linux ftp客户端 pasv 227,FileZilla客户端连接腾讯云FTP服务器时出现“227 Entering Passive Mode”...
  15. C++ CEF3加载高清图片崩溃
  16. 替代MP9486A 输入120V降压恒压IC方案 GPS防盗器IC方案
  17. (三十三 :2021.01.12)MICCAI 2016 追踪之论文纲要
  18. 操作系统中并发与并行的几个例子
  19. 最新医疗系统三测单控件(体温单控件)
  20. 微博相互关注互粉mysql表实现_数据库设计实现用户关注、被关注、互粉

热门文章

  1. HTML制作注册页面
  2. 从前端角度浅谈如何做好网站的SEO优化
  3. 辩论赛计算机软件更重要论据,善意的谎言辩论会正方观点要例子
  4. ESP8266 AT指令连接阿里云物联网平台
  5. wisdomsell-day4-模板技术
  6. 不用P图!用Python给头像加圣诞帽并制作成可执行软件!
  7. Cadence IC 617 虚拟机添加工艺库教程
  8. 携程编程大赛预赛第二场
  9. Maven setting配置文件
  10. Spatial Attention U-Net for Retinal Vessel Segmentation(ICPR 2020)