Word automation

MarcelMarcel Member Posts: 26
edited 2003-02-27 in Navision Financials
Can anyone help me with the following problem?

I've created a report that uses automation to export data to MS Word. I want the text placed at the boomark to be bold. For this i wrote the following code:

NameBookmark := 'BookmarkA';
WordBookmark := wdDoc.Bookmarks.Item(NameBookmark);
WordBookmark.Select();
WordBookmark.Range.Font.Bold := 1;
WordBookmark.Range.Text := 'Text';

Using this code the text will not be shown Bold.
What am i doing wrong?
Best Regards,

Marcel Bierens

Comments

  • Morten_SolbergMorten_Solberg Member Posts: 24
    hi Marcel: With Bookmarks, try this code:


    Variables:
    wdApp Automation Microsoft Word 9.0 Object Library: Application
    wdDoc ..Document
    wdRange ..Range
    wdBookMarks ..Bookmarks
    wdBookMark ..Bookmark
    TemplateName Text 250
    NameBookmark Text 30


    TemplateName := 'C:\myTemplate.dot';
    NameBookmark := 'myMark';

    IF NOT CREATE(wdApp,FALSE) THEN ERROR('WORD couldn't start');
    IF NOT EXISTS(TemplateName) THEN ERROR('File %1 not found',TemplateName);

    wdApp.Visible(TRUE); //or Visible:= TRUE;
    wdApp.ScreenUpdating(TRUE); //or ScreenUpdating:= TRUE;
    wdApp.Activate;

    wdDoc := wdApp.Documents.Add(TemplateName);
    wdBookMarks := wdDoc.Bookmarks;
    IF NOT wdBookMarks.Exists(NameBookmark) THEN
    ERROR('Bookmark not found');
    wdBookMark := wdBookMarks.Item(NameBookmark);
    wdRange := wdBookMark.Range;
    wdRange.Text('Text'); //or Text := 'Text';
    wdRange.Bold(1); //or Bold := 1;
  • MarcelMarcel Member Posts: 26
    Hi Morten,

    I solved the problem using the following code:


    NameBookmark := 'NameOfBookmark';
    WordBookmark := wdDoc.Bookmarks.Item(NameBookmark);
    WordBookmark.Select();
    WordBookmark.Range.Text := 'Text';

    wdRange := WordBookmark.Range;
    wdRange.Expand();
    wdFont := wdRange.Font;
    wdFont.Bold := -1;

    Using this code the text placed at the bookmark was shown bold!

    The problem was the selection of the text, you have to expand the selection before you can edit the font properties. when you don't expand the selection, the selection includes only the bookmark.

    Thanks for your help,
    Marcel
    Best Regards,

    Marcel Bierens
  • DenSterDenSter Member Posts: 8,307
    You could also try adding a bold style to your template and assign it to the bookmarked paragraph, instead of directly trying to format the text in VBA code. This way you don't have to worry about where to start and end your text selection, it makes it a lot easier to program and maintain.
Sign In or Register to comment.