Created
June 7, 2018 18:43
-
-
Save eduard93/9e2b8ce27eee1d3ca927d8d0eeb9d8b3 to your computer and use it in GitHub Desktop.
Update only some fields in docx file
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 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."); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Word replace latin symbols with cursive
([a-z]{1;10})
\1