0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Word VBA 文字がずれない標準スタイルMSGothic12ポイント版

Posted at

前回と同じ理論 ただし余白が非対称

https://qiita.com/Q11Q/items/bf057563de547ccba70a
今回はMSゴシックで40行40字にしたかったので、右余白が25㎜ではなく15mmしかありません。
これだと両面印刷の時、左右をパンチで穴をあけて閉じると、文字にかかります。
ワープロの時代は文字間隔を詰めるのですが、文字間隔を詰めるとおかしくなります。
一応左右25mmの場合、文字間隔を11.35ptにすると、40字になります。
文字間隔はいじらず、行間隔は行単位でワードにやらせておいて、そのうえで左詰めにして標準スタイルを決めるとよいようです。
またこのバージョンはグリッドが合います。しかし文字単位、行単位では設定できる命令がみつかっていないため、グリッドを合わせて値をとってから、その値をVBAに書き込まないといけません。
グリッドの間隔の値は、グリッドを文字グリッド1本、行グリッド2本、行間隔0.5行、文字間隔1文字にあわせてからイミディエイトで、次の2つの命令で取得できます。
直接文字単位、行単位ではやはりできないのかもしれません。
また、この時1行ではなく0.5行の値を取得します。文字間隔も0.5字で取得してください。
行間隔?Application.PointsToMillimeters(ThisDocument.GridDistanceVertical)
文字間隔?Application.PointsToMillimeters(ThisDocument.GridDistanceHorizontal)
image.png

左右25㎜にする場合には全角で37字になります。

Sub SetJapaneseMsGothic12PtSytle()
Const FontNameMSGothic = "MS ゴシック"
Const wdSytleStandardJpn = "標準"
Const DefaultFontSize = 12
Const lngTopMargine = 30 'mm
Const lnglftMargine = 25 'mm
Const lngRgtMargine = 15.7 'mm
Const lngBtmMargine = 20 'mm
Const A4PortraitWidhth = 210 'mm
Const A4PortraitHeight = 297 'mm
Dim wdOMathAutoCorrect As OMathAutoCorrect
Dim wDoc As Word.Document: Set wDoc = ThisDocument
'Name Ascii PageSetup
Selection.WholeStory
With wDoc.Styles(wdStyleNormal).Font
If .NameFarEast = .NameAscii Then
.NameAscii = ""
End If
.NameFarEast = ""
End With
With wDoc.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = MillimetersToPoints(30)
.BottomMargin = MillimetersToPoints(20)
.LeftMargin = MillimetersToPoints(25)
.RightMargin = MillimetersToPoints(15.7)
.Gutter = MillimetersToPoints(0)
.HeaderDistance = MillimetersToPoints(15)
.FooterDistance = MillimetersToPoints(17.5)
.PageWidth = MillimetersToPoints(A4PortraitWidhth)
.PageHeight = MillimetersToPoints(A4PortraitHeight)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
.CharsLine = 40
.LinesPage = 40
.LayoutMode = wdLayoutModeGrid
End With
wDoc.KerningByAlgorithm = False
wDoc.JustificationMode = wdJustificationModeExpand
With wDoc.Styles(wdSytleStandardJpn).Font
.NameFarEast = FontNameMSGothic
.NameAscii = FontNameMSGothic
.Name = FontNameMSGothic
.Size = 12
.Bold = False
.Italic = False
.Underline = wdUnderlineNone
.UnderlineColor = wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.Emboss = False
.Shadow = False
.Hidden = False
.SmallCaps = False
.AllCaps = False
.Color = wdColorAutomatic
.Engrave = False
.Superscript = False
        .Spacing = 0
        .Scaling = 100
        .Position = 0
        .Kerning = 0
.Animation = wdAnimationNone
.DisableCharacterSpaceGrid = False
.EmphasisMark = wdEmphasisMarkNone
.Ligatures = wdLigaturesNone
.NumberSpacing = wdNumberSpacingTabular
.NumberForm = wdNumberFormOldStyle
.StylisticSet = wdStylisticSetDefault
.ContextualAlternates = 0
End With

