How can I find and edit/delete links from a Word document before JS requirement set 1.4? - ms-word

I'm developing a MS Word Add-In using the JS API. Currently, I need to find and edit or delete specific links inside the entire document. I know that this works using context.document.body.fields and then item.result.delete() and item.result.insertText('new text', 'Replace'). But context.document.body.fields is only available in the latest versions of Word (technically since requirement set 1.4).
I just want to know, are there alternative ways to do this in older Word versions before rs 1.4? And if so, what are those?

I think you can use document.body.search() method to find the hyperlink and this method will return a Range object, you can futher call something like InsertText() to replace it or call range.hyperlink = "" to remove the hyperlink.

Related

Can not find get_Item() method for ListTemplates

I'm working on a C#/VSTO application for automate some tasks in MS Word. I'm trying to change the start number of a numbered list using a ListTemplate. I've seen several examples of accessing a ListTemplate like this:
ListTemplate template =
app.ListGalleries[WdListGalleryType.wdNumberGallery].ListTemplates.get_Item(ref n);
However, Visual Studio doesn't recognize get_Item() as a method for ListTemplates. Other methods of ListTemplates are listed in the VS popup so I'm not sure what the issue is? How do I go about referencing a ListTemplate? Thanks!
When I check in VSTO there is no get_Item for ListTemplates. Instead, one uses the index. For example
ListTemplate template =app.ListGalleries[Word.WdListGalleryType.wdNumberGallery].ListTemplates[Word.WdListGalleryType.wdNumberGallery];

I don't have MS Word installed on my system, is there a way to use/include MS Word reference to be included in my VB 6 program?

I don't have MS Word installed on my system, is there a way to use and/or an MS Word reference in my VB 6 program?
No, the use Word automation always requires that Word is installed on the system.
Depending on what you are trying to achieve there are multiple alternatives though:
OpenXML SDK: For basically anything that does not require the document to be rendered (e.g. conversion to PDF does require rendering), you can directly manipulate the contents of the file using the API provided by the SDK. As the SDK is independent of Word/Office, you can use this approach anywhere you like, be it client-side or on the server, e.g. for server-side document creation and manipulation.
Word Automation Services: If you are within SharePoint and you need to manipulate or convert documents, then this would be the tool of choice.
Third-party libraries: For instance, Aspose offers a great library for automating and converting Office documents.
You can do this by using late binding. You can write code to use Word by dimming an object variable and using CreateObject to create a Word Application object, but you can't debug it. Like #Dirk Vollmar says, it depends on what you're trying to do as to whether this makes you life inconvenient, or a nightmare.
Here is a slightly modified version of a code sample from https://support.microsoft.com/en-us/kb/177097
Sub RunWord()
Dim wObj as Object 'must use object instead of Word.Application because Word is not installed
On Error Resume Next
' Get existing instance of Word if it exists.
Set wObj = GetObject(, "Word.Application")
If Err <> 0 then
' If GetObject fails, then use CreateObject instead.
Set wObj = CreateObject("Word.Application")
End If
' Add a new document.
wObj.Documents.Add
' Exit Word.
wObj.Quit
' Clear object memory.
Set wObj = Nothing
End Sub

Display an older version of a CQ page

For audit purposes I got the requirment to create a tool where the authors can look at older versions of a CQ page. I managed to get the available versions with the JCR VersionManager using the following code (used in a SlingServlet with cq:Page as the resourceType):
Session session = request.getResourceResolver().adaptTo(Session.class);
VersionManager vm = session.getWorkspace().getVersionManager();
VersionHistory versionHistory = vm.getVersionHistory(request.getResource().getPath());
VersionIterator vIt = versionHistory.getAllVersions();
while (vIt.hasNext()) {
Version version = vIt.nextVersion();
String no = version.getName();
Calendar createdDate = version.getCreated();
// do something with it
}
The path of the version points to e.g. /jcr:system/jcr:versionStorage/d6/23/4f/d6234f36-3360-4024-bee2-411020ac63ae/1.0 where I can see a child node called jcr:frozenNode which seems to represent the jcr:content node of this specific version.
How can I tell CQ to render the page in this version? I would expect an url with some parameter or selector, but I didn't find any documentation. I tried to reverse engineer it with the Timewarp, but there the URL seems to be still the original and the magic is hidden somewhere.
I was also in contact with adobe support regarding this, and beside the timewarp there seems to be no built in feature to achieve this. Nevertheless I did some experimenting and found a feasible workaround. Though it might not be easy for a complex layout with many fixed components in the template, luckily on our case we mainly have a parsys.
So my solution is the following: I load the older version through two selectors in the url:
I called it "versionhistory" which is used to take another rendering script called versionhistory.jsp on the page component.
contains the actual version/node name (replacing "." with "_" to not add more selectors
In my versionhistory.jsp I just add the correct path for the parsys component (taking the example path from the question), and include the same layout elements as in the default script e.g. page.jsp:
<cq:include path="/jcr:system/jcr:versionStorage/d6/23/4f/d6234f36-3360-4024-bee2-411020ac63ae/1.0/jcr:frozenNode/par" resourceType="foundation/components/parsys" />

Setting / accessing the Application.CutCopyMode property via UI

We are facing an error of "This method or property is not available because the clipboard is empty or not valid" in one of our Lotus Notes applications. The code basically opens a document, selects all the text, copies it and then pastes it in MS Word document. It does this continuously for a bunch of documents. I found this link which basically suggests adding Application.CutCopyMode = False to the code to resolve the issue.
I need to know can this property be accessed and set via UI for MS Word?
In the example you reference, that Keyword is actually part of the Excel object model, not the Word object model. So you are unlikely to find it in Word's UI. Perhaps you are doing something with Excel as well, or perhaps it is a keyword in the relevant Lotus object model as well?
[
The OP mentioned a link, http://www.tech-archive.net/Archive/Word/microsoft.public.word.vba.general/2007-06/msg00446.html
that suggested changing variables names, but that that did not work.
The linked article also suggests "sleeping for a few milliseconds between copying and pasting"
]

how to stop macros running when opening a Word document using OLE Interop?

As the title suggests, I have a .Net application which uses interop to open documents in Word. I have set
app.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable
before opening the document. According to the documentation, thhis "Disables all macros in all files opened programmatically, without showing any security alerts"
However, when I attempt to open one specific document I get a dialog box on the screen that says "could not load an object because it is not available on this machine". It's a customer document but I believe it contains a macro with references to a COM object which I don't have installed.
Am I doing something stupid? is there any way to actually disable macros when opening a Word document?
Try:
WordBasic.DisableAutoMacros 1
Bizarrely, this relies on a throwback to pre-VBA days, but still seems to be the most-reliable way to ensure that no auto macros are triggered (in any document - you may want to turn it back using the parameter "0").
I recently had a project where I had to process 6,000 Word templates (yes, templates, not documents) many of which had oddball stuff like macros, etc. I was able to process all but 6 using this technique. (I never did figure out what the problem was with those 6).
EDIT: for a discussion of how to call this from C#, see: http://www.dotnet247.com/247reference/msgs/56/281785.aspx
For c# you can use
(_wordApp.WordBasic as dynamic).DisableAutoMacros();
The whole code I'm using is:
using Word = Microsoft.Office.Interop.Word;
private Word.Application _wordApp;
...
_wordApp = new Word.Application
{
Visible = false,
ScreenUpdating = false,
DisplayAlerts = Word.WdAlertLevel.wdAlertsNone,
FileValidation = MsoFileValidationMode.msoFileValidationSkip
};
_wordApp.Application.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable;
(_wordApp.WordBasic as dynamic).DisableAutoMacros();