我想创建一个新的Word文档并设置自定义页边距。我已经编写了这段代码,但它似乎在WITH语句处崩溃了。但不太确定为什么会发生这种情况。
Sub test()
Dim docCreate As Document
Set docCreate = Documents.Add
With docCreate.PageSetup
.TopMargin = WordApp.InchesToPoints(0.6)
.BottomMargin = WordApp.InchesToPoints(0.6)
.LeftMargin = WordApp.InchesToPoints(0.6)
.RightMargin = WordApp.InchesToPoints(0.6)
End With
docCreate.Range.Paste
End Sub
Run Code Online (Sandbox Code Playgroud)
尝试这个:
Sub test2()
Dim docCreate As Document
Set docCreate = Documents.Add
With docCreate.PageSetup
.TopMargin = Application.InchesToPoints(0.6)
.BottomMargin = Application.InchesToPoints(0.6)
.LeftMargin = Application.InchesToPoints(0.6)
.RightMargin = Application.InchesToPoints(0.6)
End With
docCreate.Range.Paste
End Sub
Run Code Online (Sandbox Code Playgroud)
问题是WordApp未定义,它可能来自您复制粘贴此片段的位置。要么定义它(Set wordApp = Application如果您想要当前的对象,或者Set wordApp = CreateObject("Word.Application")想要一个新的对象),或者Application像我上面那样简单地使用该对象(但您假设,像这样,您正在当前正在运行的 Word 应用程序上工作) 。