Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do I need MS Word to create and edit a .docx file

Status
Not open for further replies.

Doug Lindauer

Programmer
Feb 2, 2017
29
0
0
US
Hopefully this isn't a stupid question. My current system creates and edits a Word file. Here's a sample of some of the code:
Code:
local oWord, wdReplaceAll  
wdReplaceAll = 2

oWord     = Createobject("Word.Application")
oDocument = oWord.Documents.Open(mWORDFILE)
oRange    = oWord.ActiveDocument.Content

with oRange.Find
    .Text = "CARDYEAR"
    .Replacement.Text = mCARDYEAR
    .Execute( ,  , , , ,  , , , , ,  wdReplaceAll)

All of my computers do have MS Office installed so I don't really have a way to easily test it. One of my potential users wants to know if he needs to have Office installed or is something like Open Office sufficient. So that's my question if anyone can answer it. :)
 
Thanks much for that info.

Ok so using Createobject("Word.Application") actually tries to invoke MS Word. I didn't know that level of detail but it makes sense. I'm an analyst, modeler, and database developer and I have no actual idea even what active X, com objects, or automation is about. :) Apologies, but I've always focused on the complexities of the database and pushing things around to get the data a user needs, not external stuff like this. So I'm just looking for the Cliff's Notes solution. If that entails telling them that they have to buy Office, ok. I'm just thinking that there has to be a fairly simple workaround.

So does anyone know if there is some equivalent like Createobject("OpenOffice.Application")? And if there is, shouldn't the same VBA code work on it once the document is opened? A .docx is just really a text document with embedded formatting codes and for the most part all I'm doing is replacing some markers with data, like a mailmerge function would do and then saving it as a .pdf.
 
There are alternative libraries for Excel. One by Greg Greene we've used for years: VFPx Xlsx. Has had a few bugs we've fixed. Minor things.

I don't know of any equivalent for Word. But, old versions of MS Office are cheap and give you automation.

--
Rick C. Hodgin
 
Automating OpenOffice is not directly compatible with automating Microsoft Office. Your Word Automation code will NOT work in OpenOffice.

The file formats are (somewhat) compatible. The command structures and object models are not.

Tamar
 
Since you specifically ask about docx.

One way of handling these files without Office/Word is first add the file extension ZIP and then unzip the file. You get a lot of files, including XML files, which contain one or the other XML format, there's office XML. That's how the tools by Greg Greene can work on such files.

