Skip to content

Instantly share code, notes, and snippets.

@eduard93
Created June 7, 2018 18:43
Show Gist options
  • Save eduard93/9e2b8ce27eee1d3ca927d8d0eeb9d8b3 to your computer and use it in GitHub Desktop.
Save eduard93/9e2b8ce27eee1d3ca927d8d0eeb9d8b3 to your computer and use it in GitHub Desktop.
Update only some fields in docx file
using System;
using System.Collections.Generic;
using Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
namespace FieldUpdater
{
class Program
{
static void Main(string[] args)
{
string filePathstr = @"C:\Test.docx";
//List<string> path = new List<string>(args);
//string filePathstr = string.Join(" ", path.ToArray());
try
{
Application ap = new Application();
Document document = ap.Documents.Open(filePathstr);
List<string> list = new List<string>(1000);
foreach (Field field in document.Fields)
{
try
{
string text = field.Code.Text;
var value = field.Result.Text;
if (text.Contains("REF _Ref")) // && !(value.Contains(".")
{
if ((value != "") && (value != null))
{
// I wanted to update only links with names 1-99
if (value.Length < 3) // value.Contains(".")
{
field.Update();
//list.Add(text + " " + value);
}
}
}
} catch (COMException ex) {
}
}
document.Save();
document.Close();
}
catch (NullReferenceException)
{
System.Console.WriteLine("A valid file was not selected.");
}
}
}
}
@eduard93
Copy link
Author

eduard93 commented Jun 7, 2018

Word replace latin symbols with cursive

([a-z]{1;10})
\1

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