【问题标题】:Woes using Word Interop - Inserting an image on a template使用 Word 互操作的问题 - 在模板上插入图像
【发布时间】:2018-12-12 14:04:52
【问题描述】:

我尝试了多种不同的解决方案来尝试将图像插入到文档模板中。我取得了一些成功,但没有达到预期的效果。基本上,我只是想在所有其他内容之上的文档顶部插入图像。图片最多只能将其余内容移到页面下方,而不是放在任何内容之上。

    With objDoc
        Dim filePath As String = Path.Combine(appPath, fileName)
        If Not filePath = "" Then
            Dim img As Image = Image.FromFile(filePath)
            Dim imgX As Integer = img.Width
            Dim imgY As Integer = img.Height

            'insert picture here
        End If
    End With

将上面的评论替换为以下内容,我已经成功了……有点。下面将图像插入到标题中。但是无论定义图像参数如何,图像都会以非常奇怪的方式缩放。

            .PageSetup.DifferentFirstPageHeaderFooter = True
            .Sections(1).Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Shapes.AddPicture(
               FileName:=filePath,
               LinkToFile:=False,
               SaveWithDocument:=True,
               Left:=0,
               Top:=0,
               Width:=imgX,
               Height:=imgY
        ).ConvertToInlineShape()

以下内容将在标题下方和文本上方插入图像。尝试将其转换为内联形状会引发异常。在这种情况下,给画布一个 0,0 开始会忽略模板边距并将图像放在工作表的左边缘。即使我将它向右移动并让文本向下移动,它仍然是不可取的,因为顶部有太多的空白。

        Dim objCanvas As Word.Shape = objWordApp.ActiveDocument.Shapes.AddCanvas(Left:=0, Top:=0, Width:=imgX, Height:=imgY)
        objCanvas.CanvasItems.AddPicture(FileName:=filePath, LinkToFile:=False, SaveWithDocument:=True)

以下其余部分是我通过 msdn 和其他各种渠道进行筛选的尝试。这些都没有显示任何图像。

               '.InlineShapes.AddPicture(filePath, Type.Missing, Type.Missing, Type.Missing)

                'Dim objRng As Word.Range = .Range()
                'objRng.InlineShapes.AddPicture(filePath)

                'Dim objInlineShape As Word.InlineShape = objWordApp.Selection.InlineShapes.AddPicture(
                '    FileName:=filePath,
                '    LinkToFile:=False,
                '    SaveWithDocument:=True
                ')
                'objInlineShape.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue
                'objInlineShape.Width = imgX
                'objInlineShape.Height = imgY

                '.Application.Selection.InlineShapes.AddPicture(filePath)

                'Dim objRng As Word.Range = .Sections(1).Range()
                'objRng.InlineShapes.AddPicture(filePath)
                '
                'Dim objInlineShape As Word.InlineShape = .InlineShapes.AddPicture(filePath)
                'Dim objShape As Word.Shape = objInlineShape.ConvertToShape()

                'objDoc.Bookmarks.Item("\startofdoc").Range.InlineShapes.AddPicture(filePath)

文档本身只有四个带有一些格式的表格。我到底做错了什么?

【问题讨论】:

  • objDoc.Sections(1).Headers(1).Shapes.AddPicture(filePath, False, True, Width:=imgX, Height:=imgY) 为我工作

标签: vb.net ms-word office-interop


【解决方案1】:

以下代码来自我的一个 VB.Net 插件。

                rng = rng.Rows(1).Cells(1).Range
                rng.Delete()
                rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
                rng.InlineShapes.AddPicture(.SelectedItems(i))

它将图像添加到表格单元格中。 “SelectItems(i)”引用是用户从 msoFileDialogFilePicker 函数中选择的图像文件,您可以将其更改为图像的任何已知文件路径。我发布它是为了向您展示实际代码的示例。

将图像置于文档顶部就像将插入范围设置到该位置一样简单。下面的代码就可以了。

Dim rng as Word.Range
rng = ActiveDocument.Content
rng.Collapse(Word.WdCollapseDirection.wdCollapseStart)
rng.InlineShapes.AddPicture(Your Path)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-16
    • 2021-08-26
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 2017-05-19
    相关资源
    最近更新 更多