在vc中怎样读写word文件中的数据并可控制word的打开和关闭等?

ltz 2003-06-23 09:56:15
...全文
146 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
epico 2003-06-23
  • 打赏
  • 举报
回复
在dotnetFramework 1.0 得Samples中Tech???/Interop/office/word
epico 2003-06-23
  • 打赏
  • 举报
回复
vc.net中很好做,VC中有点繁(我不会,你可以查一下#import “Excel.exe”的帮助)
给你个C#的例子,可以改成vc.net

// ==============================================================================
// Filename: wordapp.cs
//
// Summary: C# application demonstrating the use of the Microsoft Word object model
//
// This file is part of the Microsoft CLR Samples
//
// Copyright (C) 2000 - 2001 Microsoft Corporation. All rights reserved
//
// This source code is intended only as a supplement to Microsoft
// Development Tools and/or on-line documentation. See these other
// materials for detailed information reagrding Microsoft code samples.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Collections;
using System.Threading;
using Word;

namespace WordApp
{
class WordAppMain
{
static object missing = Missing.Value;

static int Main()
{

WordAppMain myWord = new WordAppMain();
int return_Result = 0;

// Create a word object that we can manipulate
Word.Application Word_App = null;
Word.Document Word_doc=null;
try
{
Word_App = new Word.Application();
Word_doc=new Word.Document();
}
catch(Exception e)
{
Console.WriteLine("Can't create a word document " + e.ToString());
return_Result = 1;
goto Exit;
}

AutoCorrect autocorrect = Word_App.AutoCorrect;
Word.AutoCorrectEntries autoEntries = autocorrect.Entries;

string theEnd= "\nThe End";
autoEntries.Add("Inntroduction", "Introduction");

Word.Documents Docs = Word_App.Documents;
if (Docs == null)
{
Console.WriteLine("Docs is null");
}
else
{
Console.WriteLine("Docs exists:" + Docs.Count);
}

Word_App.Visible=true;
Word._Document my_Doc= (Word._Document) Word_doc;
Word_doc=Docs.Add(ref missing, ref missing, ref missing, ref missing);

object start = 0;
object end = 0;
Word.Range range = Word_doc.Range(ref missing,ref missing);

// add text to the doc -- this contains some deliberate misspellings so that we can correct them in a short while
range.Text="Microsoft Word Interoperability Sample\n\nInntroduction:\n\nMicrosoft .NET will alow the creation of truly distributed XML Web services. These services will integrate and collaborate with a range of complementary services to work for customers in ways that today's internet companies can only dream of. Microsoft .NET will drive the Next Generation Internet and will shift the focus from individual Web sites or devices connected to the Internet, to constellations of computers, devices, and services that work together to deliver broader, richer solutions.\nFor more info go to:\n ";

// Wait so the starting state can be admired
Thread.Sleep(2000);

// Format the title
Word.FontClass fc= new Word.FontClass();
try
{
Console.WriteLine("Formatting the title");
start = 0; end = 40;
range=Word_doc.Range(ref start, ref end);
fc.Size=24;
fc.Bold=1;
fc.Color=Word.WdColor.wdColorGray30;
range.Font=fc;
start = 40; end = 54;
range=Word_doc.Range(ref start, ref end);
fc.Size=14;
range.Font=fc;

}
catch(Exception e)
{
Console.WriteLine(" Font exception:{0}", e.ToString());
}


// Wait so the new formatting can be appreciated
Thread.Sleep(3000);

autocorrect.ReplaceTextFromSpellingChecker=true;
// Fix inntroduction
object obj = "Inntroduction";
Word.AutoCorrectEntry errEntry= autoEntries.Item(ref obj);

Word.Words myWords=Word_doc.Words;
Word.Range errRange= myWords.Item(7);
errEntry.Apply(errRange);

// Add a caption to the window and get it back
Word.Window myWindow = Word_App.ActiveWindow;
myWindow.Caption = "Managed Word execution from C# ";
string gotCaption = myWindow.Caption;
if (gotCaption.Equals("Managed Word execution from C# "))
{
Console.WriteLine("Caption assigned and got back");
return_Result = 1;
}
Thread.Sleep(2000);

// define the selection object, find and replace text
Word.Selection mySelection = myWindow.Selection;
Word.Find myFind = mySelection.Find;
object findText = "alow";
object replaceText ="allow";

// Find "alow" and replace with "allow"
try
{
myFind.Execute(ref findText,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref replaceText,ref missing,ref missing,ref missing,ref missing,ref missing);
}
catch(Exception e)
{
Console.WriteLine(e);
}
Thread.Sleep(2000);
Console.WriteLine(myFind.Text + " has been corrected");

try
{
start = 65; end = 69;
range=Word_doc.Range(ref start, ref end);
Console.WriteLine("The color of .NET is being changed");
fc.Bold=16;
fc.Color= Word.WdColor.wdColorLavender;
range.Font=fc;

}
catch(Exception e)
{
Console.WriteLine(" Font exception:{0}", e.ToString());
}
Thread.Sleep(2000);

// underline the selected text
range=Word_doc.Range(ref start,ref end);
range.Underline=(Word.WdUnderline.wdUnderlineDouble);

// add hyperlink and follow the hyperlink
Word.Hyperlinks my_Hyperlinks = Word_doc.Hyperlinks;

// Make the range past the end of all document text
mySelection.Start = 9999;
mySelection.End = 9999;
range = mySelection.Range;

// Add a hyperlink
string myAddress = "http://go.microsoft.com/fwlink/?linkid=3269&clcid=0x409";
object obj_Address = myAddress;
Console.WriteLine("Adding hyperlink to the document");
Word.Hyperlink my_Hyperlink1= my_Hyperlinks._Add(range, ref obj_Address, ref missing);
Word_App.ActiveWindow.Selection.InsertAfter("\n");

Thread.Sleep(5000);

// Open a window to Hyperlink
Process ie = Process.Start("iexplore.exe", my_Hyperlink1.Address);

// Wait for a short spell to allow the page to be examined
Thread.Sleep(10000);

// close the browser first
Console.WriteLine("Removing browser window");
ie.Kill();

// Display "The End"
Word_App.ActiveWindow.Selection.InsertAfter(theEnd);
Word_App.ActiveWindow.Selection.Start = 0;
Word_App.ActiveWindow.Selection.End = 0;
Word_App.Activate();
Thread.Sleep(5000);

// Close Microsoft Word
object myBool = Word.WdSaveOptions.wdDoNotSaveChanges;
Word_App.ActiveWindow.Close(ref myBool,ref missing);

Word_App.Quit(ref missing, ref missing, ref missing);

Exit:
return return_Result;
}
}
}
ltz 2003-06-23
  • 打赏
  • 举报
回复
我想用vba可以实现,但我没有这方面的经验,有谁能提供源代码吗?
shifengy 2003-06-23
  • 打赏
  • 举报
回复
我没有使用过,但是我想应该是使用自动化服务器技术
通过office中的word 的接口操作,里面应该有open
close等方法。

5,139

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 Office开发/ VBA
社区管理员
  • Office开发/ VBA社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