0

我运行了一个宏,如下所示:

Sub Macro1()
'
' Macro1 Macro
'

'
    Range("D7").Select
    ActiveSheet.OLEObjects.Add(Filename:= _
        "C:\Documents and Settings\rakshiar\Desktop\\Operating+System+Concepts.pdf" _
        , Link:=False, DisplayAsIcon:=True, IconFileName:= _
        "C:\WINDOWS\Installer\{AC76BA86-7AD7-FFFF-7B44-AB0000000001}\PDFFile_8.ico", _
        IconIndex:=0, IconLabel:="Operating+System+Concepts.pdf").Select

    End Sub

并尝试将其转换为VBScript代码,如下所示:

excel = WIN32OLE.new('Excel.Application')
excel.visible = true
wb=excel.workbooks.open("E:\\WIPData\\Ruby\\Scripts\\GSL_File_DownLoad.xlsx")
wbs= wb.Worksheets(1)
wbs.cells(rows,2).values = wbs.OLEObjects.Add(,full_path,False,True, IconFileName:= _
               "C:\WINDOWS\Installer\{AC76BA86-7AD7-FFFF-7B44-AB0000000001}\PDFFile_8.ico",_
               IconIndex:=0,"Operating+System+Concepts.pdf")

但现在无法理解如何以及如何提出IconIndexIconFileName论点?

我有 .pdf、.tif、.docs、docx、.gif 等扩展文件,我需要使用脚本将它们附加到 Excel 列。那么在运行时我将如何获得"C:\WINDOWS\Installer\{AC76BA86-7AD7-FFFF-7B44-AB0000000001}\PDFFile_8.ico".pdf 文件的值?

请在这里帮助我。

4

1 回答 1

1

您可以使用这样的变量Set ol = ws.OLEObjects.Add(, FileToBeinserted, False, True, icoFile, icoIndex,lbl),在执行上述语句之前,只需确定您要插入的文件类型,然后设置相关图标文件的路径icoFile

这是一个适用于选定扩展的示例(TRIED AND TESTED )。请酌情修改

'~~> Sample icons for relevant files
Const pdfIco = "D:\Temp\icons\ARROWS\AARROW.ico"
Const wrdIco = "D:\Temp\icons\ARROWS\ANYWHERE.ico"
Const TiffIco = "D:\Temp\icons\ARROWS\ARROW1.ico"
Const OtherFormat = "D:\Temp\icons\ARROWS\ARROW1A.ico"

Dim Xl, Wb, Ws, ol, FileToBeInserted, lbl

Set Xl = CreateObject("Excel.Application")
Xl.Visible = True

Set Wb = Xl.Workbooks.Add
Set Ws = Wb.Sheets(1)

'~~> Let user select a file
FileToBeInserted = Xl.GetOpenFilename("All Files (*.*), *.*")

'~~> Check if the user selected a file
If FileToBeInserted <> False Then
    '~~> Get the file name for the label
    lbl = GetFileName(FileToBeInserted)

    icoIndex = 0

    '~~> I am testing for just 3 extensions. Amend as applicable
    Select Case GetFileExtn(FileToBeInserted)
    Case "PDF"
        icoFile = pdfIco
    Case "DOCX", "DOC"
        icoFile = wrdIco
    Case "Tiff"
        icoFile = TiffIco
    Case Else
        icoFile = OtherFormat
    End Select

    '~~> Create the object
    Set ol = Ws.OLEObjects.Add(, FileToBeInserted, False, True, icoFile, icoIndex, lbl)
End If

'~~> Function to get the File extention
Function GetFileExtn(sFile)
    GetFileExtn = Trim(UCase(Right(sFile, Len(sFile) - InStrRev(sFile, "."))))
End Function

'~~> Function to get the file name
Function GetFileName(sFile)
    GetFileName = Left(sFile, (InStrRev(sFile, ".", -1, 1) - 1))
End Function
于 2013-01-28T12:59:59.233 回答