用户登录
用户注册

分享至

wordvba段落

  • 作者: 故人难聚c
  • 来源: 51数据库
  • 2020-06-05

1. word vba 插入图片

Sub 批量插入图片()

Dim myfile As FileDialog

Set myfile = Application.FileDialog(msoFileDialogFilePicker)

With myfile

.InitialFileName = "E:\工作文件" ‘这里输入你要插入图片的目标文件夹

If .Show = -1 Then

For Each Fn In .SelectedItems

Selection.Text = Basename(Fn) '这两句移到这里

Selection.EndKey

If Selection.Start = ActiveDocument.Content.End - 1 Then '如光标在文末

Selection.TypeParagraph '在文末添加一空段

Else

Selection.MoveDown

End If

Set MyPic = Selection.InlineShapes.AddPicture(FileName:=Fn, SaveWithDocument:=True) '按比例调整相片尺寸

WidthNum = MyPic.Width

c = 6 '在此处修改相片宽,单位厘米

MyPic.Width = c * 28.35

MyPic.Height = (c * 28.35 / WidthNum) * MyPic.Height

If Selection.Start = ActiveDocument.Content.End - 1 Then '如光标在文末

Selection.TypeParagraph '在文末添加一空段

Else

Selection.MoveDown

End If

Next Fn

Else

End If

End With

Set myfile = Nothing

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

执行此代码后,弹出的选择对话框, 全选目标文件夹下的所有图片文件之后,点击确定。然后静静的等待电脑完成处理工作,次数word会进入无响应状态。图片越多,无响应的时间越长。

2. vba如何设置word图片格式

一、旋转图片

Dim blnIsInlineShape As Boolean

If Selection.Type = wdSelectionInlineShape Then

blnIsInlineShape = True

Selection.InlineShapes(1).ConvertToShape

End If

Dim intTurn As Integer

intTurn = InputBox("请输入图形要旋转的角度值" & vbCrLf &; "正数表示顺时针,负数表示逆时针。", "图形旋转", 30)

Selection.ShapeRange.IncrementRotation intTurn

End Sub

二、将文档中的每张图片的版式转换为嵌入式图形

For Each s In Documents("MyDoc.doc").Shapes

If s.Type = msoPicture Then

s.ConvertToInlineShape

End If

Next s

三、设置图片的高度宽度

Mywidth=10'10为图片宽度(厘米)

Myheigth=10'10为图片高度(厘米)

For Each iShape In ActiveDocument.InlineShapes

iShape.Height = 28.345 * Myheigth

iShape.Width = 28.345 * Mywidth

Next iShape

四、得到图片的像素

Sub 获取嵌入型图片的像素()

On Error Resume Next

With Selection.Range.Find

.ClearFormatting

.Replacement.ClearFormatting

.Execute findtext:="^g", MatchWildcards:=False, Wrap:=wdFindStop

MsgBox "该图片的像素为:" & Selection.InlineShapes(1).Width _

& " * " & Selection.InlineShapes(1).Height

End With

End Sub

五、复制图片到word文档中

Dim objWordApp As Word.Application

Dim objWord As Word.Document

Range(Cells(3, 2), Cells(11, 11)).Select

Selection.CopyPicture

Set objWordApp = CreateObject("Word.Application")

Set objWord = objWordApp.Documents.Add

objWord.Application.Visible = True

objWord.Application.Selection.Paste

Set objWord = Nothing

Set objWordApp = Nothing

3. 在word中如何用VBA实现插入多张图片

要从两个方面考虑:

1、如何把现有文档中的图片导出?这个代码片断可以参考一下:

Set ImageStream = CreateObject("ADODB.Stream")

With ImageStream

.Type = 1

.Open

.Write ActiveDocument.InlineShapes(1).Range.EnhMetaFileBits

.SaveToFile "d:\Temp\Output.bmp"

.Close

End With

Set ImageStream = Nothing

2、如何把已经到处的图片导入到新文档中?这个函数调用可以参考一下:

ActiveDocument.InlineShapes.AddPicture

如果还是不明白的话,请补充提问。

_____

补充:

请问楼主你运行那个代码片断得到的*完整*错误信息是什么?报错的时候,系统一般会把光标移动到出错的那行代码上,你看到的是哪一行代码出错呢?

我看你贴上来的代码应该是没有什么语法上的错误,“Exit For”的确是不应该要的,但那也不是语法错误啊。

4. Word中如何用VBA 将所有图片加上边框

只要一句代码?那就给你一句参考一下吧:

picCount = ActiveDocument.InlineShapes.Count '计算文件中图片数目

在Word中,插入的图片已被转化为 InlineShape 对象。

之后用For循环语句,给所有图片加黑色边框。

单个图片加边框的语句,你自己可以录制一个宏看看,将录制的宏代码拷贝到For循环中修改一下即可。

有问题,再补充问好了。

___________________________________________________________________

单个图片加边框,你自己录制宏就可以看到代码了,这是学习VBA的必由之路啊。

