C#操作Word相关
//C#获取Word文档页数,并跳转到指定的页面获取页面信息
using MSWord = Microsoft.Office.Interop.Word;
private MSWord.Application wordApp;
//Word应用程序变量
private MSWord.Document wordDoc;//Word文档变量
private Object Nothing = Missing.Value;
//初始化
wordApp = new MSWord.ApplicationClass();
wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
// 打开Word object FileName = strPath;
object readOnly = false;
object isVisible = true;
wordDoc = wordApp.Documents.Open(ref FileName, ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
// 计算Word文档页数 MSWord.WdStatistic stat = MSWord.WdStatistic.wdStatisticPages;
int num = wordDoc.ComputeStatistics(stat, ref Nothing);
// 跳转到指定书签 object BookMarkName = "BookMark1";
object what = MSWord.WdGoToItem.wdGoToBookmark;
wordDoc.ActiveWindow.Selection.GoTo(ref what, ref Nothing, ref Nothing, ref BookMarkName);
MessageBox.Show( wordDoc.ActiveWindow.Selection.Paragraphs[1].Range.Text.ToString());// 跳转到指定页
object What = MSWord.WdGoToItem.wdGoToPage;
object Which = MSWord.WdGoToDirection.wdGoToNext;
object Name = "1";
// 页数
wordDoc.ActiveWindow.Selection.GoTo(ref What, ref Which, ref Nothing, ref Name);
// 第二个参数可以用Nothing
wordDoc.ActiveWindow.Selection.Paragraphs[1].Range.Text.ToString();
// 以下是跳转到某个页面之后的操作
// 通过PageSetup可以获取某个页面的信息
// 获取页面的分栏
wordDoc.ActiveWindow.Selection.PageSetup.TextColumn;
'Sub DelWdTextFrameStory(ByRef WordDoc As Microsoft.Office.Interop.Word.Document)
' Dim sr As Microsoft.Office.Interop.Word.StoryRanges = WordDoc.StoryRanges
' Dim r1 As Microsoft.Office.Interop.Word.Range = Nothing
' Do
' r1 = Nothing
' For Each r As Microsoft.Office.Interop.Word.Range In sr
' If RunFlag = False Then
' Me.SafeInvoke(Sub()
' LB_info.Text = "您终止了操作"
' End Sub)
' Exit For
' End If
' 'Console.WriteLine(r.StoryType & vbTab & r.Text)
' 'If Microsoft.Office.Interop.Word.WdStoryType.wdTextFrameStory = r.StoryType Then
' ' Console.WriteLine("InlineShapes:" & r.InlineShapes.Count)
' 'ElseIf r.StoryType = Microsoft.Office.Interop.Word.WdStoryType.wdPrimaryHeaderStory Then
' ' Console.WriteLine("StoryLength:" & r.StoryLength)
' 'End If
' If (Microsoft.Office.Interop.Word.WdStoryType.wdTextFrameStory = r.StoryType AndAlso r.InlineShapes.Count > 0) OrElse (r.StoryType = Microsoft.Office.Interop.Word.WdStoryType.wdPrimaryHeaderStory AndAlso r.StoryLength > 1) Then
' 'Console.WriteLine(r.InlineShapes.Count)
' 'Console.WriteLine(r.Sections.Count)
' 'Console.WriteLine(r.Sections)
' 'Console.WriteLine(r.Text & r.ShapeRange.Type)
' 'If r.Subdocuments IsNot Nothing Then
' ' r.Subdocuments.Delete()
' 'End If
' r.Delete()
' r1 = r
' 'Dim d As String = r.XML
' Console.WriteLine("文本框")
' End If
' 'If r.StoryType = 1 Then
' ' 'r.Delete()
' ' 'r1 = r
' ' Console.WriteLine(r.ShapeRange.Count)
' ' 'r1 = r.NextStoryRange
' ' 'With r1 IsNot Nothing
' ' ' Console.WriteLine(r.StoryType)
' ' 'End With
' ' 'Console.WriteLine(r.StoryLength)
' 'End If
' 'If r.XML.Contains("03000001.png") Then
' ' r.Delete()
' ' r1 = r
' 'End If
' Next r
' 'RunFlag = False
' If RunFlag = False Then
' Me.SafeInvoke(Sub()
' LB_info.Text = "您终止了操作"
' End Sub)
' Exit Do
' End If
' Loop While r1 IsNot Nothing
'End Sub
'Sub DelShape(ByRef WordDoc As Microsoft.Office.Interop.Word.Document)
' Dim ish As Microsoft.Office.Interop.Word.Shape = Nothing, ish_tem As Microsoft.Office.Interop.Word.Shape = Nothing
' Do
' ish_tem = Nothing
' For Each ish In WordDoc.Shapes
' If RunFlag = False Then
' Me.SafeInvoke(Sub()
' LB_info.Text = "您终止了操作"
' End Sub)
' Exit For
' End If
' Console.WriteLine(ish.Type)
' If ish.Type = Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapeSmartArt OrElse ish.Type = Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapeDiagram OrElse ish.Type = 17 Then 'OrElse ish.Type = Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapeHorizontalLine
' 'Console.WriteLine("删除艺术字和" & ish.AlternativeText)
' ish.Delete()
' ish_tem = ish
' End If
' Next ish
' 'RunFlag = False
' Loop While ish_tem IsNot Nothing
'End Sub
// 计算Word文档页数
MSWord.WdStatistic stat = MSWord.WdStatistic.wdStatisticPages;
int num = wordDoc.ComputeStatistics(stat, ref Nothing);
int wordNum=wordDoc.Characters.Count;//文档字数
浙公网安备 33010602011771号