Skip to content

Instantly share code, notes, and snippets.

@Manzanit0
Created March 27, 2018 12:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Manzanit0/8ee560edb07103179d845704d8ea2ecb to your computer and use it in GitHub Desktop.
Save Manzanit0/8ee560edb07103179d845704d8ea2ecb to your computer and use it in GitHub Desktop.
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();
}
}
}
@davidefiocco
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment