使用Embarcadero c++ (Builder)从Word OLE中检索和循环故事范围
Retrieve and loop through Story Ranges from Word OLE using Embarcadero C++ (Builder)
我有一个代码片段,它循环遍历超链接(文件链接)对象列表(成员anchor
和destination
)并将它们插入选定的Word文档中。我可以搜索Word文档,并在找到目标(锚)字符串时插入超链接。我现在想循环通过所有的StoryRanges
在Word文档中,以确保没有节错过。我可以得到故事范围的数量,例如,使用WordApplication1->ActiveDocument->StoryRanges->Count
。但是,我不能以正确的形式传递参数来实际检索我想要搜索的故事范围,例如WordApplication1->ActiveDocument->StoryRanges->Item
。我有一个数字计数,但Item
期望作为参数的VBA常数具有正确的类型。我错过了什么?
编辑下面是代码。引起问题的行是
wdFootnotesStoryID = WordApplication1->ActiveDocument->StoryRanges->Item(??????)->get_ID();
我之前尝试将参数键入为int, OleVariant。OleVariant的错误信息是与期望的参数不匹配。我使用wdFootnotesStory试图引用枚举,但没有定义。我也尝试过整型值。
这是在Word_2k.h
中定义的olese枚举enum class WdStoryType
{
wdMainTextStory = 1,
wdFootnotesStory = 2,
wdEndnotesStory = 3,
wdCommentsStory = 4,
wdTextFrameStory = 5,
wdEvenPagesHeaderStory = 6,
wdPrimaryHeaderStory = 7,
wdEvenPagesFooterStory = 8,
wdPrimaryFooterStory = 9,
wdFirstPageHeaderStory = 10,
wdFirstPageFooterStory = 11
};
下面是主代码:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "Word_2K_SRVR"
#pragma resource "*.dfm"
struct THyperLink
{
String Anchor;
String FilePath;
};
TList* linkList = new TList;
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
THyperLink* link = new THyperLink;
link->Anchor = "ABC.0001.0002.0003";
link->FilePath = "S:\Development\WordLinking\Test\ABC.0001.0002.0003.pdf";
linkList->Add(link);
THyperLink* link2 = new THyperLink;
link2->Anchor = "ABC.0001.0002.0004";
link2->FilePath = "S:\Development\WordLinking\Test\ABC.0001.0002.0004.pdf";
linkList->Add(link2);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
OleVariant Template = EmptyParam();
OleVariant NewTemplate = False;
OleVariant ItemIndex = 1;
OleVariant strToInsert = "Insert String";
OleVariant endOfLine = "TEST";
OleVariant wdStory = 6;
//OleVariant wdMove = 0;
OleVariant rng;
OleVariant strSearch;// = "ABC.0001.0002.0003";
OleVariant strLink; // = "S:Development\WordLinking\Test\ABC.0001.0002.0003.pdf";
OleVariant lnkDoc = "S:\Development\WordLinking\Test\ZZZ.0001.0002.0003.docx";
OleVariant wdCharacter = "wdCharacter";
OleVariant cChars = 18;
OleVariant wdMove = "wdMove";
OleVariant wdTrue = true;
OleVariant wrdStoryRangeCount;
OleVariant wdFootnotesStoryID;
OleVariant rangeStory;
OleVariant wrdDoc;
WdStoryType wdFootnotesStory = 2;
try
{
WordApplication1->Connect();
}
catch (...)
{
ShowMessage("Microsoft word is not installed");
}
//Make application visible
WordApplication1->GetDefaultInterface()->Visible = True;
//Open document to be linked
WordApplication1->Documents->Open(lnkDoc);
//Open new document - add to document collection in application
//WordApplication1->Documents->Add(Template, NewTemplate);
//go to top of the document
WordApplication1->Selection->HomeKey(wdStory);
for (int index = 0; index <= linkList->Count-1; index++)
{
//Retrieve hyperlink object
THyperLink* link = reinterpret_cast<THyperLink*>(linkList->Items[index]);
strSearch = (OleVariant)link->Anchor;
strLink = (OleVariant)link->FilePath;
//
wrdStoryRangeCount = WordApplication1->ActiveDocument->StoryRanges->Count;
//WordApplication1->ActiveDocument->StoryRanges->Item(1);
wdFootnotesStoryID = WordApplication1->ActiveDocument->StoryRanges->Item(??????)->get_ID();
ShowMessage ("wdFootnotesStory ID: " + wdFootnotesStoryID);
//WordApplication1->ActiveDocument->get_
while (WordApplication1->Selection->Find->Execute(strSearch,Template,Template,Template,Template,Template,wdTrue))
//while (WordApplication1->ActiveDocument->StoryRanges->Item(wdFootnotesStory)->Find->Execute(strSearch,Template,Template,Template,Template,Template,wdTrue))
{
WordApplication1->Selection->Hyperlinks->Add(WordApplication1->Selection->Range,strLink,Template,Template,strSearch,Template);
}
//go back to top of Word document
WordApplication1->Selection->HomeKey(wdStory);
}
//Disconnect from word
WordApplication1->Disconnect();
}
//---------------------------------------------------------------------------
解决方案:在添加代码的过程中,我想我偶然发现了解决方案- WdStoryType::wdFootnotesStory
。看来我所缺少的只是类限定符引用。
解决方案:在添加代码的过程中,我想我偶然发现了解决方案- WdStoryType::wdFootnotesStory。看来我所缺少的只是类限定符引用。
- 如何循环打印顶点结构
- 如何在C++中从两个单独的for循环中添加两个数组
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 正在尝试了解输入验证循环
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 循环后如何继续阅读
- Ardunio UNO解决了多个重叠的定时器循环
- Eigen如何在容器循环中干净地附加矩阵
- 在某些循环内使用vector.push_back时出现分段错误
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么我的for循环不能正确获取argv
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- while循环中while循环的时间复杂度是多少
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 为什么在这个代码结束循环中没有得到结束
- 在基于范围的for循环中使用结构化绑定声明
- 用于C++中带有数组和指针的循环
- 循环中的随机函数
- 我如何从循环内部使用迭代器的函数内部的getter中检索特定的成员
- 使用Embarcadero c++ (Builder)从Word OLE中检索和循环故事范围