private string GenerateNestedField(Word.Field fldOuter, string sPlaceholder, object forward) { Word.Range rngFld = fldOuter.Code; bool bFound; string sFieldCode; //Get the field code from the placeholder by removing the { } sFieldCode = sPlaceholder.Substring(1, sPlaceholder.Length - 2); //Mid(sPlaceholder, 2, Len(sPlaceholder) - 2) rngFld.TextRetrievalMode.IncludeFieldCodes = true; System.Diagnostics.Debug.WriteLine("sFieldCode: " + sFieldCode); System.Diagnostics.Debug.WriteLine("rngFld.StoryType.ToString(): " + rngFld.StoryType.ToString()); System.Diagnostics.Debug.WriteLine("rngFld.StoryType.ToString(): " + rngFld.Text.ToString()); rngFld.Fields.Update(); rngFld.Find.ClearFormatting(); bFound = rngFld.Find.Execute(sPlaceholder, ref missing, ref missing, ref missing, ref missing, ref missing, ref forward, Word.WdFindWrap.wdFindContinue, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); System.Diagnostics.Debug.WriteLine("bFound: " + bFound); System.Diagnostics.Debug.WriteLine(" "); if (bFound) { Doc.Fields.Add(rngFld, Word.WdFieldType.wdFieldEmpty, sFieldCode, false); } return(fldOuter.Code.ToString()); }
public void WriteToRange(Word.Range fullRange) { Word.Range rangeCursor = fullRange; rangeCursor.Collapse(Word.WdCollapseDirection.wdCollapseStart); foreach (ContentItem item in Contents) { switch (item.Type) { case ContentType.Field: Word.Field field = fullRange.Fields.Add(rangeCursor); field.Code.Text = item.Text; field.Code.Font = item.Font; rangeCursor = field.Result; rangeCursor.Collapse(Word.WdCollapseDirection.wdCollapseEnd); break; case ContentType.RangeText: rangeCursor.Text = item.Text; rangeCursor.Font = item.Font; rangeCursor.Collapse(Word.WdCollapseDirection.wdCollapseEnd); break; case ContentType.LineBreak: rangeCursor.InsertBreak(Word.WdBreakType.wdLineBreak); rangeCursor.Collapse(Word.WdCollapseDirection.wdCollapseEnd); break; } } }
/// <summary> /// Adds a new empty <see cref="Word.Field"/> to the specified <see cref="Word.Range"/>. /// </summary> /// <param name="range">The <see cref="Word.Range"/> where to add the <see cref="Word.Field"/>.</param> /// <param name="preserveFormatting"> /// Whether to apply the formatting of the previous <see cref="Word.Field"/> result to the new result. /// </param> /// <returns>The newly created <see cref="Word.Field"/>.</returns> public Word.Field InsertEmpty(Word.Range range, bool preserveFormatting = false) { Word.Field result = this.AddFieldToRange(range, Word.WdFieldType.wdFieldEmpty, preserveFormatting); // Show the field codes of an empty field, because otherwise we can't be sure that it is visible. result.ShowCodes = true; return(result); }
void Application_WindowBeforeRightClick(Word.Selection Sel, ref bool Cancel) { Word.Field _field = CommonFunction.GetFieldBySection(WordApplication.GetInstance().WordApp); if (_field == null) { return; } if (_field.Code.Text.Contains(QuotationItem.FLAG) || _field.Code.Text.Contains(QuotationItem.FLAG)) { addBtn.Enabled = true; WordApplication.GetInstance().WordApp.ActiveDocument.Range(_field.Result.End + 1, _field.Result.End + 1).Select(); } }
private void AddMergeField(string stFieldName) { try { SplashScreenManager.ShowForm(null, typeof(WaitForm1), true, true, false); SplashScreenManager.Default.SetWaitFormDescription("Bitte warten…"); Object objType = Word.WdFieldType.wdFieldMergeField; Object oMissing = System.Reflection.Missing.Value; Object objFieldName = stFieldName; Word.Application WordApp = new Word.Application(); Word.Document WordDoc = Globals.ThisAddIn.Application.ActiveDocument; dynamic oRange = WordDoc.Content.Application.Selection.Range; Microsoft.Office.Interop.Word.Range rngFieldCode = oRange; Word.Field field = rngFieldCode.Fields.Add(rngFieldCode, ref objType, ref objFieldName, ref oMissing); } catch (Exception ex) {} finally { SplashScreenManager.CloseForm(false); } }
public void buildIfEmptyField(Word.Range rngTarget, object PageString, object textString, string data) { string sQ = '"'.ToString(); Word.Field fldIf = null; Word.View vw = Doc.ActiveWindow.View; rngTarget.Text = " "; rngTarget.End = rngTarget.Start; bool bViewFldCodes = false; string sFieldCode; string pageVar = "{PAGE \\* ARABIC}"; string docVar = "{DOCVARIABLE " + sQ + "edocs_Page" + pageVar + "_page" + sQ + "\\* MERGEFORMAT}"; if (data == "page") { docVar = pageVar; } string fullPageVar = "{DOCVARIABLE " + sQ + "edocs_Page" + docVar + "_" + data + sQ + "}"; string ifVar = "IF" + fullPageVar + "=" + sQ + "Error!*" + sQ + " " + sQ + "eDoc Empty Field" + sQ + fullPageVar; System.Diagnostics.Debug.WriteLine("String - " + ifVar); bViewFldCodes = vw.ShowFieldCodes; //Finding text in a field codes requires field codes to be shown if (!bViewFldCodes) { vw.ShowFieldCodes = true; } fldIf = rngTarget.Fields.Add(rngTarget, Word.WdFieldType.wdFieldEmpty, ifVar, false); sFieldCode = GenerateNestedField(fldIf, fullPageVar, true); sFieldCode = GenerateNestedField(fldIf, fullPageVar, false); if (data != "page") { sFieldCode = GenerateNestedField(fldIf, docVar, true); sFieldCode = GenerateNestedField(fldIf, docVar, false); } sFieldCode = GenerateNestedField(fldIf, pageVar, true); sFieldCode = GenerateNestedField(fldIf, pageVar, false); rngTarget.Fields.Update(); vw.ShowFieldCodes = bViewFldCodes; }
private void VerileriWordeAktar(Word.Field alan, string text) { alan.Select(); uygulama.Selection.TypeText(text); }
/// <summary> /// Adds one or more new <see cref="Word.Field"/> to the specified <see cref="Word.Range"/>. /// <para> /// This method allows to insert nested fields at the specified range. /// </para> /// <example> /// <c>InsertField(Application.Selection.Range, {{= {{PAGE}} - 1}};</c> /// will produce /// { = { PAGE } - 1 } /// </example> /// </summary> /// <param name="range">The <see cref="Word.Range"/> where to add the <see cref="Word.Field"/>.</param> /// <param name="theString">The string to convert to one or more <see cref="Word.Field"/> objects.</param> /// <param name="fieldOpen">The special code to mark the start of a <see cref="Word.Field"/>.</param> /// <param name="fieldClose">The special code to mark the end of a <see cref="Word.Field"/>.</param> /// <returns>The newly created <see cref="Word.Field"/></returns> /// <remarks> /// A solution for VBA has been taken from <a href="http://stoptyping.co.uk/word/nested-fields-in-vba">this</a> /// article and adopted for C# by the author. /// </remarks> public Word.Field InsertField( Word.Range range, string theString = "{{}}", string fieldOpen = "{{", string fieldClose = "}}") { if (null == range) { throw new ArgumentNullException("range"); } if (string.IsNullOrEmpty(fieldOpen)) { throw new ArgumentException("fieldOpen"); } if (string.IsNullOrEmpty(fieldClose)) { throw new ArgumentException("fieldClose"); } if (!theString.Contains(fieldOpen) || !theString.Contains(fieldClose)) { throw new ArgumentException("theString"); } // Special case. If we do not check this, the algorithm breaks. if (theString == fieldOpen + fieldClose) { return(this.InsertEmpty(range)); } // TODO Implement additional error handling. // TODO Possible to remove the dependency to state capture? using (new StateCapture(range.Application.ActiveDocument)) { Word.Field result = null; Stack fieldStack = new Stack(); range.Text = theString; fieldStack.Push(range); Word.Range searchRange = range.Duplicate; Word.Range nextOpen = null; Word.Range nextClose = null; Word.Range fieldRange = null; while (searchRange.Start != searchRange.End) { nextOpen = this.FindNextOpen(searchRange.Duplicate, fieldOpen); nextClose = this.FindNextClose(searchRange.Duplicate, fieldClose); if (null == nextClose) { break; } // See which marker comes first. if (nextOpen.Start < nextClose.Start) { nextOpen.Text = string.Empty; searchRange.Start = nextOpen.End; // Field open, so push a new range to the stack. fieldStack.Push(nextOpen.Duplicate); } else { nextClose.Text = string.Empty; // Move start of main search region onwards past the end marker. searchRange.Start = nextClose.End; // Field close, so pop the last range from the stack and insert the field. fieldRange = (Word.Range)fieldStack.Pop(); fieldRange.End = nextClose.End; result = this.InsertEmpty(fieldRange); } } // Move the current selection after all inserted fields. // TODO Improvement possible, e.g. by using another range object? int newPos = fieldRange.End + fieldRange.Fields.Count + 1; fieldRange.SetRange(newPos, newPos); fieldRange.Select(); // Update the result of the outer field object. result.Update(); return(result); } }