I'm trying to replace a bookmarked picture in a MS word document so, that my program opens another MS Word file where it gets the replacement picture by a bookmark. For some reason the excecution ends up in the following error message:
System.Runtime.InteropServices.COMException: 'This method or property is not available because the Clipboard is empty or not valid.'
The pictures are bookmarked so that the bookmark range is only the picture itself. The code works fine, when a line break is bookmarked witch the picture. I would like this to work so, that the bookmark range is only the picture.
Heres the what I was trying:
'Button click opens my base document
Private Sub test123_Click(sender As Object, e As EventArgs) Handles test123.Click
oWord = CreateObject("Word.Application")
oWord.Visible = True
path = "C:\Users\user\basefile\"
file = Dir(path & "*.docx")
oDoc = oWord.Documents.Add(path & file)
If Me.checkbox.Checked = False Then
'Open the source file
Dim sourceFile As String = "C:\Users\user\sourcefile\source.docx"
Dim sourceDoc As Word.Document = oWord.Documents.Open(sourceFile)
'Copy the bookmarked figure from the source file
Dim sourceBookmark As String = "bkm_src"
Dim sourceRange As Word.Range = sourceDoc.Bookmarks(sourceBookmark).Range
sourceRange.CopyAsPicture()
'Close the source file
sourceDoc.Close()
'Replace the bookmarked figure in the destination file with the copied one
Dim destBookmark As String = "bkm_dest"
Dim destRange As Word.Range = oDoc.Bookmarks(destBookmark).Range
destRange.Delete() ' Added a delete method to remove the existing picture
destRange.PasteSpecial() ' Pasted the copied picture
End If
How about using FormattedText property instead?