简体   繁体   English

如何使用 C# 进入 Microsoft.Office.Interlop.Word Mailmerge 上的 NextRecord?

[英]How do I advance to the NextRecord on Microsoft.Office.Interlop.Word Mailmerge with C#?

I have this method that "works", but only gives me one row of data.我有这种“有效”的方法,但只给了我一行数据。 I used EPPlust to loop through the Excel file and calling the Word document template.我使用 EPPlust 遍历 Excel 文件并调用 Word 文档模板。 The template is an 8.5 X 11 document with 21 labels.该模板是一个 8.5 X 11 的文档,带有 21 个标签。 My goal is the fill the labels with information from the Excel file.我的目标是用 Excel 文件中的信息填充标签。 I think I'm just missing a command to advance the next record.我想我只是缺少一个命令来推进下一个记录。 I've checked the Microsoft definition and it left me with more questions...and google has gotten me this far but I still need this little nugget of information to finish off this app.我已经检查了 Microsoft 的定义,它给我留下了更多的问题……谷歌已经让我走到了这一步,但我仍然需要这些小信息来完成这个应用程序。

Thanks,谢谢,

  public static void MergeLabels(string labelDoc)
        {
            FileInfo fi = new FileInfo(labelDoc);
            string labelSave = labelDoc.Replace(".xlsx", ".docx");
            string labelTemplet = fi.DirectoryName.ToString() + "\\StockLabelsMerge.docx"; 
            using (ExcelPackage excelPackage = new ExcelPackage(fi))
            {    
                var application = new Microsoft.Office.Interop.Word.Application();
                var document = new Microsoft.Office.Interop.Word.Document();
                
                document = application.Documents.Add(Template: labelTemplet);


                foreach (var worksheetLoop in excelPackage.Workbook.Worksheets)
                {
                   // this.comboBox1.Items.Add(worksheet.Name);
            
               ExcelWorksheet workSheet = excelPackage.Workbook.Worksheets[worksheetLoop.Index];

                int colCount = workSheet.Dimension.End.Column; 
                int rowCount = workSheet.Dimension.End.Row; 
                string specieSize = string.Empty;
                string cabinet = string.Empty;
                string jobNumber = string.Empty;
            


                for (int row = 1; row <= rowCount; row++)
                {

                    if (workSheet.Cells[row, 1].Value != null)
                    {
                        jobNumber = workSheet.Cells[row, 1].Value.ToString();
                        if (workSheet.Cells[row, 2].Value != null)
                        {
                            cabinet = workSheet.Cells[row, 2].Value.ToString();
                            if (workSheet.Cells[row, 3].Value != null)
                            {
                                specieSize = workSheet.Cells[row, 3].Value.ToString();

                                foreach (Microsoft.Office.Interop.Word.Field label in document.Fields)
                                {
                                    if (label.Code.Text.Contains("F1"))
                                    {
                                        label.Select();
                                        application.Selection.TypeText(jobNumber);
                                    }
                                    else if (label.Code.Text.Contains("NoName"))
                                    {
                                        label.Select();
                                        application.Selection.TypeText(cabinet);
                                    }
                                    else if (label.Code.Text.Contains("NoName2"))
                                    {
                                        label.Select();
                                        application.Selection.TypeText(specieSize);


                                    }

                                        //next record?
                                      //  label.Next.Result;



                                }

                            }
                        }
                    }




                }
                   
                } 
               
                document.SaveAs2(FileName: labelSave);    
            }




        }

If someone knows what's missing, or even has a good suggestion, I would appreciate it.如果有人知道缺少什么,或者甚至有很好的建议,我将不胜感激。

it seems you loop through your Excel rows, fetch 3 data from each row and then you fill in 3 labels in the Word template.似乎您遍历 Excel 行,从每行获取 3 个数据,然后在 Word 模板中填写 3 个标签。 I think the issue is in the foreach for labels.我认为问题在于标签的 foreach。 You basically loop through each label (out of 21) and only fill the same 3 of them.您基本上遍历每个标签(共 21 个)并且只填充相同的 3 个。 What are the ids for the other 18?其他 18 个的 ID 是什么? Another issue might be with the subsequent ifs.另一个问题可能与后续的 ifs 有关。 If any cell in the row is null, you won't hit the label filling code.如果行中的任何单元格为空,则不会命中标签填充代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 C# Word interlop 获取 Word 文档中每一页上第一段的范围 - How to get the range of first paragraph on each page in Word Document using C# Word interlop 如何在C#中修复&#39;Office.Interlop -2146822384:无法编辑范围&#39; - How to fix 'Office.Interlop -2146822384: Cannot edit Range' in C# 在 c# 中使用 microsoft.office.tools.word 我们如何将单个页面保存为 rtf 文件 - In c# using microsoft.office.tools.word how do we save a single page as rtf file C#Microsoft.Office.Interop.Word - C# Microsoft.Office.Interop.Word C#-MS Word-MailMerge如何在MailMerge.DataSource.LastRecord返回-16时获取最新记录 - C# - MS Word - MailMerge how to get Last Record when MailMerge.DataSource.LastRecord returns -16 C#Microsoft Word自动化Microsoft.Office.Interop.Word - C# Microsoft Word automation Microsoft.Office.Interop.Word 如何在不安装 Microsoft Office 的情况下在 C# 中创建 Excel(.XLS 和 .XLSX)文件? - How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office? 如何使用Microsoft.Office.Interop.Word在C#中从Word文件中查找突出显示的文本? - How to find highlighted text from Word file in C# using Microsoft.Office.Interop.Word? MailMerge:从Excel到Word C# - MailMerge: From Excel to Word C# 如何在C#中使用Microsoft.Office.Interop.Word从Word文件(.Docx)中按页获取文本 - How to get text by page from word file (.Docx) using Microsoft.Office.Interop.Word in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM