Docx4j API performing mail merge for input dotx to output docx file giving alert - docx4j

While we perform mail merge through FieldMailMerge.java , we got the output in a docx file but when we open it in Microsoft Word 2010 an alert message appear:
enter image description here

Configured with OutputField.REMOVED, the mail merge was removing an empty paragraph from a table cell. But as per the error message, Word requires a w:p in each w:tc (ie a w:tc can't be empty).
So removing such a w:p is a bug, now fixed at https://github.com/plutext/docx4j/commit/13c48ce3f2fb2cbad02b784b6dcf1930428bb9e8
Or you can use something other than OutputField.REMOVED, for example: org.docx4j.model.fields.merge.MailMerger.setMERGEFIELDInOutput(OutputField.DEFAULT);

Related

How to pull text from a shape in a Word document using VBA?

I'm trying to grab the text from inside a shape on a Word document.
Sub textgrab()
MsgBox ActiveDocument.Shapes("Rectangle 85").TextFrame.TextRange.Text
End Sub
I get the error:
Run-time error '-2147024809 (80070057)':
The item with the specified name wasn't found.
In the Word document when I go to the top menu, hit the shape format tab, and in the arrange section, I select 'selection pane', I get a list of all the shapes, 'Rectangle 85' is there.
When I select it, it highlights the box i'm trying to grab the value from.
This is a pdf that I've opened in Word. I'm trying to automate a process that will open a pdf invoice, grab the dollar total, and pull it into Excel.
Solution for those that stumble upon this later. I used the following:
ActiveDocument.ActiveWindow.Panes(1).Pages(1).Rectangles.Item(i).Range
Word can only extract text from Drawing objects. These are inserted in the UI, for example, from Insert/Shapes. Shape.TextFrame.TextRange has no OCR capabilities, so can't be used to get text "embedded" in other kinds of graphic objects, such as an embedded PDF file or a JPG or anything similar.
When uncertain whether a particular Shape supports reading or writing text, right-click it in the UI and see if the menu selection Add Text or Edit Text is available.

vba code - Replace text in html email without breaking formatting

I am trying to replace specific text in an email (skype meeting invite) with a custom piece of text that is a URL. I can do this fine for plain text via a vba macro in outlook click to run (2016/2019), but if I run the below against the expected html email that the meetings create it breaks the formatting completely.
Sub ChangeS4bMeeting()
Application.ActiveInspector.CurrentItem.body = Replace(Application.ActiveInspector.CurrentItem.body, "Conference ID: ", "https://linkhere")
End Sub
It ends up replacing the text correctly and adding what follows that space after the colon to the replaced text as the URL, but formatting of the whole invite email breaks.
I have tried using "HTMLBody" value instead of "body" value but this fails to run at all as it breaks on that line. Any ideas? Would it also be possible to have this run automatically when I selected New Items > Skype Meeting in outlook rather than running the macro manually (or have the macro include the creation of a skype meeting directly)?
Thanks in advance!
You must ensure that the content placed in the HTMLBody is properly formatted and that any such text is placed between the and tags in the HTMLBody.
For more information, please see the following link:Find and Replace as a VBA Macro
You can create custom rules that execute scripts to implement specific conditions for executing the script.
please see the following link: Create a Custom Rule executing a Script

Section Header Range.Text Returning Empty String Instead of Actual Text

I have a PDF file that I am trying to parse text out of. I opened the file using Microsoft Word, and text I need is in the header. On the first page, the header is justified left with a center tab that has the text (plain English name document title instead of the complicated reference name) that I am trying to grab. There is a right tab that has a page number control that I don't care about.
When I try to run the following:
Debug.Print ThisDocument.Sections(1).Headers(wdHeaderFooterPrimary).Exists
it gives me True, so I know the header exists. However, when I try to run
Debug.Print ThisDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text
it gives me nothing but an empty string, which I can further confirm by wrapping it in a Len(…) command which gives me 1. How can I get the text out of the header?
Of note, I tried using some Adobe SDK functions which would have been easier, but I do not have the professional Acrobat suite so I do not have access to those tools. Hence the MS Word workaround.

Microsoft Excel Search Engine - Hyperlink Results Provides Error Message

I've been trying for two days to figure out this problem.
I have created an Excel search engine to search through a database of hyperlinks in the same worksheet. I want the results to display working hyperlinks, but right now it just displays the text. When the hyperlink is clicked, I get an error message - "Cannot open the specified file."
Can someone please help me. I've tried multiple codes including the following:
=HYPERLINK("#"&VLOOKUP(D5,A2:C91,3,FALSE),""""))) to no avail.
I can also email the spreadsheet if a email is provided. Thank you!
A hyperlink cannot start with the # sign. If the link is to a file, then you need to start with the file path, like shown in the Excel help
=HYPERLINK("D:\FINANCE\1stqtr.xlsx", H10)
If the link goes to another sheet in the same file, use
=HYPERLINK("[Budget.xlsx]E56", E56)
The Vlookup will need to return the correct file path or a valid URL, including the "http://"

How to parse text from a plain text file and use the result to highlight a PDF file

Back in 2010, some guy claimed to be capable of doing this:
http://www.mobileread.com/forums/showthread.php?t=103847
"The Kindle stores its annotations in a Mobipocket (".mobi") file for each document and in one long text file named "My Clippings.txt." In this post I describe a system that synchronizes these annotations with PDF versions of the corresponding documents on a computer.
Overview
This system is embodied in an Applescript that parses the My Clippings file and controls the Skim PDF reader. The script first parses the clippings file. It then searches through the clippings and isolates any that come from documents on the kindle matching the filename of the currently open PDF file (the "pertinent clippings"). The script then iterates through each of the pertinent clippings, locating the matching text or location in the PDF document and applying highlights or adding notes where appropriate. The end result is an annotated, printable PDF document that matches the document on the kindle.
You can download the script here: http://dl.dropbox.com/u/2541109/KindleClippings.scpt. Before running the script, be sure to change the value of MyEmail to match your sending address and to verify that the Kindle mount point defined in MyClippingsFile is correct. You'll also need the free Skim PDF Reader.
To use it, send or copy a document file to your kindle. Remember, the kindle supports RTF, DOC, TXT and other common text formats and it will convert them into MobiPocket files internally for easier reading. Make some notes. Then take the same document that you just sent to the kindle and convert it to a PDF, e.g. by using the print to PDF feature in Mac OS X. Be sure to keep the filename the same. Open that same PDF in Skim and run the script. The highlights and notes should appear in the PDF.
If you're interested in how this works, read more on my blog here:
[not longer available]
Sadly, his script is no longer available, nor his blog.
Do you guys know if this is possible? I've been looking for this kind of functionality but can't find it anywhere.
This code, using python and PyMuPDF, works:
import fitz
# the document to annotate
doc = fitz.open("text_to_highlight.pdf")
# the text to be marked
text_list = [
"first piece of text",
"second piece of text",
"third piece of text"
]
for page in doc:
for text in text_list:
rl = page.search_for(text, quads = True)
page.add_highlight_annot(rl)
# save to a new PDF
doc.save("text_annotated.pdf")
The original 'My Clippings.txt' should be manipulated somehow, stringr could work but I found more useful to manipulate the text with multiple selections in Sublime Text---the goal is to have a list of highlights in the form of text_list above.
I am trying to do this using Python + a Windows macro creator (I'm a Win 7 user). You can use this approach to save the file as RTF, DOCX, PDF, etc. So far, it's been reasonably effective. Do note 2 things first:
1- the 'My Clippings' file only saves the text and the page, it does not save the location on the page (e.g., if you highlighted "mammals are animals" on page 15, it will give you this line and the page number, but if there are more than one "mammals are animals" on page 15, it's impossible to know which one you've highlighted). This is specially bad when you've highlighted a generic word, like "animals" or "the". And if you made comments by pressing on a word, this word is the only information you'll get about what in that page the comment refers to (e.g., I pressed on "animals" and the menu popped up, I selected 'Comment'. If "animals" appears 20 times on page 15, I cannot know to which of them my comment is refering).
2- The only way to retrieve the location on the page would be to analyze the *.pds and *.pdt files, inside the *.sdr folder in Kindle's drive ('Documents'). I can make no sense of these files.
In Python, you can run an easy code to extract the information you want from "My Clippings". Then you can use a macro creator to automate the process of copying the text and annotating it to the PDF (using Adobe Acrobat, for example), and then saving the PDF file.
Exemplifying with Adobe Acrobat:
Say I want to save all my highlights to the PDF file. First, I'll create a *.txt file on Python and run a script to copy all the strings related to the highlights to this new txt file (i.e., the highlighted text & the page number). Here's an example of such code (but first, copy and paste the "My Clippings.txt" file to the IDE start folder, e.g.: C:\Python27):
#for python 2.7.6
with open('My Clippings.txt','r') as rf:
with open('My Clippings Output.txt','w') as wf:
access = 0
bookTitle = 'Book Title'#put the book file's name as it's written in "My Clippings.txt"
for x in rf:
if access == 1:
wf.write(x)
if bookTitle in x:
access = 1
#for highlights only, instead of all annotations, include this if statement:
if (' | Added on ' in x) and ('- Your Note ' in x) or ('- Your Bookmark ' in x):
access = 0
if x == '==========\n':
access = 0
Then I'll create a macro to copy the page number in the "My Clippings Output.txt" file (it's inside the same folder you put the "My Clippings.txt" file), paste in Acrobat "page window", find (ctrl+f) the string in the page, then press "highlight". Done!
There's a catch in Acrobat though, the search/find function has a limit of ~28 chars, so your highlighted text can't be longer than that. I still don't know how to circumvent this limitation... I raised this problem here https://superuser.com/questions/884221/how-to-search-and-highlight-long-passages-in-a-pdf-file . As a bypass to the 28 chars limit on Acrobat, you can program the macro to copy using "shift"+"right arrow 28 times", and then use "cut" instead of "copy".
There are many free-to-use and libre macro creators out there, just google and choose the one you like best. For Windows, my favorite one is Pulover's Macro Creator. If you have any doubts about the process you can comment here or PM me. I'd prefer you to comment here, so that I can improve the answer