Skip to content

Instantly share code, notes, and snippets.

@Nia-TN1012
Forked from Myoga1012/Calendar.vb
Last active January 30, 2023 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nia-TN1012/7059b4e6ad4cb51d317067a0a988d19f to your computer and use it in GitHub Desktop.
Save Nia-TN1012/7059b4e6ad4cb51d317067a0a988d19f to your computer and use it in GitHub Desktop.
Wordのドキュメントに、カレンダーをMicrosoft数式3.0の行列として出力するVBAのモジュールです。
' 名前 : Nia Tomonaka
' Twitter : https://twitter.com/nia_tn1012
Option Explicit
Sub Calendar()
' 現在の日付を取得します。
Dim now As Date
now = Date
' 当月1日を表すインスタンスを生成します。
Dim iday As Date
iday = DateSerial(Year(now), Month(now), 1)
Dim seq As Integer
seq = Weekday(iday) - 1 ' 日曜日が1、月曜日が2、・・・となっています。
ActiveDocument.range.Delete ' 文章をクリアします。
Dim calStr As String
' 1日の位置に合わせてオフセットします。
Dim i As Integer
For i = 1 To seq
calStr = calStr + "&"
Next
Do
' 日付を出力します。
calStr = calStr + CStr(Day(iday))
' idayの日付を1日分進めます。
iday = DateAdd("D", 1, iday)
' Microsoft数式3.0の行列は、&が列の区切りで、@が行の区切りです。
If Month(iday) = Month(now) Then
' 土曜日で改行します。
If (seq Mod 7) = 6 Then
calStr = calStr + "@"
Else
calStr = calStr + "&"
End If
End If
' seqをインクリメントします。
seq = seq + 1
Loop While Month(iday) = Month(now) ' idayの月が変わるまで繰り返します。
' カレンダーを行列として表示します。
Dim oRange As range
Set oRange = Selection.range
' VBAで数式入力する場合、オートコレクト後の文字列を入力します。
' "■"は行列(\matrix)です。
oRange.Text = "■(" + calStr + ")"
Selection.OMaths.Add(oRange).OMaths.BuildUp
End Sub
' Calender.vb ( WordのVBA用 )
' Copyright (c) 2014-2023 Nia T.N. Tech Lab. / Chronoir.net.
' This software is released under the MIT License.
' http://opensource.org/licenses/mit-license.php
@Nia-TN1012
Copy link
Author

GitHubのアカウント統合のため、Myoga1012→Nia-TN1012に移行しました。

旧URL: https://gist.github.com/Myoga1012/c4ee441aa2684d1be56c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment