宏插入和更改标题中的图片所有表格都很出色

时间:2015-10-17 11:49:18

标签: excel vba excel-vba

我不是专业人士,但我试图找到一种方法,我创建的Excel工作簿的用户可以选择并在excel工作簿中的所有工作表的左标题中插入图片(公司徽标)。 如果可能的话,也应该可以改变图像,即删除当前图像(如果有的话)并插入新的图像。我搜索并找到了一些使用宏的解决方案,但还没有找到我想要实现的完整解决方案。感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

这似乎工作正常(使用虚拟打印机

Option Explicit

Public Sub setLeftHeaderImage()
    Const TITLE     As String = "Select Header Image"
    Const IMG_TYPES As String = "All Files (*.*),*.*,Jpg (*.jpg),*.jpg,Png (*.png),*.png"

    Dim selectedFile As Variant, iniName As String, ws As Worksheet

    selectedFile = Application.GetOpenFilename(FileFilter:=IMG_TYPES, TITLE:=TITLE)
    If selectedFile <> False Then
        For Each ws In Worksheets
            With ws.PageSetup
                Application.PrintCommunication = False
                With .LeftHeaderPicture
                    .Filename = selectedFile
                    .Height = 71.25
                    .Width = 117.75
                    .Brightness = 0.36
                    .ColorType = msoPictureAutomatic     'msoPictureWatermark
                End With
                Application.PrintCommunication = True
                On Error GoTo ShowError    'https://support.microsoft.com/en-us/kb/291298
                .LeftHeader = "&G"         '"&G" Enable the image to show up in left header
                .Zoom = 100
                .ScaleWithDocHeaderFooter = True
                .AlignMarginsHeaderFooter = True
            End With
        Next
        Exit Sub
ShowError:
        MsgBox "Setup a printer and 'Print Spooler' service; (" & Err.Description & ")"
    End If
End Sub

感谢EEM的建议!