Duplicating Microsoft Word Style Bookmarks using C#

I am trying to use Microsoft Word stlye bookmarks in a C# Application.
Limitation: The application may be running on a PC that does NOT have Microsoft Word installed so my option of using VBA runtime stuff may be limited (unless there are available libs I am not aware of)
I would like to be able to import a .RTF document written in Microsoft Word (with bookmarks set in MS Word) then be able to find and change those bookmarks in my application.  Does anybody know if any references or libraries that may be available to do this sort of thing?
So Here is a quick Use Case to help clarify...
User has a file created by someone off site (using Microsoft Word) saved as a RTF document.
I receive the document and load their document into my app.  (App currently reads MS Word .RTF docs just fine)
(My app currently has some limited text / font / color edit functions.)
I would like to be able to
1) find the bookmarks that someone pre-set in MS Word and display them in a list in my app
2) be able to goto and edit any bookmarks in the file (using 'editor' in my app)
3) After I save the .RTF file from my app, have the other user be able to see the bookmarks (made by my app) in MS Word.
------------------------
In short...  I would like to know how to disply / goto/ set /delete and save Word style bookmarks using C#.
Hope this is clear.  Thank You in advance for your kind help.
dannyhopAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
lwebberConnect With a Mentor Commented:
First, remove the references to the the Interop stuff. Next, instal the PIA redistributables (you can download them from the site I mentioned earlier). Now put the references back in. In the Solution Explorer, click on the reference, then in the Properties panel, look at the directory name where the assemblies are alleged to exist. Make sure they are in fact there (use Windows Explorer to navigate to that directory).

As for the registry -- a CLSID is a unique key that lets any process on the computer locate another executable. The registry lists all the CLSIDs available on the computer, and (for most of them) gives the physical location of the chunk of code. Go into REGEDIT and do a search (F3) for the CLSID value (type it in with the hyphens, but without the curly braces). Repeat the search (F3) until you find the place where the registry shows the location of the component. It will often be in a key called InprocServer32. For each of those locations, use Windows Explorer to check that the dll (or whatever) is actually in that location.
0
 
lwebberCommented:
You can redistribute the word primary interop assemblies your C# app would need (you might want to check this with Microsoft). Try designing a "hello world" app in C# that uses the Word assemblies, package it, and install it on a machine without Word installed. It should run just fine. You can't run Word itself without WinWord.exe (and a proper install of Word). But your app won't reference the .exe at all. Word's functionality is not contained in WinWord.exe -- it's in the dll.

There is no way you would want to reverse engineer an RTF doc and diddle with it. I have done a bit of this, and it ain't pretty.

For more on the Word PIAs, see http://www.microsoft.com/downloads/details.aspx?FamilyID=59daebaa-bed4-4282-a28c-b864d8bfa513&displaylang=en (or Google word primary interop assemblies redistributable)
0
 
