Created
March 27, 2018 12:39
-
-
Save Manzanit0/8ee560edb07103179d845704d8ea2ecb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Office.Interop.Word; | |
using System.IO; | |
namespace MSWordExample | |
{ | |
public class LineNumberingKiller | |
{ | |
static void Main(string[] args) | |
{ | |
Application word = new Application(); | |
foreach (var documentPath in GetDocumentPaths(args[0])) | |
{ | |
if (documentPath.EndsWith(".docx")) | |
{ | |
ProcessDocument(word, documentPath); | |
} | |
} | |
word.Quit(); | |
} | |
static string[] GetDocumentPaths(string directory) | |
{ | |
if (!Directory.Exists(directory)) | |
{ | |
throw new DirectoryNotFoundException("The directory does not exist"); | |
} | |
return Directory.GetFiles(directory); | |
} | |
static void ProcessDocument(Application word, string docPath) | |
{ | |
// Set document properties (path, read/write permissions, etc.) | |
object miss = System.Reflection.Missing.Value; | |
object readOnly = false; | |
object path = docPath; | |
// Open doc. | |
Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss); | |
// Change line numbering to desired status. | |
docs.PageSetup.LineNumbering.Active = 0; // The integral equivalent of the Boolean True (in VSTO) is -1, and of False is 0. | |
// Save document and close stream. | |
docs.Save(); | |
docs.Close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! Also the call to Open can be done simply with
Document docs = word.Documents.Open(ref path, ref missing, ref readOnly);
(the other args are optional I think)