.net 将光标定位在 Word 文档的开头/结尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1591682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Position cursor at start/end of Word document
提问by hawbsl
We are manipulating our Word 2007 documents from .Net using Word Interop. Mostly doing stuff with fields as in:
我们正在使用 Word Interop 从 .Net 操作我们的 Word 2007 文档。主要是用字段做的事情,如:
For Each f In d.Fields
f.Select()
//do stuff with fields here
Next
This leaves the last field in the document selected.
这将保留所选文档中的最后一个字段。
So, for the sake of neatness we would like to position the cursor at the endof the document (or even the startwould be OK).
因此,为了整洁起见,我们希望将光标定位在文档的末尾(甚至开头也可以)。
Googling for the answer doesn't throw up much ... the nearest I can get seems to be suggesting we need to involve ourselves with ranges or bookmarks. There's a GoTomethod for the Documentobject but none of the WdGoToItemoptions it offers are useful.
谷歌搜索答案并没有太多......我能得到的最接近的似乎是建议我们需要让自己参与范围或书签。GoTo该Document对象有一个方法,但WdGoToItem它提供的选项都没有用。
Isn't there a simple way to just send the cursor to the end (or start) of document?
是否有一种简单的方法可以将光标发送到文档的末尾(或开头)?
Edit
编辑
Part of my problem was I didn't like leaving the last field selected. Have now realised that I can do
我的部分问题是我不喜欢选择最后一个字段。现在意识到我可以做到
f.Unlink
to remove the mergefieldand just leave the field text there as plain text. Which is neater, whether or not we also reposition the cursor
删除mergefield并将字段文本保留为纯文本。哪个更整洁,我们是否也重新定位光标
回答by hawbsl
@Alexander Kojevnikov: Thanks for your help because you put me on the right track. However I found I had to apply the .GoTo to the Word Selection object, not the Document. As in:
@Alexander Kojevnikov:感谢您的帮助,因为您让我走上了正轨。但是我发现我必须将 .GoTo 应用于 Word Selection 对象,而不是文档。如:
Dim what As Object = Word.WdGoToItem.wdGoToLine
Dim which As Object = Word.WdGoToDirection.wdGoToLast
//below line had no effect
//d.GoTo(what, which, Nothing, Nothing)
w.Selection.GoTo(what, which, Nothing, Nothing)
回答by Alexander Kojevnikov
This is how it looks in C#:
这是它在 C# 中的样子:
object missing = Missing.Value;
object what = Word.WdGoToItem.wdGoToLine;
object which = Word.WdGoToDirection.wdGoToLast;
doc.GoTo(ref what, ref which, ref missing, ref missing);
I guess it will be even easier in VB.Net as it supports optional parameters.
我想在 VB.Net 中它会更容易,因为它支持可选参数。
回答by Fuhrmanator
I'm not sure I'm using the same environment as you, but to go to the startor endof the document here's what works for me:
我不确定我使用的是与您相同的环境,但是要转到文档的开头或结尾,这对我有用:
Private Sub moveCursorToStartOfDocument()
w.Selection.HomeKey(WdUnits.wdStory, Nothing)
End Sub
Private Sub moveCursorToEndOfDocument()
w.Selection.EndKey(WdUnits.wdStory, Nothing)
End Sub
回答by Irq Mishell
I use unit Word_TLB in Delphi with Appliction object- Word.Application
我在 Delphi 中使用单元 Word_TLB 和 Appliction 对象 - Word.Application
as following:
如下:
aWordDoc.Application.Selection.EndKey(wdStory,wdMove);
generally end of word document is:
一般word文档的结尾是:
Selection.EndKey( WdUnits.wdStory, WdMovementType.wdMove)
When I use
当我使用
Selection.GoTo(Word.WdGoToItem.wdGoToLine, Word.WdGoToDirection.wdGoToLast, Nothing, Nothing);
Selection.InsertFile('documnet.docx');
new content was insert before last line.
新内容在最后一行之前插入。
回答by Arpit Buddhiwant
The easiest way to figure out an outline for the actual code is to record a macro in Word for that specific action. Then you can modify the generated code to suit different syntax(s) of VB, VB.NET, C# etc.
找出实际代码大纲的最简单方法是在 Word 中为该特定操作录制一个宏。然后您可以修改生成的代码以适应 VB、VB.NET、C# 等的不同语法。
The code snippet below demonstrates the usage for a VB.NET application:
下面的代码片段演示了 VB.NET 应用程序的用法:
Imports wordNmSpace = Microsoft.Office.Interop.Word
' Create an object for the application instance
objWord = CreateObject("Word.Application")
' Create a reference of the selection object within Word
objSelection = objWord.Selection
' Now comes the part where you move selection position to the end of document
objSelection.endof(wordNmSpace.WdUnits.wdStory, wordNmSpace.WdMovementType.wdMove)
Hope this helps.
希望这可以帮助。
回答by Amir Keynia
Try this :
尝试这个 :
int lNumberOfPages =
_WordDoc.ComputeStatistics(Word.WdStatistic.wdStatisticPages, false);
WordApp.Selection.GoTo(Word.WdGoToItem.wdGoToPage,WordApp.WdGoToDirection.wdGoToLast, lNumberOfPages);
回答by user3333036
You can use the predefined bookmark:
您可以使用预定义的书签:
EndOfDoc oDoc.Bookmarks.Item("\endofdoc").Range
Other predefined bookmarks:
其他预定义书签:
ActiveDocument.Bookmarks("\Para").Copy "currpara"
https://msdn.microsoft.com/en-us/VBA/Word-VBA/articles/predefined-bookmarks
https://msdn.microsoft.com/en-us/VBA/Word-VBA/articles/predefined-bookmarks
回答by Ehsan.B
for net office:
网络办公:
mydoc.Range(GlobalClass.mydoc.Content.End-1 , mydoc.Content.End - 1).Select();
回答by hayj
To change cursor position at the end of the current document in a C# Word Add-In VSTO :
要在 C# Word 插件 VSTO 中更改当前文档末尾的光标位置:
this.Application.ActiveDocument.Range(
this.Application.ActiveDocument.Content.End-1,
this.Application.ActiveDocument.Content.End-1).Select();
See How to: Programmatically Define and Select Ranges in Documents

