最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

vba - Insert a preselected image into a table cell when table is within a text box - Stack Overflow

programmeradmin0浏览0评论

My template creates a report based on UserForm inputs. It works fine except for inserting an image into a specific table cell; the table is within a text box. This is so that the image can then be moved by the user to "overlay" onto a larger image of a document. The table is used because there are actually two images side by side (a before/after situation). The table also has a description title in the first row and captions under where each photo would be inserted. The description and captions are both bookmarks and those populate as designed. The image will not insert. This is the code (below) that I have be trying. It was suggested by CoPilot AI. I've tried several variations on this same code; none of which have worked.

Private SelectedPhotoPath As String

Private Sub Select_Photo_Command_Button_Click()
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    
    With fd
        .Title = "Select a Photo"
        .InitialFileName = "C:\Desktop"
        .Filters.Clear
        .Filters.Add "Image Files", "*.jpg; *.jpeg; *.png; *.bmp; *.gif"
        .AllowMultiSelect = False
        If .Show = -1 Then
            SelectedPhotoPath = .SelectedItems(1)
            Image_Preview.Picture = LoadPicture(SelectedPhotoPath)
        End If
    End With
    
End Sub

Sub InsertPhotoIntoTable()
Dim wdDoc As Document
    Dim tbl As Table
    Dim cell As cell
    Set wdDoc = ActiveDocument
    Set tbl = wdDoc.Tables(1) 
    Set cell = tbl.cell(Row:=2, Column:=1)
    
    ' Insert the photo into the cell
    Dim oShape As InlineShape
    Set oShape = cell.Range.InlineShapes.AddPicture(FileName:=SelectedPhotoPath, LinkToFile:=False, SaveWithDocument:=True)
    
    ' Resize the photo to fill the cell
    With oShape
        .LockAspectRatio = msoFalse
        .Width = InchesToPoints(1.5)
        .Height = InchesToPoints(1.5)
    End With

End Sub

Private Sub New_Report_Command_Button_Click()
    InsertPhotoIntoTable
End Sub
发布评论

评论列表(0)

  1. 暂无评论