• 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

    • Ashes of the Singularity II announced promising even larger battles, new faction, and more by Pulasthi Ariyasinghe A sequel for the 2016-released Ashes of the Singularity has officially been confirmed, and the original developer and publisher duo, Oxide Games and Stardock Entertainment, are back for this release too. Check out the debut trailer above. Ashes of the Singularity II is under development with a significantly expanded team compared to the original, according to today's announcement. It will be adding in a range of fresh units and gameplay systems for real-time strategy fans to dive into. More options for strategic gameplay, more intricate base building, and better enemy AI are listed as features for the new game as well. "Ten years ago, we set a new, literal benchmark for RTS games with massive battles and groundbreaking technology," said Brad Wardell, CEO of Stardock Entertainment. "With Ashes of the Singularity II, we’re raising the bar even higher, delivering strategic gameplay depth that RTS fans have always dreamed of.” The storyline of Ashes of the Singularity II will take place in 2031, with a complete single-player campaign, as well as cooperative and competitive multiplayer modes, being confirmed as features today. Moreover, a third faction is joining the fray this time, with Humans joining the fight alongside the original games' Post-Human Coalition and the Substrate. "Obviously, in the first game, the number one request was to have a human faction," adds Wardell. “Back then, we just couldn’t support having thousands of organic, walking, squishy people in the world and thus had to design in favor of machines. We’re really excited to bring the humans into the war and watch how they fare against the massive mechanical armies of the Substrate and PHC.” Ashes of the Singularity II is targeting a broad 2026 release window, with the team hoping to launch the title as the original celebrates its 10th anniversary. Its Steam store page is already up for wishlisting over here. Disclaimer: Neowin's relationship to Stardock
    • How exactly is a FREE image generation model a... Ponzi scheme?!?! I don't love all of these Gen-AI things flooding the market but there's nothing "ponzi" about it.
    • I can't decide if this is tacky or a really fun move for more casual players like myself to enjoy. I'll be curious to see how we are to fly to fictional land masses on a real globe.
  • Recent Achievements

    • One Month Later
      SamZrize earned a badge
      One Month Later
    • Week One Done
      SamZrize earned a badge
      Week One Done
    • One Year In
      SamZrize earned a badge
      One Year In
    • One Year In
      barracuda earned a badge
      One Year In
    • One Month Later
      barracuda earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      724
    2. 2
      +FloatingFatMan
      189
    3. 3
      ATLien_0
      179
    4. 4
      Xenon
      113
    5. 5
      neufuse
      109
  • Tell a friend

    Love Neowin? Tell a friend!