如何解决在Word VBA中复制图像
我在Word UserForm中有一个图像控件,还有一个按钮,该按钮打开一个对话框让用户选择图像。如果要将此图像复制到另一个文档,该如何通过代码来实现?
以下是通过按钮选择图片的代码:
Dim fd As FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
.Title = "Select Photo"
.Filters.Clear
.Filters.Add "Images","*.gif; *.jpg; *.jpeg",1
If .Show = -1 Then
filetoinsert = .SelectedItems(1)
For Each vrtSelectedItem In .SelectedItems
Next vrtSelectedItem
Me.myImage.Picture = LoadPicture(.SelectedItems(1))
Else
End If
End With
Set fd = Nothing
End Sub
At the bottom of this form,I have another button that will copy this photo to another document. Here's what i've tried so far:
Private Sub cmdTransferPhoto_Click()
'Copy and Paste photo on "NextDoc" document. On the this document,I have a Picture Content Control called "Picture" where i plan to paste the copied image
Set Picture = NextDoc.SelectContentControlsByTitle("Picture").Item(1)
Picture.Range.InlineShapes.AddPicture _
Me.myImage.Picture,linktofile:=False,savewithdocument:=True
End Sub
When I click,the cmdTransferPhoto button,I get this error
Run-time error '5152'
This is not a valid file name.
Try one or more of the following:
* Check the path to make sure it was type correctly.
*Select a file from the list of files and folders.
Ideas are welcome! Thanks.
解决方法
正如您可能从the documentation中发现的那样,并且应该从Intellisense中注意到, a b c d
0 1 0 0 0
1 1 0 0 0
2 0 0 0 1
3 0 0 1 0
将数据类型字符串.InlineShapes.AddPicture
作为必需参数。当您尝试将图像(质量较差的位图)传递给该参数时,它自然会失败。
您已经获得了用于将图片加载到图像控件中的文件名,为什么不使用它呢?
FileName
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。