1 (изменено: Mat Skywalker, 2011-02-08 15:44:12)

Тема: VBS + VBA Запуск макроса в Excel

Внутри скрипта VBS есть вот такой код:

'===================================        
i = objExcel.VBE.ActiveVBProject.VBComponents.Count
objExcel.VBE.ActiveVBProject.VBComponents.Add 1
Set objModule = objExcel.VBE.ActiveVBProject.VBComponents.Item(i + 1)
With objModule.CodeModule
    .InsertLines 1,  "Sub TestSub()"
    .InsertLines 2,  "    Cells.Select"
    .InsertLines 3,  "    Selection.NumberFormat = ""@"""
    .InsertLines 4,  "    Range(""A1:Q1"").Select"
    .InsertLines 5,  "    With Selection"
    .InsertLines 6,  "        .HorizontalAlignment = xlGeneral"
    .InsertLines 7,  "        .VerticalAlignment = xlCenter"
    .InsertLines 8,  "        .WrapText = True"
    .InsertLines 9,  "        .Orientation = 0"
    .InsertLines 10,  "        .AddIndent = False"
    .InsertLines 11,  "        .IndentLevel = 0"
    .InsertLines 12,  "        .ShrinkToFit = False"
    .InsertLines 13,  "        .ReadingOrder = xlContext"
    .InsertLines 14,  "        .MergeCells = False"
    .InsertLines 15,  "    End With"
    .InsertLines 16,  "    Selection.Borders(xlDiagonalDown).LineStyle = xlNone"
    .InsertLines 17,  "    Selection.Borders(xlDiagonalUp).LineStyle = xlNone"
    .InsertLines 18,  "    Selection.Borders(xlEdgeLeft).LineStyle = xlNone"
    .InsertLines 19,  "    Selection.Borders(xlEdgeTop).LineStyle = xlNone"
    .InsertLines 20,  "    With Selection.Borders(xlEdgeBottom)"
    .InsertLines 21,  "        .LineStyle = xlContinuous"
    .InsertLines 22,  "        .ColorIndex = 0"
    .InsertLines 23,  "        .TintAndShade = 0"
    .InsertLines 24,  "        .Weight = xlThin"
    .InsertLines 25,  "    End With"
    .InsertLines 26,  "    Selection.Borders(xlEdgeRight).LineStyle = xlNone"
    .InsertLines 27,  "    Selection.Borders(xlInsideVertical).LineStyle = xlNone"
    .InsertLines 28,  "    Selection.Borders(xlInsideHorizontal).LineStyle = xlNone"
    .InsertLines 29,  "    Range(""A1"").Select"
    .InsertLines 30,  "    End Sub"
End With
objExcel.Application.Run "TestSub"
'===================================

Уважаемые, подскажите, как текст VBA кода подгрузить из текстового файла?

Автоматизирую торговлю. Не 1С

2

Re: VBS + VBA Запуск макроса в Excel

Mat Skywalker пишет:

... как текст VBA кода подгрузить из текстового файла?

Используйте метод AddFromFile объекта CodeModule

3

Re: VBS + VBA Запуск макроса в Excel

Что вроде:

objModule.CodeModule.AddFromFile("имя и путь к текстовику")

Так?

Автоматизирую торговлю. Не 1С

4

Re: VBS + VBA Запуск макроса в Excel

Так

Application.VBE.ActiveVBProject.VBComponents("Module1").CodeModule.AddFromFile "C:\vba_module.txt"

или вот так

ThisWorkbook.VBProject.VBComponents.Import "C:\vba_module.txt"
Времени не хватает... :-(

5

Re: VBS + VBA Запуск макроса в Excel

Спасибо. Вы супер.

Автоматизирую торговлю. Не 1С