选中一张图片,工具-宏-录制新宏,然后,格式-边框和阴影,给图片加上黑边框,然后,alt+F11打开VB编译器,就看到代码了。本想只授人以渔即足够了,但犹豫了一下,还是贴给你吧:

With Selection.InlineShapes(1)

With .Borders(wdBorderLeft)

.LineStyle = wdLineStyleSingle

.LineWidth = wdLineWidth050pt

.Color = wdColorAutomatic

End With

With .Borders(wdBorderRight)

.LineStyle = wdLineStyleSingle

.LineWidth = wdLineWidth050pt

.Color = wdColorAutomatic

End With

With .Borders(wdBorderTop)

.LineStyle = wdLineStyleSingle

.LineWidth = wdLineWidth050pt

.Color = wdColorAutomatic

End With

With .Borders(wdBorderBottom)

.LineStyle = wdLineStyleSingle

.LineWidth = wdLineWidth050pt

.Color = wdColorAutomatic

End With

.Borders.Shadow = False

End With

With Options

.DefaultBorderLineStyle = wdLineStyleSingle

.DefaultBorderLineWidth = wdLineWidth050pt

.DefaultBorderColor = wdColorAutomatic

End With

计算图片数目是为了For循环用的,有多少图片就要循环多少次,给所有图片都加上边框。

For i = 1 to picCount

Next i

中间就是上面那段代码,把InlineShapes(1)改为InlineShapes(i)

5. 如何用vba取得一个word文件中的所有的图片

提供网上示例代码供参考(此代码的思路是遍历 word 文档中的 Shapes ,缩放到原始图片尺寸,再转粘贴到 Excel 中,借用 Excel 的 ChartObjects 提供的导出功能实现图片原样导出):

Sub test()

Rem 工具--引用--勾选 Microsoft Excel x.x Object Library..

Dim myshape As Object, ExcelApp As New Excel.Application

Dim Excel As Workbook, i%, z%

Set Excel = ExcelApp.Workbooks.Add

For Each myshape In ActiveDocument.InlineShapes

If myshape.Type = 3 Then

i = i + 1

myshape.Select

Set myshape = myshape.ConvertToShape

Rem 以下代码将图片以原始比例展示

With myshape

.ScaleHeight 1, True, msoScaleFromMiddle

.ScaleWidth 1, True, msoScaleFromMiddle

End With

Selection.Copy

With Excel.ActiveSheet.ChartObjects.Add(0, 0, myshape.Width, myshape.Height).Chart

.Paste

.Export ActiveDocument.Path & "" & i & ".png"

.Parent.Delete

End With

End If

Next

Excel.Close False

ExcelApp.Quit

End Sub

6. 如何用VBA中获取word文档中的图片

于图片来说,,分为二种的,,

第一种:是Shape对象(Word中自带的“自选图形”)

第二种:是InlineShape对象(嵌入式图片,也就是所谓的通过点击菜单栏中的“插入”->“图片”->“来自文件”)

-------------------

如何判断word文档中是否存在一张图片呢?这个问题。

我来解答:判断word文档中是否存在一张图片,可以通过看一看word文档中的inlineshape对象的数量来判断。

举例:

Dim doc As Document '当前打开的文档对象Document

Dim MyApp As Word.Application 'Word应用程序对象

Dim MyFileName As String '当前打开的文件名

MyFileName="c;\aaa.doc" '此处可以改成你的文件名字

Set MyApp = CreateObject("Word.Application")

Set doc = MyApp.Documents.Open(MyFileName)

If doc.InlineShapes.Count <> 0 then

msgbox "有嵌入式图片存在!"

Else

msgbox "没有嵌入式图片存在!"

End if

-------------------------------------------------------

如果你非要想知道,用户到底是不是插入了你要求的图片的话,你要必须要再他进行插入图片操作时进行控制,也就是说,你必须把用户所插入的图片的(绝对地址)或者(图片名字)添加到用户所插入图片的“可选文字”中记录下来。

MydocInlineShapes.AddPicture(FileName, LinkToFile, SaveWithDocument, Range)

FileName String 类型,必需。指定要创建对象的文件名。如果忽略本参数,则使用当前文件夹。

LinkToFile Variant 类型,可选。如果此属性设置为 True,则将 OLE 对象与创建该对象的文件链接。如果设置为 False,则令该 OLE 对象成为该文件的独立副本。默认值为 False。

SaveWithDocument Variant 类型,可选。如果此属性为 True,则将链接的图片与文档一起保存。默认值为 False。

Range Variant 类型,可选。指定一个区域,OLE 对象放置在该区域的文字中。如果该区域未折叠,那么图片将覆盖该区域。如果忽略此参数,则自动放置图片。

ActiveDocument.Shapes.AddPicture "C:\stone.bmp",True, True,MyRange

ActiveDocument.InlineShapes(ID).AlternativeText = App.Path & "\temp" & shapeII & ".bmp" '添加“可选文字”

转载请注明出处51数据库 » wordvba段落

软件
前端设计
程序设计
Java相关