VBA之Word应用第三章第九节:Document.SaveAs2方法的利用实例

360影视 动漫周边 2025-05-07 17:53 3

摘要:《VBA之Word应用》(版权10178982),是我推出第八套教程,教程是专门讲解VBA在Word中的应用,围绕“面向对象编程”讲解,首先让大家认识Word中VBA的对象,以及对象的属性、方法,然后通过实例让大家感受到Word VBA 的妙处。

《VBA之Word应用》(版权10178982),是我推出第八套教程,教程是专门讲解VBA在Word中的应用,围绕“面向对象编程”讲解,首先让大家认识Word中VBA的对象,以及对象的属性、方法,然后通过实例让大家感受到Word VBA 的妙处。

这套教程是专门针对WORD VBA 的教程,是VBA中的稀缺资源,我给这套教程分归为中级教程,希望大家在VBA入门后再学习这套教程,这样会更加深入的理解面向对象编程的意义。

本套教程共三册十六章,今日内容为:VBA之Word应用第三章第九节:Document.SaveAs2方法的利用实例

【分享成果,随喜正能量】136 人活得累,是因为能左右你心情的东西太多。天气的变化,人情的冷暖,不同的风景都会影响你的心情。因此只有看淡了,天无非阴晴,人不过聚散,沧海桑田,我心不惊,自然安稳。活在当下,并安于自已的选择,为自已的每一个选择负完全的责任,便是晴天。。

大家好,我们继续Word VBA的讲解。这讲开始,我们进入非常重要一章的学习,文档集合Documents对象及文档Document对象。在前面的讲解中我们可以大概看出文档和文档集合在Word VBA中的作用,很多的对象都是文档对象属性的返回对象,我们通过这章的学习要掌握Word VBA的基本应用。

大家好,我们这节继续Document对象方法的讲解,这节的内容是第七节内容的继续,对于任何对象来讲,都是我们在代码中利用的高效工具。

这种方法将保存指定的文档。

语法:expression.Save

其中:expression 代表一个 Document对象

如果为true,则 Word 将自动保存所有文档。 如果为false,则 Word 会提示用户保存自上次保存以来已更改的每个文档。

使用新的名称或格式保存指定的文档。此方法的一些参数与 “另存为” 对话框(“文件” 选项卡)中的选项相对应。

语法:

expression.SaveAs2(FileName,FileFormat,LockComments,Password,AddToRecentFiles, WritePassword,ReadOnlyRecommended, EmbedTrueTypeFonts, SaveNativePictureFormat, SaveFormsData,SaveAsAOCELetter,Encoding,InsertLineBreaks,AllowSubstitutions,LineEnding, AddBiDimarks, CompatibilityMode)

其中:expression 代表一个 Document对象

参数:

1)FileName 可选 Variant 文档的名称。 默认值为当前文件夹和文件名。 如果从未保存过文档,将使用默认名称(例如,Doc1.doc)。 如果已经存在具有指定文件名的文档,则覆盖该文档,并且在覆盖前不提示用户。

2)FileFormat 可选 Variant 文档的保存格式。可以是任何 WdSaveFormat 常量。 若要以另一种格式保存文档,请为 FileConverter 对象的 SaveFormat 属性指定适当的值。

WdSaveFormat 常量的枚举值如下:

1)wdFormatDocument 0 Microsoft Office Word 97 - 2003 binary file format.

2)wdFormatDOSText 4 Microsoft DOS text format.

3)wdFormatDOSTextLineBreaks 5 Microsoft DOS text with line breaks preserved.

4)wdFormatEncodedText 7 Encoded text format.

5)wdFormatFilteredHTML 10 Filtered HTML format.

6)wdFormatFlatXML 19 Open XML file format saved as a single XML file.

7)wdFormatFlatXMLMacroEnabled 20 Open XML file format with macros enabled saved as a single XML file.

8)wdFormatFlatXMLTemplate 21 Open XML template format saved as a XML single file.

9)wdFormatFlatXMLTemplateMacroEnabled 22 Open XML template format with macros enabled saved as a single XML file.

10)wdFormatOpenDocumentText 23 OpenDocument Text format.

11)wdFormatHTML 8 Standard HTML format.

12)wdFormatRTF 6 Rich text format (RTF).

13)wdFormatStrictOpenXMLDocument 24 Strict Open XML document format.

14)wdFormatTemplate 1 Word template format.

15)wdFormatText 2 Microsoft Windows text format.

16)wdFormatTextLineBreaks 3 Windows text format with line breaks preserved.

17)wdFormatUnicodeText 7 Unicode text format.

18)wdFormatWebArchive 9 Web archive format.

19)wdFormatXML 11 Extensible Markup Language (XML) format.

20)wdFormatDocument97 0 Microsoft Word 97 document format.

21)wdFormatDocumentDefault 16 Word default document file format. For Word, this is the DOCX format.

22)wdFormatPDF 17 PDF format.

23)wdFormatTemplate97 1 Word 97 template format.

24)wdFormatXMLDocument 12 XML document format.

25)wdFormatXMLDocumentMacroEnabled 13 XML document format with macros enabled.

26)wdFormatXMLTemplate 14 XML template format.

27)wdFormatXMLTemplateMacroEnabled 15 XML template format with macros enabled.

28)wdFormatXPS 18 XPS format.

这里我给大家介绍一段代码,将当前文档保存为一个文本文件。我们看下面的代码:

Sub mynzH

Dim myDoc As String

myDoc = ActiveDocument.Name

i = InStrRev(myDoc, ".")

If i = 0 Then

myDoc = InputBox("请输入您的文件名。")

Else

myDoc = Left(myDoc, i - 1)

myDoc = myDoc & ".txt"

End If

ActiveDocument.SaveAs2 FileName:=myDoc, FileFormat:=wdFormatText

End Sub

代码截图:

下面我们看代码的解读:

1) myDoc = ActiveDocument.Name 这段代码将当前活动文档名字存储在一个字符串的变量中。

2) i = InStrRev(myDoc, ".")

If i = 0 Then

myDoc = InputBox("请输入您的文件名。")

如果文件名是不存在的,那么就令用户输入一个新的文件名。

3) myDoc = Left(myDoc, i - 1)

myDoc = myDoc & ".txt"

取得后缀为.txt的文件名

4) ActiveDocument.SaveAs2 FileName:=myDoc, FileFormat:=wdFormatText

保存文件,注意这个时候的文件是往往保存在“文档”的文件夹下面。

文档的运行效果:

今日内容回向:

1 文档对象的Save方法和SaveAs2 方法的意义是什么?

2 如何利用SaveAs2 方法将word文件保存为文本文件?

本讲内容参考程序文件:Doc 003文档.docm

我20多年的VBA实践经验,全部浓缩在下面的各个教程及应用工具中:

来源:VBA语言专业教育

相关推荐