在 Microsoft Word 文档中创建 ImageBox 并使用 Visual Basic 插入图像
Create ImageBox in Microsoft Word document and insert image with Visual Basic
我正在 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 的大小。我希望我已经足够清楚了。
提前致谢!
好吧,如果您要查看正在创建的 table 的属性,您会发现您 not 创建了一个 table 具有固定的高度和宽度。为此,您可以使用类似的东西:
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
感谢@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 文档中已经设计好的框架中。所以对我来说,预先设计的框架是一个单元格 table。如果您只想在图片周围添加一个框架,那么下面的代码应该可以正常工作。
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)
我正在 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 的大小。我希望我已经足够清楚了。
提前致谢!
好吧,如果您要查看正在创建的 table 的属性,您会发现您 not 创建了一个 table 具有固定的高度和宽度。为此,您可以使用类似的东西:
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
感谢@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 文档中已经设计好的框架中。所以对我来说,预先设计的框架是一个单元格 table。如果您只想在图片周围添加一个框架,那么下面的代码应该可以正常工作。
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)