2017-03-15 6 views
1

おはようございます!Excel VBAを使用したワードマージン/列間隔の設定の不一致

私は同じ設定で多くのワード文書をフォーマットするためのツールを作成しており、指定された列数と指定された余白にすべてのドキュメント本文データを設定しています。実行すると、このコードは機能しますが、左右余白は適切に設定されません。コードはそれぞれの値を同じ値に設定する必要があります。

実行時は可変であるようです。たとえば、私が0.3を選択した場合、左余白は0.2で終わり、右は0.4になります。少し奇妙にするために、MS Wordのカスタムマージン設定に手動で入ると、ページのマージンバーが設定されていなくても、適切な数値(0.3)が表示されます。

これは設定上の問題であるかどうか、またはより正確な方法があるかどうかを知るために誰かがvb経由で作業していますか?私はそれが列の間隔と関係があるかもしれないと思う.SpaceAfter = InchesToPoints(frmWordEdit.txtColumnSpacing)、しかし私は確信しています。

ご協力いただきましてありがとうございます。

Sub AddRemoveWatermark(strReplaceText As String) 
    'Word Variables 
    Dim wrdApplication As Word.Application 
    Dim wrdDocument As Word.Document 
    Dim wrdSection As Word.section 
    Dim wrdSelection As Word.Selection 
    Dim wrdHeader As Word.HeaderFooter 
    Dim rngHeader As Word.Range 
    Dim rngFooter As Word.HeaderFooter 
    Dim spShape As Word.Shape 

    Dim strDocumentName As String 
    Dim strPath As String 
    Dim strBBPath As String 
    Dim lngCount As Long 

    ' Open the file dialog 
    With Application.FileDialog(msoFileDialogOpen) 
     .AllowMultiSelect = True 
     .Show 

     Set wrdApplication = New Word.Application 

     ' Display paths of each file selected 
     For lngCount = 1 To .SelectedItems.Count 
      strPath = .SelectedItems(lngCount) 
      Set wrdDocument = wrdApplication.Documents.Open(strPath) 

      strDocumentName = wrdDocument.FullName 'Record the document name 
      wrdApplication.Templates.LoadBuildingBlocks 

      wrdApplication.Visible = True 

      'Document Layout 
      If frmWordEdit.chkDocumentLayout.Value = True Then 

       'Change Columns 
       If frmWordEdit.chkColumns = True Then 
        With wrdDocument.PageSetup.TextColumns 
         .SetCount NumColumns:=frmWordEdit.txtColumns 
         '.Add EvenlySpaced:=True 
         '.Width = InchesToPoints(3) 
         '.SpaceAfter = InchesToPoints(0.3) 
        End With 

        Dim i As Integer 

        If frmWordEdit.txtColumns > 1 Then 
         For i = 1 To frmWordEdit.txtColumns - 1 
          With wrdDocument.PageSetup.TextColumns(i) 
           '.Width = InchesToPoints(4) 
           .SpaceAfter = InchesToPoints(frmWordEdit.txtColumnSpacing) 
          End With 

         Next 
        End If 
       End If 

       'Change Margins 
       If frmWordEdit.chkMargins = True Then 
        With wrdDocument.PageSetup 
         .LeftMargin = wrdApplication.InchesToPoints(frmWordEdit.txtMLeftRight) 
         .RightMargin = wrdApplication.InchesToPoints(frmWordEdit.txtMLeftRight) 
         .TopMargin = wrdApplication.InchesToPoints(frmWordEdit.txtMTop) 
         .BottomMargin = wrdApplication.InchesToPoints(frmWordEdit.txtMBottom) 
        End With 
       End If 

      End If 

      'Document Design 
      If frmWordEdit.chkDocumentDesign.Value = True Then 
       If frmWordEdit.chkMHeader = True Then 
        With wrdDocument.PageSetup 
         .HeaderDistance = wrdApplication.InchesToPoints(frmWordEdit.txtMHeader) 
        End With 
       End If 

       If frmWordEdit.chkMFooter = True Then 
        With wrdDocument.PageSetup 
         .FooterDistance = wrdApplication.InchesToPoints(frmWordEdit.txtMFooter) 
        End With 
       End If 
      End If 
End Sub 

答えて

1

これは、余白を設定するコードの一部です。 wrdDocument.PageSetupで

.LeftMargin = wrdApplication.InchesToPoints(frmWordEdit.txtMLeftRight) .RightMargin = wrdApplication.InchesToPoints(frmWordEdit.txtMLeftRight) .TopMargin = wrdApplication.InchesToPoints(frmWordEdit.txtMTop) .BottomMargin =

付(frmWordEdit.txtMBottom)

エンドwrdApplication.InchesToPointsは、私はあなたがwを指定する必要はありませんことを除いて(構文とは、障害を見つけていません他のアプリケーション(おそらくExcel)からコードを実行しない限り、rdApplicationです。コードはフォールトエラーなしで表示されるので、frmWordEditへの参照に由来する必要があります。私はポイントで表現された平易な数字でこのコードを実行し、同じ結果が得られるかどうかを確認することをお勧めします。 frmWordEdit.txtMLeftRightはテキストボックスのように見えます。あなたが参照しているプロパティを指定していないので、それはValueプロパティであるデフォルトでなければなりません。テキストボックスのValueプロパティは、InchesToPoints関数に入力する文字列を保持します。その関数は、私が間違っていなければ、単一の値を取ります。とにかく、数値です。したがって、私は文字列が正しく翻訳されていないと思われます。 InchesToPoints(Val(frmWordEdit.txtMLeftRight))のようなものを試してみてください。

+0

Valを使用するようにコードを更新しました。これは良い方法のようですが、ありがとうございます。私はそれだけでなく、直接ポイント値を行い、同じ問題を経験しました。私は起こっているかもしれないと思うのは、.space以降を.3に設定すると、2番目の列が左に引き寄せられ、右の余白が増えます。マージンは実際には0.3に設定されるかもしれませんが、列の幅はそれを満たすのに十分な幅ではないと私は信じています。つまり、私は可変入力を持つことができるツールを作成しようとしていますが、コードをさまざまな状況で最後の列幅に合わせる方法がわかりません。 – Allen

+0

私は列を設定するコードを見て、そこに明白な不具合も見つからなかった。 SpaceAfterを含む列の合計幅が、余白で定義されたページ領域の合計幅を超えている可能性はありますか? – Variatus