• 0

[C++] MS Word Automation


Question

Have a problem inserting things into predefined places (like mail merge) in a word document.

Here's my code so far:

String AppPath = ExtractFilePath(Application->ExeName);
Variant MSWord;

MSWord = CreateOleObject("Word.Basic");
MSWord.Exec(Procedure("AppShowMSWord.Exec(Procedure("FileOpent;< AppPath+"Word.doc");
MSWord.Exec(Procedure("Insertt;< "Insert Text");

After googling.. i've found InsertAfter as a parameter... but I cant seem to work out how to use it :(

Has anyone done this before?

MdSalih

Link to comment
https://www.neowin.net/forum/topic/287507-c-ms-word-automation/
Share on other sites

4 answers to this question

Recommended Posts

  • 0
  mdsalih said:
*bump * :)

MdSalih

585503286[/snapback]

Sub InsertTextAtEndOfDocument()
    ActiveDocument.Content.InsertAfter Text:=" The end."
End Sub

Sub AddTextBeforeSelection()
    Selection.InsertBefore Text:="new text "
End Sub

From Word's help file:

  Quote
Finding and Replacing Text or Formatting

See AlsoSpecificsFinding and replacing is exposed by the Find and Replacement objects. The Find object is available from the Selection and Range object. The find action differs slightly depending upon whether you access the Find object from the Selection or Range object.

Finding text and selecting it

If the Find object is accessed from the Selection object, the selection is changed when the find criteria is found. The following example selects the next occurrence of the word "Hello." If the end of the document is reached before the word "Hello" is found, the search is stopped.

With Selection.Find

    .Forward = True

    .Wrap = wdFindStop

    .Text = "Hello"

    .Execute

End With

The Find object includes properties that relate to the options in the Find and Replace dialog box (choose Find from the Edit menu). You can set the individual properties of the Find object or use arguments with the Execute method as shown in the following example.

Selection.Find.Execute FindText:="Hello", _

    Forward:=True, Wrap:=wdFindStop

Finding text without changing the selection

If the Find object is accessed from a Range object, the selection is not changed but the Range is redefined when the find criteria is found. The following example locates the first occurrence of the word "blue" in the active document. If the find operation is successful, the range is redefined and bold formatting is applied to the word "blue."

With ActiveDocument.Content.Find

    .Text = "blue"

    .Forward = True

    .Execute

    If .Found = True Then .Parent.Bold = True

End With

The following example performs the same result as the previous example using arguments of the Execute method.

Set myRange = ActiveDocument.Content

myRange.Find.Execute FindText:="blue", Forward:=True

If myRange.Find.Found = True Then myRange.Bold = True

Using the Replacement object

The Replacement object represents the replace criteria for a find and replace operation. The properties and methods of the Replacement object correspond to the options in the Find and Replace dialog box (Edit menu).

The Replacement object is available from the Find object. The following example replaces all occurrences of the word "hi" with "hello." The selection changes when the find criteria is found because the Find object is accessed from the Selection object.

With Selection.Find

    .ClearFormatting

    .Text = "hi"

    .Replacement.ClearFormatting

    .Replacement.Text = "hello"

    .Execute Replace:=wdReplaceAll, Forward:=True, _

        Wrap:=wdFindContinue

End With

The following example removes bold formatting in the active document. The Bold property is True for the Find object and False for the Replacement object. In order to find and replace formatting, set the find and replace text to empty strings ("") and set the Format argument of the Execute method to True. The selection remains unchanged because the Find object is accessed from a Range object (the Content property returns a Range object).

With ActiveDocument.Content.Find

    .ClearFormatting

    .Font.Bold = True

    With .Replacement

        .ClearFormatting

        .Font.Bold = False

    End With

    .Execute FindText:="", ReplaceWith:="", _

        Format:=True, Replace:=wdReplaceAll

End With

  • 0
  mdsalih said:
Thank You :)

Now I've got to work out how to do it in Borland C++ Builder :-/

MdSalih

585511955[/snapback]

It's been so long since I've used BCB, but I think you can import an ActiveX/COM dll which BCB will wrap with VCL. I seem to recall doing something to that effect with MSXML.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • There won't be any major changes from beta to release.
    • Having been using Windows for 30 years and 3 Surface Pros in the meantime (4,6,9) and recently bought a 15" macbook air m4. The OS is indeed a big deal. Hardware wise the surface pro feels as nice as macbook air, but Windows...man it's it's own enemy now
    • 2013 to 2017...wtf, is Steve on the wacky tabacky? Never heard of this or this Nathan guy in Australia.
    • Dell says it can't be blamed for Windows Night Light breaking on its Arm PCs by Usama Jawad Night Light is a very handy feature that is available by default in Windows 10 and Windows 11. Essentially, when you toggle it on, Windows reduces the emission of blue light from the display which in turn decreases the stress on eyes and can potentially aid in better sleep too. While this feature generally works quite well, this hasn't been the case so far on some Dell PCs. Now, Dell has acknowledged the issue in a recently published support document. As the situation currently stands, Night Light does not work on the secondary display on certain Windows Arm PCs when an external monitor is connected to them. This happens on the following PC models: Inspiron 14 5441 Inspiron 14 Plus 7441 Latitude 5455 Latitude 7455 XPS 13 9345 Now, Dell has confirmed that this is not due to any hardware or software issue on its side, it's just a limitation of the Qualcomm Oryon chipset. In essence, this is a problem that is outside the control of Dell. What this means is that while customers can utilize built-in and third-party tools to adjust the color profiles of their displays, Dell can't really get Windows Night Light to work as-is on external displays connected to any of the Arm PCs listed above. The company has emphasized that there are no plans to support Windows Night Light on its existing Arm PCs, and that customers will just have to make do with what they have. However, this feature will likely work as designed with the "next generation" of Arm computers, which will presumably leverage a chipset that is not held back by this limitation. Windows on Arm has enjoyed decent support from software developers in recent times; Microsoft will be hoping that it can keep the momentum going with its next Snapdragon PCs.
    • that car they are showing run on very small areas and in controlled environments like no difficult intersection or construction areas while also having drivers in the car, while waymo has proven track record, one is working product which you can take today, the other is future promise with less service and more caveats (which has been delayed over a decade).
  • Recent Achievements

    • Week One Done
      moojay67 earned a badge
      Week One Done
    • Dedicated
      lethalman earned a badge
      Dedicated
    • Week One Done
      B4dM1k3 earned a badge
      Week One Done
    • One Month Later
      adnan.hebibovic earned a badge
      One Month Later
    • Week One Done
      adnan.hebibovic earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      595
    2. 2
      ATLien_0
      219
    3. 3
      Michael Scrip
      198
    4. 4
      +FloatingFatMan
      144
    5. 5
      Xenon
      135
  • Tell a friend

    Love Neowin? Tell a friend!