How can I use the GoTo-command to go to a Bookmark in a word document using actxserver

조회 수: 10 (최근 30일)
Hi,
we create an extensive word document as a result of our data analysis which can be up to 100 pages long by using the WriteToWordFromMatlab function from the Mathworks File Exchange. Depending on the dataset we are analyzing, the chapters and sub-chapters can vary.
What I want to do is to move around in the word document freely. We are already using this to go back the table of contents after the word document is written and write it.
I checked the VBA documentation on the GoTo method and the WdGoToItem command:
From the lists available there I conclude that the only way to go to a specific point in our document would be to use bookmarks. This would allow us to set bookmarks while creating the word document and then, later on, go to that specific bookmark using its unique name.
However, when testing the GoTo method, I ran into the problem that I can use all options from the table above (numbers 0 to 15), but using -1 (the number for bookmarks) always returns an error message. The code I am using is:
word_connector.Selection.GoTo(-1,1,1,'Evaluation')
Which returns the error message
Error using Interface.Microsoft_Word_15.0_Object_Library.Selection/GoTo
I have no idea how to change the code as all other numbers work fine. Help is much appreciated.
Best regards, Frederik

답변 (2개)

Jeffery Devereux
Jeffery Devereux 2018년 10월 30일
word_connector.Selection.GoTo(-1,0,0,'Evaluation')
Try this...
  댓글 수: 1
virup Rao
virup Rao 2019년 7월 25일
편집: virup Rao 2019년 7월 25일
I am using matlab 2018a and word 2010.
I have tried this but no use..any other alternative to Goto the already set bookmarks in the document.
I would also need to set bookmarks to a specific text box or cell in the table.
If anybody can help.

댓글을 달려면 로그인하십시오.


Jeffery Devereux
Jeffery Devereux 2019년 7월 25일
COMM = actxserver('Word.Application');
wdoc = COMM.Documents.Open([pwd '\Documents\YourWordDOC.docx']);
TH2 = [];
Headers = {'EmDes','DigModType','DigBitRate','NumDigStates','OccBW'};
Data = ??
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function InsertTableAtCursor(Data,Headers,COMM,BookMark,TH2)
[NoRows,NoCols] = size(Data);
TableData = Data;
TableHeader = Headers;
Doc = COMM.Selection;
Doc.Paragraphs.Alignment = 0;
Doc.Font.Name = 'Arial';
Doc.Font.Size = 7;
Doc.Font.Bold = 1;
Doc.Font.ColorIndex = 'wdBlack';
Doc.GoTo(-1,0,0,BookMark);
Table1 = WordCreateTable(COMM,NoRows,NoCols,TableData,1,TableHeader);
Table1.Columns(1).AutoFit
if ~isempty(TH2)
Table1.Cell(1,1).Range.Text = 'XXXXXXX';
A = Table1.Cell(1,1).Range;
Table1.Rows.Add(A);
Table1.Cell(1,2).Merge(Table1.Cell(1,3));
Table1.Cell(1,2).Merge(Table1.Cell(1,3));
Table1.Cell(1,2).Merge(Table1.Cell(1,3));
Table1.Cell(1,3).Merge(Table1.Cell(1,4));
Table1.Cell(1,3).Merge(Table1.Cell(1,4));
Table1.Cell(1,3).Merge(Table1.Cell(1,4));
Table1.Cell(1,4).Merge(Table1.Cell(1,5));
Table1.Cell(1,4).Merge(Table1.Cell(1,5));
LTH2 = length(TH2);
for j = 1:LTH2
Table1.Cell(1,j).Range.Text = TH2{j};
Table1.Cell(1,j).Range.ParagraphFormat.Alignment = 1;
end
end
Table1.Rows.Alignment = 1;
Table1.Range.Font.Size = 7;
Table1.AllowAutoFit = 1;
end

카테고리

Help CenterFile Exchange에서 Price and Analyze Financial Instruments에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by