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

7 views (last 30 days)
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

Answers (2)

Jeffery Devereux
Jeffery Devereux on 30 Oct 2018
word_connector.Selection.GoTo(-1,0,0,'Evaluation')
Try this...
  1 Comment
virup Rao
virup Rao on 25 Jul 2019
Edited: virup Rao on 25 Jul 2019
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.

Sign in to comment.


Jeffery Devereux
Jeffery Devereux on 25 Jul 2019
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

Categories

Find more on Price and Analyze Financial Instruments in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!