dannyhopAuthor Commented:
Hummm...  I did not know about being able to redistribute any of the PIA's from Office.  I will read up further on what this contains. Has anyone redistributed ANY PIA's from ANY office product (EXCEL, WORD, ACCESS, etc) within a C# win application.  
I guess the big question is...  Where is the Bookmark functionality.  Is it in then PIA functionality or does the PIA just allow direct access to the Bookmark functionality that must already be  present in an installed copy of MS Word?  I'd love to see how / what it entails.  ANY EXAMPLES of anything like this would be very helpful.
Also...  (from within the page your link directs me to) the statement:
Supported Operating Systems: Windows 2000 Service Pack 4; Windows Server 2003; Windows XP Service Pack 2
What about Vista and Win 7 ?  Is Microsoft that far behind their own development efforts?
------
(You are correct, I don't want to reverse engineer an RTF doc.  But if I can't get Word style functionality Bookmarks...  I would need to insert some kind of non-viewable characters that I could manage myself...  I don't even want to think about that...)
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
lwebberCommented:
That link is probably just an old one. Word 2003 and Word 2007 both work perfectly in XP, Vista and Win 7.

The PIA's *are* Word -- minus the UI. If you have Word installed on your dev machine and you use C# (or any other dotnet language) to manipulate a Word document using Word's object model, you are using the PIAs. Bookmark functionality is exposed in the Word object model (along with almost everything in Word). An easy way to get used to the Word object model is to write some simple VBA macros, like in the attached code. The objects in this macro are all exposed exactly the same way via the PIAs. The code snippet shows the equivalent in a C# application.

BTW, all those "ref missing" parms are rather ugly, but that's how you pass optional parameters from C# into a PIA method.

Sub Test()
    Dim thisBookmark As Bookmark
    For Each thisBookmark In Application.ActiveDocument.Bookmarks
        Debug.Print thisBookmark.Name & ": " & thisBookmark.Range.Text
    Next thisBookmark
End Sub

=====================
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Word;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
           Application wordApp = new Application(); 
           Document thisDoc;
           object fileName;
           fileName = "C:\\deleteme.docx";
           object missing = System.Reflection.Missing.Value;
           thisDoc = wordApp.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
           foreach (Bookmark thisBookmark in thisDoc.Bookmarks)
           {
               System.Console.WriteLine(thisBookmark.Name + ": " + thisBookmark.Range.Text );
           }
        }
    }
}

Open in new window

0
 
lwebberCommented:
I should have stressed that when you package up and install your app (along with the PIAs) on a machine that has never had Word or any other MS Office app installed, it will run just fine. All of Word's functionality (at least, the parts exposed to automation) are encapsulated in the PIAs. You don't need Word installed on the target machine.
0
 
dannyhopAuthor Commented:
This looks very intriguing.  I created a simple console app and loaded the code snippet.  I also added a reference to ....Interop.Word.
I get an exception like I have seen when a file or DLL is missing?    Do I need to add a ref to DLL for Word?  (Word 2007 is installed on this PC)?
Thank you again for your kind assistance.  I didn't know about any of this.
ref attached image...
exception.gif
0
 
lwebberCommented:
There is something screwy with your machine. First, when that error throws, click on "Copy exception detail to the clipboard", then paste the results in here. Second, go into your registry and find out what component has that CLSID.  
0
 
dannyhopAuthor Commented:
Looks like the CLSID is that of the included InterProcessor server I added to the solution reference. (see pic)

----------------------------  exception details follow -----------------------------
System.IO.FileNotFoundException was unhandled
  Message="Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 8007007e."
  Source="ConsoleApplication1"
  StackTrace:
       at ConsoleApplication1.Program.Main(String[] args) in C:\Documents and Settings\Danny\Desktop\MyNewProject\ConsoleApplication1\ConsoleApplication1\Program.cs:line 13
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:

exception.jpg
0
 
lwebberCommented:
Why did you add that to your solution? What happens when you remove it? Did you explorer the registry entry thoroughly? Do any of the registry keys refer to a disk location where a dll (or some other file) is supposed to be? If so, does the file actually exist? If the CLSID refers to a third party application or component, did you try reinstalling it? What else have you tried to resolve this?
0
 
dannyhopAuthor Commented:
Good questions...
Until I add the reference to the solution
the statement
using Microsoft.Office.Interop.Word;  
is meaningless and references to Document or Application in the .cs code are undefined?
-------------------------------------
I assumed the Interop DLLs were installed w/ Office when it was installed.  
Did I wrongly assume that I did not need to install the 'Redistributable' if i had Office already installed?
No dll's directly referenced in the Registry?  I am no expert on registry things so this may be getting over my head fast...
Thanks again for your help...
attached pic is the on;y entry in regisry under this key that has any Data
registry.jpg
0
 
dannyhopAuthor Commented:
Thanks, I learned alot here!
0
All Courses

From novice to tech pro — start learning today.