MATLAB Answers

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

17 views (last 30 days)
Frederik Rösch
Frederik Rösch on 10 Nov 2017
Answered: Jeffery Devereux on 25 Jul 2019
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:
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
Try this...
  1 Comment
virup Rao
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';
Table1 = WordCreateTable(COMM,NoRows,NoCols,TableData,1,TableHeader);
if ~isempty(TH2)
Table1.Cell(1,1).Range.Text = 'XXXXXXX';
A = Table1.Cell(1,1).Range;
LTH2 = length(TH2);
for j = 1:LTH2
Table1.Cell(1,j).Range.Text = TH2{j};
Table1.Cell(1,j).Range.ParagraphFormat.Alignment = 1;
Table1.Rows.Alignment = 1;
Table1.Range.Font.Size = 7;
Table1.AllowAutoFit = 1;

Community Treasure Hunt

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

Start Hunting!

Translated by