0

我正在用 VB 开发一个与 Microsoft Word 中的文档处理有关的项目。我很难在文档中创建具有特定大小的 ImageBox。有人知道如何做到这一点吗?甚至可以做到吗?目标是创建 ImageBox,然后将图像插入此框。图像必须拉伸并获得 ImageBox 的大小。

到目前为止我所做的是:

(...)  
Dim NewSize As Size
NewSize = New Size(Width, Height)
ResizedImage = New Bitmap(ImageToInsert, NewSize)
(...)
WordDocument.AddPicture(DirectoryAddress & "\ResizedImage." & ImageExtension)

但是,此代码的作用是在 Word 文档中插入具有指定大小的图像。我想要的是要拉伸的图像并获得将要创建的 ImageBox 的大小。我希望我足够清楚。

提前致谢!

4

2 回答 2

0

好吧,如果您要查看正在创建的表格的属性,您会发现您尚未创建具有固定高度和宽度的表格。为此,您可以使用以下内容:

NewTable = WordDoc.Tables.Add(para.Range, 1, 1, 0, 0)
NewTable.Cell(1,1).Width(500)
NewTable.Cell(1,1).Height(389)
NewTable.Cell(1, 1).HeightRule(2)

或者,在 VBA 中:

Set NewTable = WordDoc.Tables.Add(para.Range, 1, 1, 0, 0)
NewTable.Cell(1,1).Width = 500
NewTable.Cell(1,1).Height = 389
NewTable.Cell(1, 1).HeightRule = 2
于 2020-03-15T20:43:43.950 回答
0

感谢@CindyMeister 和@macropod,我设法创建了我需要的东西。所以这里是答案:

Dim rng As Word.Range
(...)
rng = para.Range
(...)

Dim img As Image = Image.FromFile(imagePath)
Dim objtable as Word.Table

'In my case I needed a temporary paragraph to be added for my project and later delete it. If you don't need it, just don't declare it.

Dim tempTablePara As Word.Paragraph = WordDoc.Content.Paragraphs.Add() 'Previously declared WordDoc as Word.Document
objtable = WordDoc.Tables.Add(rng, 1, 1)
objtable.Cell(1, 1).Width = img.Width * 0.75 'width is in pixels, convert to points
objtable.Cell(1, 1).Height = img.Height* 0.75 'height is in pixels, convert to points
objtable.Cell(1, 1).HeightRule = Word.WdRowHeightRule.wdRowHeightExactly ' Done so as the cell to get the same size with the picture

Dim objShapes = objtable.Range.InlineShapes

rng = tempTablePara.Range
tempTablePara.Range.Delete()

objShapes.AddPicture(imagePath)

'add cell borders

With objtable.Rows(1).Cells.Borders
.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle
End With

额外的。我一直在寻找的是将图像插入到 word 文档中已经设计好的框架中。所以对我来说,预先设计的框架是一个单格表。如果您只想在图片周围添加一个框架,那么以下代码应该可以正常工作。

Dim shape
(...)

shape = WordDoc.InlineShapes.AddPicture(imagePath, Type.Missing, Type.Missing, rng)
rng = shape.Range
Dim objshape As Word.InlineShape
objshape = shape
objshape.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingleWavy
objshape.Borders.OutsideColor = Word.WdColor.wdColorBlack
rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd)
于 2020-05-14T11:09:59.430 回答