Word automation

Marcel
Member Posts: 26
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?
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
Marcel Bierens
0
Comments
-
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;0 -
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,
MarcelBest Regards,
Marcel Bierens0 -
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.0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions