gpt4 book ai didi

vba - VBA : Run-Time error '91' (Code working in template but not “new from template” )

转载 作者:行者123 更新时间:2023-12-03 09:05:24 35 4
gpt4 key购买 nike

从“打开”模板(例如Template.dotm)运行时,我的代码有效,但当我使用“从模板中新建”(例如Document1.docx)时,我的代码不起作用。

从“Document1”运行时,错误代码提示:

“运行时错误'91'对象变量或带块变量未设置”

调试器亮点:

Selection.MoveDown单位:= wdLine,计数:= 129

我在Microsoft Word对象中的代码/本文档:

Opens Userform1 when making new document from template:


Private Sub Document_New()
Userform1.Show
End Sub

USER1中的 代码

EDITED Now "run-time error '438' Object does not supp. property or method."

If CheckBox1 = True it Opens text.docx and pastes its contents in line 129 of this document


    Private Sub CommandButton1_Click()
Dim Newdocument As Document
Set NewDocument = ThisDocument
Dim myDoc As Document
Set myDoc = Documents.Open(FileName:="C:\Users\Stack\Documents\Text.docx", ReadOnly:=False)

' do some stuff
'Opens text document and pastes it in line 129 of this document
If CheckBox1 = True Then



myDoc.WholeStory
myDoc.Copy

Newdocument.Activate

Selection.MoveDown Unit:=wdLine, Count:=129
Selection.PasteAndFormat (wdFormatOriginalFormatting)

Application.DisplayAlerts = False
myDoc.Close

End If

Unload Me
Exit Sub

End Sub



I probably seem clueless in your eyes, but i am new to VBA and still learning. Debugger now highlights the "mydoc.wholestory" with error '438'

最佳答案

这工作:

Selection.InsertFile FileName:=“文件路径”

关于vba - VBA : Run-Time error '91' (Code working in template but not “new from template” ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45487268/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com