Selection.WholeStory
With wDoc.Styles(wdSytleStandardJpn).ParagraphFormat
.LeftIndent = MillimetersToPoints(0)
.RightIndent = MillimetersToPoints(0)
.SpaceBefore = 0
.SpaceBeforeAuto = False
.SpaceAfter = 0
.SpaceAfterAuto = False
.LineSpacingRule = wdLineSpaceSingle
.Alignment = wdAlignParagraphLeft
.WidowControl = False
.KeepWithNext = False
.KeepTogether = False
.PageBreakBefore = False
.NoLineNumber = False
.Hyphenation = True
.FirstLineIndent = MillimetersToPoints(0)
.OutlineLevel = wdOutlineLevelBodyText
.CharacterUnitLeftIndent = 0
.CharacterUnitRightIndent = 0
.CharacterUnitFirstLineIndent = 0
.LineUnitBefore = 0
.LineUnitAfter = 0
.MirrorIndents = False
.TextboxTightWrap = wdTightNone
.CollapsedByDefault = False
.AutoAdjustRightIndent = False
.DisableLineHeightGrid = False
.FarEastLineBreakControl = True
.WordWrap = True
.HangingPunctuation = False
.HalfWidthPunctuationOnTopOfLine = True
.AddSpaceBetweenFarEastAndAlpha = False
.AddSpaceBetweenFarEastAndDigit = False
.BaseLineAlignment = wdBaselineAlignAuto
End With

Selection.WholeStory
With Options
.ApplyFarEastFontsToAscii = True
.IgnoreInternetAndFileAddresses = True 'Trueの場合は、ファイル名拡張子、MS-DOS パス、電子メールアドレス、サーバー名と共有名 (UNC パス)、およびインターネットアドレス (url とも呼ばれます) は、スペルチェック中は無視されます
.AutoFormatAsYouTypeApplyHeadings = False
.AutoFormatAsYouTypeApplyBorders = False
.AutoFormatAsYouTypeApplyBulletedLists = False
.AutoFormatAsYouTypeApplyNumberedLists = False
.AutoFormatAsYouTypeApplyTables = False
.AutoFormatAsYouTypeReplaceQuotes = True
.AutoFormatAsYouTypeReplaceSymbols = True
.AutoFormatAsYouTypeReplaceOrdinals = True
.AutoFormatAsYouTypeReplaceFractions = False
.AutoFormatAsYouTypeReplacePlainTextEmphasis = False
.AutoFormatAsYouTypeReplaceHyperlinks = False
.AutoFormatAsYouTypeFormatListItemBeginning = True
.AutoFormatAsYouTypeDefineStyles = False
.TabIndentKey = False
.AutoFormatAsYouTypeApplyFirstIndents = False
.AutoFormatAsYouTypeApplyDates = False
.AutoFormatAsYouTypeMatchParentheses = True
.AutoFormatAsYouTypeInsertOvers = False
.AutoFormatAsYouTypeDeleteAutoSpaces = False
End With

On Error Resume Next
Set wdOMathAutoCorrect = Application.OMathAutoCorrect
If Not wdOMathAutoCorrect Is Nothing Then
With wdOMathAutoCorrect
.UseOutsideOMath = False
.ReplaceText = True
End With
End If
On Error GoTo 0

Options.LabelSmartTags = False
ActiveWindow.View.DisplayBackgrounds = True

wDoc.Styles(wdSytleStandardJpn).NoSpaceBetweenParagraphsOfSameStyle = False
With wDoc.Styles(wdSytleStandardJpn)
.AutomaticallyUpdate = False
.BaseStyle = ""
.NextParagraphStyle = wdSytleStandardJpn
End With
With wDoc
.SnapToGrid = False
.SnapToShapes = False
.GridDistanceHorizontal = MillimetersToPoints(4.233333) '2本、0.5字単位の文字グリッド
.GridDistanceVertical = MillimetersToPoints(3.086806) '2本、0.5行単位のグリッド
.GridOriginHorizontal = MillimetersToPoints(25)
.GridOriginVertical = MillimetersToPoints(30)
.GridSpaceBetweenHorizontalLines = 1
.GridSpaceBetweenVerticalLines = 2
.GridOriginFromMargin = True
End With
Options.DisplayGridLines = True
Options.DisplayAlignmentGuides = False
Options.PageAlignmentGuides = True
Options.MarginAlignmentGuides = True
Options.ParagraphAlignmentGuides = True
End Sub
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?