不一致通过Excel VBA设置Word边距/列间距

时间:2017-03-15 12:19:46

标签: excel-vba ms-word multiple-columns margins vba

早上好!

我正在创建一个工具,使用相同的设置格式化许多word文档,并且正在设置所有文档正文数据到指定的列数,以及指定的边距。运行时,此代码有效,但不会正确设置左/右边距。代码应将每个代码设置为相同的值。

运行时似乎是可变的。例如,如果我选择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 个答案:

答案 0 :(得分:1)

这是设置边距的代码部分。

  

使用wrdDocument.PageSetup     .LeftMargin = wrdApplication.InchesToPoints(frmWordEdit.txtMLeftRight)     .RightMargin = wrdApplication.InchesToPoints(frmWordEdit.txtMLeftRight)     .TopMargin = wrdApplication.InchesToPoints(frmWordEdit.txtMTop)     .BottomMargin = wrdApplication.InchesToPoints(frmWordEdit.txtMBottom)

     

结束

我发现语法没有错误(除非您从另一个应用程序(可能是Excel)运行代码,否则不需要指定wrdApplication)。由于代码出现没有错误,因此错误必须在frmWordEdit的引用中出现。我建议您使用普通数字运行此代码,以点表示,并查看是否仍然得到相同的结果。 frmWordEdit.txtMLeftRight似乎是一个文本框。由于您未指定要引用的属性,因此它必须是默认值,即Value属性。 Textbox的Value属性包含一个字符串,您可以将其输入InchesToPoints函数。如果我没有弄错的话,那个函数只需要一个数字值。因此我怀疑该字符串未正确翻译。尝试类似InchesToPoints(Val(frmWordEdit.txtMLeftRight))。