If it already exists, it will be deleted! Var newFileName = Path.ChangeExtension(fileName, ".docx")
the code will raise an exception back to the caller.
If anything goes wrong in this file handling, Output file exists and deletes it, and finally moves the file from the To do this, the codeĬreates a new file name by changing the extension verifies that the The code then renames the newly modified document. ' Track that the document has been changed. Track that the document has been changed. Therefore, you must have some way to indicate to the code after the block that you have modified the file: The fileChanged Boolean variable tracks this information for you. The using block closes the file at the end of the block. However, you cannot do that while the file is open. The Open XML SDK 2.5 provides a way to perform this task: You can call the document ChangeDocumentType method and indicate the new document type (in this case, supply the WordProcessingDocumentType.Document enumerated value). You must also convert the document type, internally. It is not enough to delete the part from the document. ' Delete the vbaProject part and then save the document. Delete the vbaProject part and then save the document. It calls the DeletePart method to delete the part, and then calls the Save method of the document to save the changes. To find the part, the sample code retrieves the VbaProjectPart property of the document. The code has no effect if the file to convert does not contain a vbaProject part. The sample code next verifies that the vbaProject part exists, deletes the part and saves the document. WordprocessingDocument.Open(fileName, True) Using document As WordprocessingDocument = WordprocessingDocument.Open(fileName, true)) The code then retrieves a reference to the Document part by using the MainDocumentPart property of the word processing document. The code starts by opening the document by using the Open method and indicating that the document should be open for read/write access (the final true parameter). Renames the document so that it uses the. The code deletes the part, it changes the document type internally and The document contains a vbaProject part, and deleting the part. The sample code modifies the document that you specify, verifying that The task of converting a macro enabled document to one that is not macroĮnabled therefore consists largely of removing the vbaProject part from The Open XML SDK 2.5 Productivity Tool for Microsoft Office shows theĭocument parts in a sample document that contains a macro. The following image from the Document Explorer in When you create and save a VBA macro in a document, Word adds a newīinary part named vbaProject that contains the internal representation For more information about the Open Packaging Conventions,
zip files is called the Open PackagingĬonventions. A part has a particular content type, and canĬontain content equivalent to an external XML file, binary file, imageįile, and so on, depending on the type. You can think of each part as being similar string filename = filename As String = "C:\Users\Public\Documents\WithMacros.docm"Ī word processing document package such as a file that has a. The following sample code shows an example.
To call the sample method, pass a string that contains the name of theįile to convert. The complete code listing for the method can be found in the Sample Code section. Public Sub ConvertDOCMtoDOCX(ByVal fileName As String) public static void ConvertDOCMtoDOCX(string fileName) Parameter that indicates the file name of the file to convert. Method to remove the macros and the vbaProject part that contains themįrom a document stored in. docmĮxtension) to a standard document (with a. The ConvertDOCMtoDOCX sample method can be used to convert a WordĢ010 or Word 2013 document that contains VBA code (and has a. Must explicitly reference the following assemblies in your project:ĭocumentFormat.OpenXml (Installed by the Open XML SDK)ĭirectives or Imports statements to compile
To use the sample code in this topic, you must install the Open XML SDK It contains an exampleĬonvertDOCMtoDOCX method to illustrate this task. docm extension) toĪ standard document (with a. Word 2013 document that contains VBA code (and has a. Office to programmatically convert a Microsoft Word 2010 or Microsoft
This topic shows how to use the classes in the Open XML SDK 2.5 for