Symptom
A PowerBuilder application uses OLE automation to resolve bookmarks in a DOC or DOCX document or template. This has worked well under MS Word 2007.
Using PowerBuilder and MS Word 2013, there are errors and no data resolves to the bookmark.
Error occurs at line with this code: lole_word.activedocument.bookmarks.item (ls_tag_name).range.text = ls_tag_value
Environment
- PowerBuilder
- Microsoft Word 2007
- Microsoft Word 2013
Reproducing the Issue
1. If you don't have MS Word2013, download a trial version.
2. Install MS Word 2013.
3. Unzip bookmark.zip.
4. Open testole.pbw and testole.pbt.
5. Open w_main/bookmark change cb.
6. Verify the location path for the ls_template_file_name
7. Run the app and click on the Bookmark Change cb.
8. Close the app and open the mytts.docx and notice PB did not change/save
the bookmark name from case_short_option to MyTest.
The powerscript code in step 5 is:
oleobjectlole_word
string ls_template_file_name,ls_ext,ls_doc_save_name,ls_columnname,ls_tag_name,ls_tag_value
integer i
long li_step_count,ll_row,li_return,ll_pos
ls_template_file_name = "C:\112957\Test-ASC-Opinion-Template.docx"
ls_doc_save_name = "C:\112957\mytts.docx"
lole_word = CREATE oleobject
TRY
lole_word.ConnectToNewObject('word.application')
CATCH (oleruntimeerror a)
MessageBox('External Editor - Word Connect Error', 'Error connecting to Microsoft Word. Terminating function.',StopSign!)
DESTROY lole_word
Return -1
END TRY
// Open the Word Template file
TRY
lole_word.Documents.open(ls_template_file_name, FALSE, TRUE,FALSE)
CATCH (oleruntimeerror b)
MessageBox('External Editor - Word Template Open Error', 'Error opening Template file with Microsoft Word. Terminating function.',StopSign!)
DESTROY lole_word
Return -1
END TRY
If ls_ColumnName = ls_columnname Then
TRY
ls_tag_name ="case_short_caption"
If lole_word.activedocument.bookmarks.exists(ls_tag_name) Then
//Determine if a Bookmark exists with the column/computed field name, and if so, then replace bookmark with datasource column value
ls_tag_value = "MyTest"
lole_word.activedocument.bookmarks.item(ls_tag_name).range.text = ls_tag_value
lole_word.activedocument.saveas(ls_doc_save_name, 16)
End If
CATCH (oleruntimeerror d)
MessageBox('External Editor - Word Bookmark Error', "Error locating or replacing Bookmark '" + ls_tag_name + "'.~r~n" + "Error Details: " + "Class: " + d.Class + "~r~n" + "Routine Name: " + d.RoutineName + "~r~n" + "Message: " + d.GetMessage() + "~r~n" + "Line Number: " + string(d.Line) + "~r~n" + "PB Error Num: " + string(d.Number) + "~r~n" + "Text: " + d.Text + "~r~n" + "Description: " + d.Description + "~r~n" + "ObjectName: " + d.ObjectName + "~r~n" + "Source: " + d.Source + "~r~n", StopSign!)
li_return = -2
END TRY
End IF
Cause
Syntax changes from MS Word 2007 and MS Word 2013
Resolution
When using MS Word 2013, change the powerscript code from:
TRY
lole_word.Documents.open(ls_template_file_name, FALSE, TRUE,FALSE)
CATCH (oleruntimeerror b)
MessageBox('External Editor - Word Template Open Error', 'Error opening Template file with Microsoft Word. Terminating function.',StopSign!)
DESTROY lole_word
Return -1
END TRY
Use the modified powerscript code with MS Word 2013:
TRY
lole_word.Documents.open(ls_template_file_name, FALSE, TRUE,FALSE)
lole_word.ActiveWindow.View.ReadingLayout = False
CATCH (oleruntimeerror b)
MessageBox('External Editor - Word Template Open Error', 'Error opening Template file with Microsoft Word. Terminating function.',StopSign!)
DESTROY lole_word
Return -1
END TRY