如何使用JavaScript在word doc的表格的特定单元格中插入图像

时间:2014-07-30 07:00:23

标签: javascript ms-word activexobject visual-studio-macros

我有一个客户端应用程序,它在浏览器(IE)上呈现地图。在该应用程序中,我有一个HTML表单,它将捕获一些文本内容,然后应用程序将生成一个Word文档,其中将包含用户以表格形式提交的所有字段。我能够做到这一点,但我必须从浏览器插入一个图像(地图的屏幕截图)。

我把问题分成两部分: 1)使用javascript以表格形式写一些表格内容---我能够做到这一点。 2)截取地图的截图并将其插入到该单词文档中.---无法截取屏幕截图,但尝试使用本地计算机中的一个虚拟图像并将其插入到单词doc中。

问题是,我希望该图像位于第四行和第一列。但是“word.Selection” 指向表格中的第一个单元格。

itable.columns(1).cells(5).Range.Text =“some text”;我们不能使用这样的东西来插入文本或在特定单元格中放置“word.Selection”。

以下页面提供了我的表单请求。

        <!DOCTYPE html>
        <html>

        <head>
        <script src="jquery-1.11.0.js"></script>
        <script type="text/javascript">

        function myFunction()
        {
        var table = document.getElementById("myTable");
        var row = table.insertRow(1);
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        cell1.innerHTML = getFieldValue("FirstName");
        cell2.innerHTML = getFieldValue("lastName");
        }
         var JPEGName = "C:\\all data\\MyBT\\tasks\\dp manifold.jpg";
        function saveAsWord() {
        word = new ActiveXObject("Word.Application");
        word.visible=false;
        word.Documents.Add();
         word.Application.Visible = true;
        word.ActiveDocument.PageSetup.LineNumbering.Active=false;
        word.ActiveDocument.PageSetup.TopMargin=30;
        word.ActiveDocument.PageSetup.BottomMargin = 30;
        word.ActiveDocument.PageSetup.LeftMargin = 35;
        word.ActiveDocument.PageSetup.RightMargin = 30;
        word.ActiveDocument.PageSetup.Gutter = 0.0;
        word.Selection.Font.Bold = true;
        word.Selection.Font.Size = 14;
        word.Selection.ParagraphFormat.Alignment = 1;
        word.Selection.TypeText("form details");
        word.Selection.Font.Size = 12;
        var itable=word.ActiveDocument.Tables.Add(word.ActiveDocument.Application.Selection.Range, 16, 4);
        //itable.AutoFormat(16);

        word.ActiveDocument.Tables(1).Range.ParagraphFormat.Alignment = 0;
        itable.columns(2).cells(1).Range.ParagraphFormat.Alignment =1;
        itable.columns(2).cells(1).Range.Text="Exchange Area"
        itable.columns(3).cells(1).Range.ParagraphFormat.Alignment =0;
        itable.columns(1).cells(2).Range.Text="CSS Job No ";
        itable.columns(3).cells(2).Range.Text="Sr. No. : ";
        itable.columns(1).cells(3).Range.Text="Customer Details : "+getFieldValue("firstName");
        itable.columns(1).cells(4).Range.Text="Engineers Details    : "+getFieldValue("lastName");
        itable.columns(3).cells(4).Range.ParagraphFormat.Alignment =0;
        itable.columns(3).cells(4).Range.Text="Date : ";
        //itable.columns(3).cells(4).Range.Content.InlineShapes.AddPicture(JPEGName);
        itable.columns(1).cells(5).Range.Text="Requirement : ";
        itable.columns(3).cells(5).Range.ParagraphFormat.Alignment =0;
        itable.columns(3).cells(5).Range.Text="Survey Reqd  : ";
        itable.columns(1).cells(6).Range.Text="Visiting Date    : ";
        itable.columns(2).cells(6).Range.ParagraphFormat.Alignment =1;
        itable.columns(2).cells(6).Range.Text="Visit Time : ";
        itable.columns(1).cells(5).Range.Text="Whom To Meet : ";
        itable.columns(3).cells(5).Range.ParagraphFormat.Alignment =0;
        itable.columns(3).cells(5).Range.Text="Underground Cable    : ";
        itable.columns(1).cells(6).Range.Text="Visiting Date    : ";
        itable.columns(2).cells(6).Range.ParagraphFormat.Alignment =1;
        itable.columns(2).cells(6).Range.Text="Overhead Work : ";
        itable.columns(3).cells(6).Range.Text="Underground Civil  : ";
        itable.columns(1).cells(7).Range.Text="Location:   ";
        itable.columns(1).cells(8).Range.Text="Map Ref:    ";
        itable.columns(1).cells(10).Range.ParagraphFormat.Alignment =1;
        itable.columns(1).cells(10).Range.Text="Title:  EXPOSE BURIED JOINT";
        itable.columns(3).cells(10).Range.ParagraphFormat.Alignment =1;
        itable.columns(3).cells(10).Range.Text="Job Summary";
        itable.columns(1).cells(12).Range.ParagraphFormat.Alignment =1;
        itable.columns(1).cells(12).Range.Text="Sig. of Security Supervisior";
        itable.columns(3).cells(12).Range.ParagraphFormat.Alignment =1;
        itable.columns(3).cells(12).Range.Text="Sig. of Meeting Person";
        itable.Cell(3, 1).Merge(itable.Cell(3, 2));
        itable.Cell(4, 2).Split(1, 2);
        itable.Cell(4, 1).Merge(itable.Cell(4, 2));
        itable.Cell(4, 2).Merge(itable.Cell(4, 3));
        itable.Cell(5, 2).Split(1, 2);
        itable.Cell(5, 1).Merge(itable.Cell(5, 2));
        itable.Cell(5, 2).Merge(itable.Cell(5, 3));
        itable.Cell(7, 1).Merge(itable.Cell(7, 3));
        itable.Cell(8, 1).Merge(itable.Cell(8, 3));
        itable.Cell(9, 1).Merge(itable.Cell(9, 3));
        itable.Cell(11, 1).Merge(itable.Cell(11, 3));
        itable.Cell(13, 1).Merge(itable.Cell(13, 3));
        itable.Cell(14, 1).Merge(itable.Cell(14, 3));
        itable.Cell(15, 1).Merge(itable.Cell(15, 3));
        itable.Cell(16, 1).Merge(itable.Cell(16, 3));
        //word.Application.PrintOut(true);
        //setTimeout("appexit()",10000);
        word.Selection.TypeParagraph();
        word.Selection.InlineShapes.AddPicture(JPEGName);
        }
        </script> 
        </head>
        <body>  
        <table id="myTable" style="border:1px solid black">
        <tr>
        <td>First Name</td>
        <td>Last Name</td><td>SSN</td>
        </tr>

        </tr>
        </table><br><br><br>
        Your Social security number is <script type="text/javascript">
        document.write(getFieldValue("ssn"))
        </script>.<br>
        You entered &quot;<script type="text/javascript">
        document.write(getFieldValue("FirstName"))
        </script>&quot; as your First Name.<br>
        You entered &quot;<script type="text/javascript">
        document.write(getFieldValue("lastName"))
        </script>&quot; as your Last Name.<br>
        <button id="foo" onclick="myFunction()">Try it</button>
        <input type="button" value="print slip" onclick="saveAsWord()"/> 

        </body>
        </html>

我是编写用于生成MS文档的脚本的新手。

1 个答案:

答案 0 :(得分:0)

我找到了我做错的代码。而不是使用&#34; word.Selection&#34;使用&#34; itable.cell(行,列)。范围&#34;插入。

      itable.cell(5,1).range.InlineShapes.AddPicture(JPEGName );