【发布时间】:2021-08-17 22:05:20
【问题描述】:
在我的项目中,我有一个用于将图像添加到文档的宏。在我的用户窗体中,我有不同的组合框和按钮来添加图像和描述。当我运行宏时,第二张图像与第一张图像重叠,如下图所示。我试图在第一张图片后换行,但我做不到。
我的 vba 代码
Private Sub addLPC15_1_Click()
Dim doc As Word.Document
Dim fd As FileDialog
Dim vItem As Variant
Dim mg1 As Range
Dim mg2 As Range
Dim objInlineShape As Shape
Dim objShape As Shape
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Set doc = ActiveDocument
With fd
.Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 1
.FilterIndex = 1
If .Show = -1 Then
For Each vItem In .SelectedItems
Set mg2 = ActiveDocument.Range
mg2.Collapse wdCollapseEnd
doc.Shapes.AddPicture _
FileName:=vItem, _
LinkToFile:=False, SaveWithDocument:=True
Set mg1 = ActiveDocument.Range
mg1.Collapse wdCollapseEnd
mg1.Text = "Situation: " & cmbLPC15_1.Value & vbNewLine & "Finding Type: " & cmbLPC15_11.Value & vbNewLine & "Task: " & cmbLPC15_21 & vbNewLine & "Comment: " & txtLPC15_1
Next vItem
End If
End With
For Each objInlineShape In ActiveDocument.Shapes
objInlineShape.ScaleHeight 0.5, msoTrue
objInlineShape.ScaleWidth 0.5, msoTrue
objInlineShape.WrapFormat.Type = wdWrapSquare
objInlineShape.LockAspectRatio = msoFalse
Next objInlineShape
Set fd = Nothing
End Sub
Private Sub addLPC15_2_Click()
Dim doc As Word.Document
Dim fd As FileDialog
Dim vItem As Variant
Dim mg1 As Range
Dim mg2 As Range
Dim objInlineShape As Shape
Dim objShape As Shape
Set fd = Application.FileDialog(msoFileDialogFilePicker)
Set doc = ActiveDocument
Selection.InsertBreak Type:=wdLineBreak
With fd
.Filters.Add "Images", "*.gif; *.jpg; *.jpeg", 1
.FilterIndex = 1
If .Show = -1 Then
For Each vItem In .SelectedItems
Set mg2 = ActiveDocument.Range(Start:=20)
mg2.Collapse wdCollapseEnd
doc.Shapes.AddPicture _
FileName:=vItem, _
LinkToFile:=False, SaveWithDocument:=True
Set mg1 = ActiveDocument.Range(Start:=21)
mg1.Collapse wdCollapseEnd
mg1.Text = "Situation: " & cmbLPC15_2.Value & vbNewLine & "Finding Type: " & cmbLPC15_12.Value & vbNewLine & "Task: " & cmbLPC15_22 & vbNewLine & "Comment: " & txtLPC15_2
Next vItem
End If
End With
For Each objInlineShape In ActiveDocument.Shapes
objInlineShape.ScaleHeight 0.5, msoTrue
objInlineShape.ScaleWidth 0.5, msoTrue
objInlineShape.WrapFormat.Type = wdWrapSquare
objInlineShape.LockAspectRatio = msoFalse
Next objInlineShape
Set fd = Nothing
End Sub
【问题讨论】: