Writing a hyperlink to an existing MS Word document via Matlab

8 ビュー (過去 30 日間)
Punch Powertrain
Punch Powertrain 2022 年 11 月 30 日
回答済み: Nithin 2025 年 4 月 23 日
Hello,
I'm having some trouble writing a hyperlink to an existing MS word document using my Matlab code.
The idea is the following: I have to write a large number of standardized reports with some text, some links, etc so I start from a word template document in which I replace certain words which are easily recognizable. Replace all for short pieces of text works fine, and adding large pieces of text also works, but when I want to have the links work as a hyperlink and not just be plain text, I get an error when I want to execute the VBA command. I can't seem to fix the issue with the existing answers to other questions.
Here is my code, the error is in the last line:
Word = actxserver('Word.application');
Word.Visible = 1;
set(Word,'DisplayAlerts',0);
Docs = Word.Documents;
Doc = Docs.Open(destination);
% Replace all for short strings
Replaced_string = 'Old_string';
New_string = 'Someting_new';
selection = Word.Selection;
selection.Find.Execute(Replaced_string,1,1,0,0,0,1,1,0,New_string,2,0,0,0,0);
% Replace string by very long string/paragraph
Replaced_string = 'Old_string';
New_string = 'Very long string or complete paragraph'
selection = Word.Selection;
T = selection.Find.Execute(Replaced_string,1,1,0,0,0,1,1,0,'',0,0,0,0,0);
if T % if the old string was found, the cursor is at the right location and the long string can be written
Word.selection.TypeText(New_string );
Word.selection.TypeParagraph;
end
% Replacing a string with an active hyperlink
Replaced_string = 'String_to_be_replaced_by_link';
New_string = 'www.google.com' ;
selection = Word.Selection;
% Look if you can find the Replace_string, and if so, write at that location your link
T = selection.Find.Execute(Replaced_string,1,1,0,0,0,1,1,0,'',0,0,0,0,0);
if T
word_handle = Word.Documents.Open(destination);
Word.ActiveDocument.Content.InsertAfter(New_string);
Word.ActiveDocument.Hyperlinks.Add(word_handle.Range(0, length(New_string)), New_string);
end
The error message I get is:
Error using Interface.0002099C_0000_0000_C000_000000000046/Add
Invoke Error, Dispatch Exception:
Source: Microsoft Word
Description: Opdracht mislukt (= comand failed (dutch))
Help File: wdmain11.chm
Help Context ID: 9066
Thanks in advance !

回答 (1 件)

Nithin
Nithin 2025 年 4 月 23 日
The main issue is with the way you're trying to add a hyperlink, the arguments of "hyperlinks.Add" are not passed properly. Instead, use "selection.Delete" to remove the found string before adding the hyperlink and use "selection.Hyperlinks.Add(selection.Range, ...)" to add the hyperlink at the cursor.
T = selection.Find.Execute(Replaced_string,1,1,0,0,0,1,1,0,'',0,0,0,0,0);
if T
% Remove the found text
selection.Delete;
% Insert the hyperlink at the current selection
% Hyperlinks.Add(Range, Address, SubAddress, ScreenTip, TextToDisplay)
selection.Hyperlinks.Add(selection.Range, LinkURL, '', '', LinkText);
end
Additionally, the error may also occur if you run the script multiple times without saving and closing the Word document, causing MATLAB to lose access to it. To prevent this, make sure to save and close the document at the end of your script:
pause(1);
% Save and close document if needed
Doc.Save;
Doc.Close;
Word.Quit;
Refer to the following documentation for more information: https://www.mathworks.com/help/matlab/ref/actxserver.html
Hope this resolves your issue!

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by