如何在vb.net中指定word文档中的插入点?
我正在构建一个应用程序来编辑现有的word文档 代码应删除第二页上的一段,并将其替换为另一段,该段由字符串变量“Direct_introduction”标识 问题是,文本被插入到文档的末尾,即使在指定了段落数之后,我也无法将其插入到其他任何地方如何在vb.net中指定word文档中的插入点?,vb.net,winforms,ms-word,Vb.net,Winforms,Ms Word,我正在构建一个应用程序来编辑现有的word文档 代码应删除第二页上的一段,并将其替换为另一段,该段由字符串变量“Direct_introduction”标识 问题是,文本被插入到文档的末尾,即使在指定了段落数之后,我也无法将其插入到其他任何地方 If DirectRB.Checked Then 'introRange.Delete() 'Dim ODirect_intropara As Word.Paragraph = oDoc.Paragraphs.Add()
If DirectRB.Checked Then
'introRange.Delete()
'Dim ODirect_intropara As Word.Paragraph = oDoc.Paragraphs.Add()
'ODirect_intropara.Range.Font.Name = "Arial Narrow"
'ODirect_intropara.Range.Font.Bold = CInt(False)
'ODirect_intropara.Range.Font.Size = 11
'ODirect_intropara.Format.SpaceAfter = 0
'ODirect_intropara.Range.Text = Direct_introduction
'ODirect_intropara.Range.ParagraphFormat.Alignment =
Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft
'ODirect_intropara.Range.InsertParagraphAfter()
introPara = oDoc.Content.Paragraphs.Add
introPara.Range.Text = Direct_introduction
introPara.Range.Font.Bold = True
introPara.Format.SpaceAfter = 24 '24 pt spacing after
paragraph.
introPara.Range.InsertParagraphAfter()
Rng = oDoc.Range(oDoc.Range.Characters.Count - 1,
oDoc.Range.Characters.Count)
Rng.InsertAfter(vbCrLf & "More text inserted using the range
object")
ElseIf PartnerRB.Checked Then
MessageBox.Show("you checked Partner")
End If
执行“直接介绍”的查找,如果找到,则在当前位置插入。例如(在VBA中):
在提供的代码中,除了在文档末尾插入之外,我看不到任何其他功能。你也没有提到“段落的编号”,这使得你很难知道你想要什么。您是否可以控制正在编辑的文档的创建?有模板吗?你能修改模板吗?没有模板。我不知道如何插入工作。这应该是第一段。即使我指定了第(1)段,它也不起作用。你知道如何使用F1吗?我一直在想,有多少人在网上发帖,却没有在发帖前阅读文档。读这篇文章是的,这是解决问题的好方法。但是,查找和替换的字符数限制为255个。因此,我创建了6个字符串来覆盖该段落。但是我需要插入9个字符串变量,当我放入InsertParagraphAfter()函数时,它总是在find函数之后添加文档的结尾。显然,您的方法是错误的。我没说要用替换来插入。此外,我说要在当前位置(即查找返回的位置)插入,而不是在文档末尾。非常感谢,它成功了。我还找到了另一种方法:使用oDoc.Bookmarks.Add(“BMname_Introduction”).Range.Text=“Introduction_Text”以oPara1=oDoc.Content.parations.Add(oDoc.Bookmarks(“Introduction”).Range)oPara1.Range.Text=“在书签位置添加新文本”添加书签完全没有必要;实现的只是臃肿的代码。
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Direct_introduction"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWildcards = False
.Execute
End With
If .Find.Found = True Then
.Text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa." & vbCr & _
"Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna." & vbCr & _
" Nunc viverra imperdiet enim." & vbCr & _
"Fusce est. Vivamus a tellus."
End If
End With
Application.ScreenUpdating = True
End Sub