Program Page: セルの文字配置

カテゴリー名: [ExcelVBAとOLE 基本概念とセル操作

2016/08/13

関連の解説ページへ戻る


《このページの目次》


    

1. JScript

△ vovXL09.js

// セルの文字配置
var fso, bookPath;
var ExlApp, wb;
eval(ReadFile("constants_xl.txt"));

fso = WScript.CreateObject("Scripting.FileSystemObject");
bookPath = fso.GetAbsolutePathName("Book1.xls");
if (fso.FileExists(bookPath))  fso.DeleteFile(bookPath);
ExlApp = WScript.CreateObject("Excel.Application");  // Excelの起動
ExlApp.Visible = true;  // Excelを見える状態に
wb = ExlApp.Workbooks.Add();  // Workbookの新規作成
with (wb.ActiveSheet) {
    Range("B1").Value = "標準";
    Range("B1").HorizontalAlignment = xlGeneral;
    Range("C1").Value = "左詰";
    Range("C1").HorizontalAlignment = xlLeft;
    Range("D1").Value = "中央";
    Range("D1").HorizontalAlignment = xlCenter;
    Range("E1").Value = "右詰";
    Range("E1").HorizontalAlignment = xlRight;
    Range("F1").Value = "両端";
    Range("F1").HorizontalAlignment = xlJustify;
    Range("G1").Value = "均等";
    Range("G1").HorizontalAlignment = xlDistributed;

    Range("A2").Value = "上詰";
    Range("A2").VerticalAlignment = xlTop;
    Range("A3").Value = "中央";
    Range("A3").VerticalAlignment = xlCenter;
    Range("A4").Value = "下詰";
    Range("A4").VerticalAlignment = xlBottom;
    Range("A5").Value = "両端";
    Range("A5").VerticalAlignment = xlJustify;
    Range("A6").Value = "均等";
    Range("A6").VerticalAlignment = xlDistributed;

    with (Range("A1:G6")) {
        ColumnWidth = 20;  // セルの幅を半角20文字分に
        RowHeight = 33;  // セルの高さを33ポイントに
    }
}
wb.SaveAs(bookPath, xlWorkbookNormal);
ExlApp.Quit();


function ReadFile(filename) {
    var fso = WScript.CreateObject("Scripting.FileSystemObject");
    var path = fso.GetAbsolutePathName(filename);
    var MyStr = null;
    if (fso.FileExists(path)) {
        var fobj = fso.OpenTextFile(path, 1);
        MyStr = fobj.ReadAll();
        fobj.Close();
    }
    return MyStr;
}

目次に戻る


    

2. VBScript

△ vovXL09.vbs

' セルの文字配置
Option Explicit
Dim FSO, BookPath
Dim EXLapp, WBobj
Include "constants_xl.txt"

Set FSO = CreateObject("Scripting.FileSystemObject")
BookPath = FSO.GetAbsolutePathName("Book1.xls")
If (FSO.FileExists(BookPath) = True) Then FSO.DeleteFile(BookPath)
Set EXLapp = CreateObject("Excel.Application")  ' Excelの起動
EXLapp.Visible = True  ' Excelを見える状態に
Set WBobj = EXLapp.Workbooks.Add()  ' Workbookの新規作成
With WBobj.ActiveSheet
    .Range("B1").Value = "標準"
    .Range("B1").HorizontalAlignment = xlGeneral
    .Range("C1").Value = "左詰"
    .Range("C1").HorizontalAlignment = xlLeft
    .Range("D1").Value = "中央"
    .Range("D1").HorizontalAlignment = xlCenter
    .Range("E1").Value = "右詰"
    .Range("E1").HorizontalAlignment = xlRight
    .Range("F1").Value = "両端"
    .Range("F1").HorizontalAlignment = xlJustify
    .Range("G1").Value = "均等"
    .Range("G1").HorizontalAlignment = xlDistributed

    .Range("A2").Value = "上詰"
    .Range("A2").VerticalAlignment = xlTop
    .Range("A3").Value = "中央"
    .Range("A3").VerticalAlignment = xlCenter
    .Range("A4").Value = "下詰"
    .Range("A4").VerticalAlignment = xlBottom
    .Range("A5").Value = "両端"
    .Range("A5").VerticalAlignment = xlJustify
    .Range("A6").Value = "均等"
    .Range("A6").VerticalAlignment = xlDistributed

    With .Range("A1:G6")
        .ColumnWidth = 20  ' セルの幅を半角20文字分に
        .RowHeight = 33  ' セルの高さを33ポイントに
    End With
End With
WBobj.SaveAs BookPath, xlWorkbookNormal
EXLapp.quit

Sub Include(ByVal FileName)
    Dim FSO, FileObj, MyStr
    Set FSO = CreateObject("Scripting.FileSystemObject") 
    Set FileObj = FSO.OpenTextFile(FSO.GetAbsolutePathName(FileName))
    MyStr = FileObj.ReadAll()
    FileObj.Close
    Set FSO = Nothing
    Set FileObj = Nothing
    ExecuteGlobal MyStr
End Sub

〜 以上 〜