/// <summary> /// 发送整个文档内容到指定接口 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void sendBtn_Click(object sender, RibbonControlEventArgs e) { ////获取当前文档的内容 Microsoft.Office.Interop.Word.Range _rng = Globals.ThisAddIn.Application.ActiveDocument.Content; string _curRangeText = _rng.Text; string _jsonParam = "{\"document\":\"" + _curRangeText + "\"}"; string _requesUrl = ConfigurationManager.AppSettings["RequestDocumentUrl"]; try { //// 异步等待请求WebAPI var hwr = await HttpTool.RequesPostAsync(_requesUrl, _jsonParam); HttpTool.ResponseResult _curResult = HttpTool.GetResponseJson <HttpTool.ResponseResult>(hwr); if (_curResult.Code == 2000) { MessageBox.Show("远程保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show(_curResult.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception ex) { Debug.WriteLine("发送整个文档内容出错:" + ex.Message); } }
private async void GetUpdateSentenceText() { ////通过选取类型来获取光标位置 Word.Selection currentSelection = Application.Selection; //// 暂存当前用户的 Overtype 设置 bool userOvertype = Application.Options.Overtype; // 确保 Overtype false. if (Application.Options.Overtype) { Application.Options.Overtype = false; } // 判断当前选取是否是一个插入点(编辑区光标) if (currentSelection.Type == Word.WdSelectionType.wdSelectionIP) { // 当前选取的Range位置,即光标位置 int _cur_start = currentSelection.Range.Start; int _cur_end = currentSelection.Range.End; //// 以当前光标位置作为Range起始区间 Word.Range rng = this.Application.ActiveDocument.Range(_cur_start, _cur_end); ////通过扩展Range范围来获取更新后的句子 rng.MoveStart(Word.WdUnits.wdSentence, -1); rng.MoveEnd(Word.WdUnits.wdSentence, 1); string _curRangeText = rng.Text; // Debug.WriteLine(_curRangeText); TaskPanel.DetectTextLabel.Text = _curRangeText; //// 正则匹配,如果是完整句子,发送到服务端。 string _sentenceRegex = @"(\.|。|\?|?|\!|!|……)"; if (Regex.IsMatch(_curRangeText, _sentenceRegex)) { //// 去掉重复句子 if (cacheUpdateText != _curRangeText) { string _jsonParam = "{\"sentence\":\"" + _curRangeText + "\"}"; string _requestUrl = ConfigurationManager.AppSettings["RequestSentenceUrl"]; HttpWebResponse hwr = await HttpTool.RequesPostAsync(_requestUrl, _jsonParam); HttpTool.ResponseResult _curResult = HttpTool.GetResponseJson <HttpTool.ResponseResult>(hwr); cacheUpdateText = _curRangeText; } } } // 恢复用户 Overtype Application.Options.Overtype = userOvertype; }