如何在VBA中的光标位置插入和图像 [英] How do I insert and image at the cursor position in VBA
问题描述
我尝试过从各种网站复制的以下代码,但我不是程序员。我希望图像在光标位置对面的Word右侧加载。
目前它加载在文档的顶部或第一页的中心如果我使用
.Top = wdShapeCenter
代码如下:
I have tried the following code copied from various websites but I am no programmer. I want the image to load at the right hand side of the screen in Word opposite the cursor position.
Currently it loads at the top of the document or the centre of the first page if I use
.Top = wdShapeCenter
The code is as follows:
Sub Apic()
'
' APic Place Picture at Cursor
'
Dim openDialog As Office.FileDialog
Dim shp As Shape
Set openDialog = Application.FileDialog(msoFileDialogFilePicker)
openDialog.Filters.Clear
openDialog.Filters.Add "JPEG Files", "*.jpg"
openDialog.Filters.Add "Gif Files", "*.gif"
openDialog.Filters.Add "PNG Files", "*.png"
openDialog.Filters.Add "All Files", "*.*"
If openDialog.Show Then
imageName = openDialog.SelectedItems(1)
End If
Set shp = ActiveDocument.Shapes.AddPicture( _
FileName:=imageName, _
SaveWithDocument:=True, _
Anchor:=ActiveDocument.Paragraphs(1).Range)
With shp
.Name = "PictureInsert"
.LockAspectRatio = True
.WrapFormat.AllowOverlap = False
.WrapFormat.Side = wdWrapTight
.WrapFormat.Type = 1
.RelativeHorizontalPosition = _
wdRelativeVerticalPositionMargin
.RelativeVerticalPosition = _
wdRelativeVerticalPositionMargin
.Left = wdShapeRight
' .Top = wdShapeCenter
End With
End Sub
我有什么特里d:
搜索本网站以及大量时间搜索VBA的网页示例
What I have tried:
Searching this site and also a lot of time searching web examples for VBA
推荐答案
替换:
Replace:
Anchor:=ActiveDocument.Paragraphs(1).Range
with
with
Anchor:=Selection.Range
in AddPicture
在光标闪烁的位置插入图像的方法。
更多:选择对象(Word) [ ^ ]
in AddPicture
method to insert image in place where cursor blinks.
More: Selection Object (Word)[^]
这篇关于如何在VBA中的光标位置插入和图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!