|
代码的功能是
功能是.
自动根据输入的列数 CL
和filedialog 选择的图片数量导入如图的格式.
图片 自适应page宽度.
上下两个表格里面是.加上和不加上箭头指示的那句代码的区别.
有大婶知道原因吗...
以下是代码...
- Option Explicit
- Sub AddPic()
- Dim CL, I&, Fn, ST&, RL&, SI
- Dim W As Double, WW As Double
- If Selection.Information(wdWithInTable) = True Then '在表格中则退出
- MsgBox "请选择非表格区域.", vbCritical + vbOKOnly, "警告..."
- Exit Sub
- End If
- CL = InputBox("请输入插入图片的列数.", "输入...")
- If Not VBA.IsNumeric(CL) Then
- If CL = "" Then Exit Sub
- MsgBox "必须输入数字.", vbCritical + vbOKOnly, "警告..."
- Exit Sub
- End If
- If Selection.Start = ActiveDocument.Content.End - 1 Then '如光标在文末
- Selection.TypeParagraph '在文末添加一空段
- Else
- Selection.EndKey
- End If
- With ActiveDocument.PageSetup
- W = (.PageWidth - .LeftMargin - .RightMargin) / CL
- End With
- Application.ScreenUpdating = False
- With Application.FileDialog(msoFileDialogFilePicker) '选择文件
- .InitialView = msoFileDialogViewList
- .Filters.Add "图片文件", "*.jpg,*.png,*.bmp", 1
- .AllowMultiSelect = True
- If .Show = -1 Then
- ST = .SelectedItems.Count
- If ST Mod CL = 0 Then
- RL = (ST \ CL) * 2
- Else
- RL = ((ST \ CL) + 1) * 2
- End If
- Set SI = .SelectedItems
- Dim R&, C&, K&
- With ActiveDocument.Tables.Add(Selection.Range, RL, CL, 1, 1) '新建表格
- .Borders.Enable = True
- For Each Fn In SI
- K = K + 1
- R = (K - 1) \ CL + 1 '现在行
- C = (K - 1) Mod CL + 1 '现在列
- .Cell(R * 2 - 1, C).Select
- Selection.Delete '加这么奇葩的语句...暂时处理
- With Selection.InlineShapes.AddPicture(FileName:=Fn, SaveWithDocument:=True)
- WW = .Width
- .Width = W
- .Height = .Height * (W / WW)
- End With
- .Cell(R * 2, C).Select
- Selection.Text = Basename(Fn)
- Next Fn
- End With
- End If
- End With
- Selection.EndKey
- Application.ScreenUpdating = True
- MsgBox "ok", vbInformation + vbOKOnly, "提示..."
- End Sub
- Function Basename(FullPath) '取得文件名
- Dim x, y
- Dim tmpstring
- tmpstring = FullPath
- x = Len(FullPath)
- For y = x To 1 Step -1
- If Mid(FullPath, y, 1) = "" Or _
- Mid(FullPath, y, 1) = ":" Or _
- Mid(FullPath, y, 1) = "/" Then
- tmpstring = Mid(FullPath, y + 1)
- Exit For
- End If
- Next
- Basename = Left(tmpstring, Len(tmpstring) - 4)
- End Function
复制代码
|
|