如何将图像放在vba的最后一段下面?

时间:2016-07-12 09:16:51

标签: vba

我使用VBA创建了一个word文档,并在其中添加了图片。
但是,我想将图片准确地放在最后一段而不是固定位置 在我在代码中添加PageSetup部分后,锚似乎出现故障。我做错了什么吗? 如果我不能使用锚点,是否可以将图片放在最后一段的正下方?

Public Sub create()

Dim certpara As Word.Paragraph
Dim certdoc As Word.Document
Dim certapp As Word.Application

Set certapp = New Word.Application

certapp.Visible = True

Set certdoc = certapp.Documents.Add

certdoc.PageSetup.TopMargin = certapp.InchesToPoints(0.59)
certdoc.PageSetup.BottomMargin = certapp.InchesToPoints(0.39)
certdoc.PageSetup.LeftMargin = certapp.InchesToPoints(0.79)
certdoc.PageSetup.RightMargin = certapp.InchesToPoints(0.79)

Dim i As Integer
Dim v As Integer

v = Range("A1").Value

For i = 1 To v
    certdoc.Paragraphs.Add
    Set certpara = certdoc.Paragraphs.Last
    certpara.Range.Text = "aaa"
Next i

Dim location As String
location = "D:\C3000.jpg"

certdoc.Shapes.AddPicture Filename:=location, _
LinkToFile:=False, _
SaveWithDocument:=True, _
Left:=300, _
Top:=230, _
Anchor:=certpara.Range, _
width:=130, _
Height:=91

certdoc.Shapes(1).WrapFormat.Type = wdWrapBehind

End Sub

1 个答案:

答案 0 :(得分:0)

您可以在插入最后一个段落后直接为该位置添加书签并插入该位置(而不是锚点),或插入重新定义的范围。由于第二个涉及较少:

定义图像位置后:

certpara.Collapse wdCollapseStart
'I had some trouble understanding your question so use the next line at your discretion
certpara.MoveEnd Unit:=wdCharacter, Count:=-1 
certpara.Select

ActiveDocument.Shapes.AddPicture FileName:=location, _
LinkToFile:=False, _
SaveWithDocument:=True, _
Anchor:=Selection.Range