The bad news is, when you expect the xml nodes to contain continuous text, you'll be astonished by how unintuitive a text is encoded just when writing and/or editing/correcting it differently.
I just wrote the sentence "The quick brown fox jumps over the laze dog." and saved that, unzipped the document1.docx.zip (after adding that file extension, of course), and find this in a file document.xml (and that's just a small portion of it):

[pre]<w:bookmarkStart w:name="_GoBack" w:id="0" /><w:bookmarkEnd w:id="0" /><w:r w:rsidR="761ADE2B"><w:rPr /><w:t xml:space="preserve">The quick </w:t>
</w:r><w:proofErr w:type="spellStart" /><w:r w:rsidR="761ADE2B"><w:rPr /><w:t>brown</w:t>
</w:r><w:proofErr w:type="spellEnd" /><w:r w:rsidR="761ADE2B"><w:rPr /><w:t xml:space="preserve"> </w:t>
</w:r><w:proofErr w:type="spellStart" /><w:r w:rsidR="761ADE2B"><w:rPr /><w:t>fox</w:t></w:r0>
<w:proofErr w:type="spellEnd" /><w:r w:rsidR="761ADE2B"><w:rPr /><w:t xml:space="preserve"> </w:t></w:r><w:proofErr w:type="spellStart" />
<w:r w:rsidR="761ADE2B"><w:rPr /><w:t>jumps</w:t></w:r><w:proofErr w:type="spellEnd" /><w:r w:rsidR="761ADE2B"><w:rPr />
<w:t xml:space="preserve"> </w:t></w:r><w:proofErr w:type="spellStart" /><w:r w:rsidR="761ADE2B"><w:rPr /><w:t>over</w:t>
</w:r><w:proofErr w:type="spellEnd" /><w:r w:rsidR="761ADE2B"><w:rPr /><w:t xml:space="preserve"> </w:t></w:r><w:proofErr w:type="spellStart" /><w:r w:rsidR="761ADE2B"><w:rPr /><w:t>the</w:t></w:r><w:proofErr w:type="spellEnd" /><w:r w:rsidR="761ADE2B">
<w:rPr /><w:t xml:space="preserve"> </w:t></w:r><w:proofErr w:type="spellStart" /><w:r w:rsidR="761ADE2B"><w:rPr /><w:t>lazy</w:t>
</w:r><w:proofErr w:type="spellEnd" /><w:r w:rsidR="761ADE2B"><w:rPr /><w:t xml:space="preserve"> dog.</w:t></w:r></w:p><w:sectPr>[/pre]

And sorry, I stopped to color the words that actually are the content of this. You see the difficulty to even drill down to the actual words. If you thought: "Fine, XML may look convoluted with pointy bracket tags all over the place, the actual text would be in tags like HTMLs paragraph tag <p>.

Well, as you see it's not. So even on that level you'll need something to extract the text, do a replace and then put the replaced text back.

I think code that goes the route of generating Office XML from test will actually produce pretty or at least prettier formatted XML than the Word application itself. And when using this to even just extract the actual text without any formatting to work on it, then you see how messy this is.

Chriss
 
Nothing is ever as simple as you'd like it be in this business, is it? Although sometimes you get surprised that something you thought was going to be hard turns out to be easy. But it's usually vice versa.

I hadn't tried looking around for legit copies of old Office versions but I'll give it a shot. I think that may be the simple solution I'm looking for if there are reliable sources for something like the 2007 or 2010 version. All they really would need would be Word. Anyway thanks very much for the excellent comments from all. It is greatly appreciated.
 
Don't go for a too old Office. Those versions are not only out of support for which you might not care much, but you also can't activate them.

Today I unsuccessfully tried to take over the activation of an already installed and activated Office 2010 to another computer using a tool called opabackup (office product activation backup). And its backup and restore reported success, still the Office isn't activated. There were some remarks about old and new hardware or OSes not differing too much for it to work.

But that experience just freshly in mind I'd not even try to install an older Office version and try to get product activation.

Just to be clear, the convoluted XML format I did show doesn't mean the Office versions using XML are not having an automation interface anymore. Just don't use Office.com, a subscription to Microsoft 365 (new name for Office 365) can include desktop applications you install locally, so watch out what you pick.

As I did this for a non-profit organization I also saw MS offers several free or low cost options for such organizations. Has anybody experience with that and how complicated it is to get eligibility? I tend to recommend using the free Office.com web apps to them.

Chriss
 
Those versions are not only out of support for which you might not care much, but you also can't activate them.

Chris's experience is slightly different to mine. I have a client who purchased Office 2010 in 2010. The licence "agreement" said that that one copy could be activated on a maximum of two computers. He installed and activated it on a laptop, and later on a desktop machine. So far so good. In 2014, he gave the laptop away. There appeared to be no way of de-activating Office, so he just deleted it. In 2016, he did something similar with the desktop machine.

He has since re-installed the same copy of Office on two new machines, without any problem. So he is completely legal, but as far as Microsoft knows, he has four copies in use.

In other words, Doug, you will probably be OK buying a copy of Office 2007 or 2010 (if you can find one). But of course there is no guarantee - especially in light of what Chris said above.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thanks, Mike.

I have to add, that the organization was poorly organized with keeping important documentation like the product keys of installed software, so I used a tool to recover the product key. That worked, the key is also accepted, i.e. it checks out as a valid key in itself, but it seems no activation server is found and the activation fails. Who knows if that's just because it's not a retail version key but only a MAK key or OEM version key which has restrictions and uses other activation servers/services than a retail version key. Anyway, I think the core problem is you don't get connected to activation servers at Microsoft for unsupported versions, no matter if MAK/OEM or retail key activation.

Shouldn't it be possible from Microsoft to be very clear on such things, with an announcement of suspending activation? I even think legally MS should need to provide activation even for unsupported old versions, as it's still just the decision of the user to continue using it unsupported and with the security risks involved in that, which are quite low for this organization. They barely use their PCs for more than mail (very used to Outlook 2010 now), Facebook, and writing word documents. They're quite uncapable of getting along with learning to use any software.

But then they also don't need any automation of Office. The perhaps most hi-tech fancy special solution they use for sending a newsletter is using a mail distribution list in their outlook addressbook they choose for BCC and use their own mail address as only normal TO recipient.

It's not the worst solution, I think, but with todays laws about newsletters and opt-out it's a bit sad they can't afford a service like mailchimpor similar for such things.

Chriss
 
You can still activate Office 2010 - I did it quite recently.

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Griff,

Home and student? Professional? another version? Within a company using a VLSC server?
It may also depend on country/location.

Chriss
 
Heres' another idea, Doug,

taken the topic of activation aside, if it's sufficient for you to use the Office web apps, you can also automate them with the Microsoft 365 API, it's a REST based web service. If all you need is the REPLACE functionality then that could be quite easy, if you do this to fill in data into templates, there also would be the mail merge functionality in Word itself, so that could be done without automation from outside, just using the Word features.

If, on the other hand, this was just one of many automation example, it's surely the easiest thing to get any activatable desktop version of Office or Word only. I just remember it's long ago since single purchasing Office products was essentially cheaper than a whole Office version, so I'd get Office for sure, alone for also using Outlook and sometimes Excel.

Chriss
 
Griff said:
You can still activate Office 2010 - I did it quite recently.

That's useful to know. But, in the case I described, the user was able to carry on using Office without any activation.

Chriss said:
Home and student? Professional? another version? Within a company using a VLSC server?
It may also depend on country/location.

In the case I described, it was a Home and Business version, purchased retail in the USA. I think the OSs in question were, first Vista, then Windows 7, then Windows 10 (but not sure about that).

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Home and Business, on Server 2012, it's not 100% happy if you use it interactively (from an RDP session)
- but for automation it is as good as it gets.

Personally, I do not think M$ has made any significant improvements to Office in a very long time.
Apart from the ribbon menus, which are not something I enjoy!

B-)

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
And another idea about merging would be doing this in VFP first, with normal ASCII/ANSI texts and TEXTMERGE or TEXT TO...ENDTEXT, also possible HTML with things like captions and images, then creating word documents from these final (html) texts.

