0

code executed inside the excel file (xlsm) work perfectly

 Sub insertObject()
    Set objExcel = CreateObject("Excel.Application")
    Set objWorkbook = objExcel.Workbooks.Open("YourPath\Datasheet.xlsx")
    objExcel.Application.Visible = True
    objWorkbook.ActiveSheet.OLEObjects.Add(Filename:="YourPath\Placeholder.txt", _
            Link:=False, DisplayAsIcon:=True, IconFileName:= _
            "C:\Windows\system32\packager.dll", IconIndex:=0, IconLabel:= _
            "C:\Placeholder.txt").Select
    objWorkbook.SaveAs "yourPath\test.xlsx"
    objExcel.ActiveWorkbook.Close
    objExcel.Application.Quit
    WScript.Quit
    End Sub

this is the code i try to execute using vbs script

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("YourPath\Datasheet.xlsx")
objExcel.Application.Visible = True
ActiveSheet.OLEObjects.Add(Filename:="YourPath\Placeholder.txt", _
        Link:=False, DisplayAsIcon:=True, IconFileName:= _
        "C:\Windows\system32\packager.dll", IconIndex:=0, IconLabel:= _
        "C:\Placeholder.txt").Select
objWorkbook.SaveAs "YourPath\test.xlsx"
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit
WScript.Quit

i replace the paths with the real ones of course the code work perfectly inside the macro but failed inside vbs script when i try to run it using the cmd command.

2
  • Failed in what way? What happended? Did you get error messages/codes? Commented Jul 25, 2018 at 11:39
  • the code is 800A03Ec , methode add of OLEObjects failed Commented Jul 25, 2018 at 13:20

1 Answer 1

2

I get the same thing when I run your code. I tried a few different tweaks and it works: you simply forgot the first parameter of the add function of OLEobject and dont forget to remove the parameters names :

 Set objExcel = CreateObject("Excel.Application")
    Set objWorkbook = objExcel.Workbooks.Open("YourPath\Datasheet.xlsx")
    objExcel.Application.Visible = false
    Set ob = objWorkbook.ActiveSheet.OLEObjects
    Set t = ob.Add (,"YourPath\Placeholder.txt",False, True,    "C:\Windows\system32\packager.dll", 0,"C:\Placeholder.txt")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.