【问题标题】:How do I progamaticaly position the cursor in a word document after inserting a hyperlink?插入超链接后,如何将光标定位在 Word 文档中?
【发布时间】:2020-11-25 01:18:13
【问题描述】:

我有一个 Outlook 插件,它通过提供 addAttachment() 处理程序并将超链接插入到每个附加文件的电子邮件正文中来处理向电子邮件添加附件。

插入超链接没问题,但如果通过将附件拖到电子邮件中添加附件,则插入光标始终保持在超链接之前。

                Document doc = currentMailItem.GetInspector.WordEditor;
                Selection objSel = doc.Windows[1].Selection;
                Range insertRange = objSel.Range;
                object missObj = Type.Missing;
                Hyperlink link = doc.Hyperlinks.Add(insertRange, MyUrl, missObj, missObj, displayName, missObj);
                objSel.Collapse(WdCollapseDirection.wdCollapseEnd);
                /* also tried this:
                Range r = link.Range;
                objSel.MoveRight(WdUnits.wdCharacter, r.End, r.Start);
                 * */

我怀疑我的问题可能是拖放处理。我注意到光标出现在我放置它的位置,在插入的 URL 之后,但随后又跳回到它之前的点。会不会是在完成拖拽操作的时候,有什么东西把光标重置到了原来的位置?

有人可以告诉我我需要做什么吗?

【问题讨论】:

    标签: c# ms-word outlook-addin


    【解决方案1】:

    我过去创建了一个与此非常相似的项目,并选择使用 MailItem.HTMLBody 属性而不是与对象放置作斗争。

    在我的例子中,我正在构建一个数据网格,但可以构建类似的东西来接收您的 URL 数据。注意:这是我开发的早期,代码可能需要一些重构,但是对于过去的复制和粘贴,我希望这还不错。我还从这篇文章中排除了一些不必要的信息,但最重要的部分都包含在此处。

    为了组装邮件正文,该方法创建了一个StringBuilder,并逐段构建HTML文档。

            public string ConstructBody(DataTable dataTable)
            {
                StringBuilder bodyStringBuilder = new StringBuilder();
    
                // Creates the HTML code, constructs body, Constructs table, defines table border.
                bodyStringBuilder.AppendLine("<html>");
                bodyStringBuilder.AppendLine(Tab + "<body>");
                bodyStringBuilder.AppendLine(Tab + Tab + "<table>");
                bodyStringBuilder.AppendLine(Tab + Tab + "<table border='2' bgcolor:#DDDDDD bordercolor:black>");
    
                // Column headers - Style defined.
                bodyStringBuilder.Append(Tab + Tab + Tab + "<tbody align='center' style='font-family:verdana; color:#000000; font-size:14;background-color:#9DB9C8'>");
                bodyStringBuilder.Append(Tab + Tab + Tab + "<tr>");
    
                // Column Headers - Named 
                foreach (DataColumn dc in dataTable.Columns)
                {
                    bodyStringBuilder.AppendFormat("<th>{0}</th>", dc.ColumnName);
                }
    
                // End of Column Header Creation
                bodyStringBuilder.AppendLine("</tr>");
    
                // Empty data rows created and filled by another foreach loop
                int drColorSwitch = 1;
    
                foreach (DataRow dr in dataTable.Rows)
                {
                    if (IsOdd(drColorSwitch))
                    {
                        bodyStringBuilder.Append(Tab + Tab + Tab + Tab + "<tbody align='center' style='font-family:verdana;background-color=#DDDDDD; color:#000000; font-size:14 '>");
                    }
                    else
                    {
                        bodyStringBuilder.Append(Tab + Tab + Tab + Tab + "<tbody align='center' style='font-family:verdana;background-color=#BDD0D9; color:#000000; font-size=14 '>");
                    }
                    bodyStringBuilder.Append(Tab + Tab + Tab + "<tr>");
                    drColorSwitch ++;
    
                    // Fills the data. 
                    foreach (DataColumn dc in dataTable.Columns)
                    {
                        string cellValue = dr[dc] != null ? dr[dc].ToString() : "";
                        bodyStringBuilder.AppendFormat("<td>{0}</td>", cellValue);
                    }
    
                    // Closes out each datarow
                    bodyStringBuilder.AppendLine("</tr>");
                }  // End of the datarow creation functions
    
                // Closes the table, closes the body, and finally ends the HTML code for the body of the email. 
                bodyStringBuilder.AppendLine(Tab + Tab + "</table>");
                bodyStringBuilder.AppendLine(Tab + "</body>");
                bodyStringBuilder.AppendLine("</html>");
    
                return bodyStringBuilder.ToString();                               // Finally - returns value of SB
            }
    

    最后,在 MailReport 方法中,我创建了电子邮件消息并调用 Send();

    public void MailReport(DataTable datatable,string[] recipients,string subject, string headertext, string reporttitle, bool includedate, bool includefiles, string[] filelist, bool isEncrypted)
            {
                // Creates instance of Header and Body Construction Classes
                HeaderConstruction headerConstruction = new HeaderConstruction(); 
                BodyConstruction bodyConstruction = new BodyConstruction();
    
    
                Outlook.Application oApp = new Outlook.Application();
                Outlook.MailItem oMsg = oApp.CreateItem(Outlook.OlItemType.olMailItem);
                foreach (string recipient in recipients)
                {
                    oMsg.Recipients.Add(recipient); 
                }
    
                if(isEncrypted)
                {
                    // If caller indicates that message should be encrypted, system will add secured flag to subject
                    oMsg.Subject = "<secure> " + subject;
                }
                else
                {
                    // Else, message will transmit without security flag 
                    oMsg.Subject = subject;
                }
    
                // introduces the body table
                oMsg.HTMLBody = headerConstruction.ConstructHeader(headertext,reporttitle, includedate) + bodyConstruction.ConstructBody(datatable);
    
                // Includes files in output message if requested
                if (includefiles)
                {
                    foreach (string path in filelist)
                    {
                        oMsg.Attachments.Add(path);
                    }
                }
    
                // Send
                oMsg.Send();       
            }
    

    【讨论】:

    • 我还要指出,我认为正文中的“标签”是不必要的回顾,但在那些日子里,我想它帮助我保持组织。
    • 谢谢,但不幸的是,这对我没有帮助,因为在我的情况下,我正在捕获正在添加的附件并将它们上传到服务器,然后将 URL 引用插入到电子邮件正文中。
    • 这是有道理的。或者,您可能想尝试 Word 的查找和选择功能。我用它来构建 word 文档,它有时可以帮助选择特定的字符串,然后折叠选择。我在stackoverflow.com/questions/5628473/… 中提到了这一点,在您的情况下,您只需搜索链接文本,然后立即折叠选择。我认为这会将光标移动到所选文本的末尾。如果可行,我会将其作为新解决方案重新发布。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多