Document.SaveAs2与Document.WdSaveFormat两种方法的区别是什么?
Hey there! Let's break down the key differences between Document.SaveAs2 and WdSaveFormat in VBA for Word—they work together but serve completely distinct roles. Here's a no-fluff breakdown:
1. Core Functionality & Purpose
First, let's get their core identities straight:
Document.SaveAs2: This is an action method—it does the actual work of saving your Word document to a specified location, with a specified name, and in a specified format. Think of it as the "doer" that executes the save operation. It's an updated version of the olderDocument.SaveAsmethod, with support for newer file formats and additional parameters.WdSaveFormat: This is an enumeration (enum) of predefined constants—it doesn't perform any actions on its own. Instead, it provides a set of standardized, human-readable values that you pass to save methods likeSaveAs2to define what file format the document should be saved in. For example,wdFormatPDForwdFormatXMLDocumentare members of this enum.
2. Usage Examples
To see how they interact, here's a practical code snippet:
' Save the active document as a PDF using SaveAs2, with WdSaveFormat constant ActiveDocument.SaveAs2 _ FileName:="My_Exported_Document.pdf", _ FileFormat:=wdFormatPDF ' This is a WdSaveFormat constant
- Without
SaveAs2, you can't trigger the save action. - Without
WdSaveFormatconstants, you'd have to use raw numeric values (e.g.,17for PDF) which are hard to remember and prone to errors.
3. Applicable Scenarios
Let's map out when you'd use each:
- Use
Document.SaveAs2when:- You need to save a document with a new name, new location, or different format.
- You're automating batch saves (e.g., converting 100 .doc files to .docx).
- You need access to its extended parameters (like
EmbedTrueTypeFontsorSaveFormsData) that aren't available in the olderSaveAsmethod.
- Use
WdSaveFormatwhen:- You're specifying the target format for any save method (
SaveAs2,SaveAs,SaveCopyAs). - You want to make your code readable and maintainable (using
wdFormatPDFis way clearer than typing17). - You need to ensure compatibility across Word versions—enum constants are updated with new formats (like
.docxor.odt) as Word evolves.
- You're specifying the target format for any save method (
Quick Bonus Note
SaveAs2 is backward-compatible with most SaveAs use cases, but it's the recommended method for modern Word versions (2010 and later) since it supports newer formats and features. WdSaveFormat is the standard way to reference save formats across all Word save methods, so you'll almost always use it alongside SaveAs2.
内容的提问来源于stack exchange,提问作者user2153022
相关产品推荐
相关产品推荐