Chriss
 
Doug,

Here's a completely different approach.

If you goal is only to replace certain text strings in the docment (as in your example code, where you are replacing every occurrence of "CARDYEAR" with something else), you could consider the following:

1. Programmatically copy the DOCX file to a different location, and change its file extension from DOCX to ZIP. It is now a standard WinZip-style archive (compressed file).

2. Use Winzip Command Line to programmatically extract the file named "document.xml" from the archive. Winzip Command Line is a free add-on to WinZip. It allows you to code a command line to automate any of Winzip's feature, which you can then run from within your VFP program.

3. Read the xml file into a VFP variable (using FILETOSTR()).

5. Do the search and replace (probably using STRTRAN()).

6. Save the variable back to the xml file (using STRTOFILE()).

7. Use Winzip Command Line to copy the xml back to the archive.

8. Finally, change the extension back to DOCX and copy the file back to its original location.

Note that this solution is not free. You would have to buy a copy of Winzip (any version), but that would probably be considerably cheaper than buying a copy of Office.

Note also that the above is just off the top of my head. I have never actually tried to do it. (So don't forget to back up the DOCX before you start.)

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Okay Griff,

and thanks for the info. I'd look into it, if it still works after a grace period of 25 days. And that's just one definition of the grace period I found. There's lots of contradicting information.

In my case it's Office 2010 Professional on Windows 10. 2012 I think compares to Windows 8?

The older PC the office came from was still Vista.

And to be very clear about this: I didn't copy it over. I used an ISO of Office 2010 and the recovered product key. The Office installed and works. But any application has a red title bar stating activation failed. And a dialog starting with the application stating activation is pending, with no button to just activate but offering to change product key. I tried several times without and with restart to reenter the key I recovered with a tool from their old PC. It may have needed the C/DVD they inintially installed from and don't have anymore. Which also may be another reason for the activation fail.

An easy option could be to simply buy a new key. There are many offerings for really low prices, but this all is a grey area I don't like them to enter.

Chriss
 
It says Product Activated under Help About.

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Mike,

just two things first:
1. Zipping is free in Windows since XP has baked that into the OS, or you could use 7Zip and others.
2. Did you see what I posted as document XML for a simple sentence document? Again, this is not the result of longwinded tries to get the XML that convoluted, I just wrote the sentence in one go.

As another warning about trying to use a simple STRTRAN in the XML, I also found cases where the text is split within the word, so you can't even rely on whole words still being together within one XML tag.

You'd really need to parse out the text by removing all tags in the simplest case and then it's the question of how you put the final text back in.

I haven't looked into the source code of Greg Greens tools, wasn't there also another one that did some library. I think they mostly generate cleaner Office XML, but don't consume, parse or enable editing existing XML.

Which is why I suggest still automating Word as Word.Application or by the Rest web service as one idea, making use of mail merge or changing the process to first generate text, probably HTML for better formatting options including images, colors and font styles, and then finally producing a doc or docx from that.

If it's about reports, there's still XFRX, PDF printers, cause Word also can open PDF (for reading) and, well, there are lots of PDF readers anyway.

Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top