hogfan

[Resolved] Issue Pasting into Word..

14 posts in this topic

#1 ·  Posted (edited)

Hello, I am trying to work out an issue where i am using Word.au3 and the _Word_DocFindReplace function to find and replace existing fields in a a Word document with user input. All fields in the word document are getting replaced correctly with the exception of a list of cities.  The string that I am trying drop into the Word document is formatted like this

 

New York
Los Angeles
Nashville
Seattle

 

This issue is that when this is pasted into the Word Document with the  _Word_DocFindReplace function, it goes in with a leading space on the first entry, and each additional line has the "square" characters in front of the city names.  This is obviously some sort of Word text formatting issue.  How can I make this paste in as "Plain Text" with the hard return after each city name, but without the unwanted characters?  I will never understand Microsoft's reasoning for making the default paste action to "keep formatting of the text you are pasting",  Any help is appreciated on this one.

 

-hogfan

Edited by hogfan

Share this post


Link to post
Share on other sites



It is definitely and issue with the hard returns being pasted.  I tried a workaround in the Word document by creating a 1 x 1 table with no border and putting my field to be replaced in there. When the paste goes in now it's a single line that looks like this:

New York[][]LOS Angeles[][]Nashville[][]Seattle

Share this post


Link to post
Share on other sites

Can you please post your script so we can see how the user data is pasted into the Word document?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I can't post the entire script as it contains some company sensitive data, but the function looks similar to this example I put together.  I have even tried here to use stringsplit function to split the entered contents of the Edit control by the the @CRLF, & and the rebuild a clean string, but this stil does not seem to be working.  

 

Func _Generate ()
;Read the user input from the form
$myCities = GUICtrlRead($edtCities)
;Trim off any leading or trailing spaces
$myCities = _AllTrim($myCities)
;Deal with MS Word Multi-line return/paste BS
Local $arrCities = StringSplit($myCities,@CRLF, $STR_ENTIRESPLIT)
;Verify the array is getting populated by displaying it
_ArrayDisplay($arrCities)
;build the new string value from the array elements to clean up any characters Word doesn't like
;set initial value for variable
$myFormattedCities = Null
;Loop through the array and build out the new string
For $i = 0 To UBound($arrCities) -1
    $myFormattedCities = $myFormattedCities & $arrCities[$i] & @CRLF
Next
;display a message box to preview the string
Msgbox(0, "Test", $myFormattedCities)
$selectedTemplate = GUICtrlRead($cmbTemplate)
$FullTemplatePath = @ScriptDir & "\templates\" & $selectedTemplate
;Create the document from the Word template
Local $oWord = _Word_Create(False, True)
        ;Msgbox(0,"", "Created Word Instance")
        Local $oDoc = _Word_DocOpen($oWord, $FullTemplatePath)
        ;Msgbox(0,"", "Successfully Opened Fulfillment Letter Template")
        Local $oFind = _Word_DocFindReplace($oDoc, "[CITIES]", $myFormattedCities)
        
        $mySavePath = $SavePath & "\" & "test.doc"
        Local $returnSave = _Word_DocSaveAs($oDoc, $mySavePath)
        ;If there is a problem saving the file to the desktop display an error message
        If $returnSave = 0 Then
            MsgBox(48, "Error Saving File", "The file was unable to save! Please ensure you have write permission to the specified save directory!")
            _Word_Quit($oWord)
            Return
        EndIf
EndFunc

 

Edited by hogfan

Share this post


Link to post
Share on other sites

You could try this to clean the string:

$myCities = $oWord.CleanString($myCities)

Unfortunately a bullet is translated to a space character (CleanString) so you might need to translate

  • @CRLF & " " to @CRLF or
  • @CR & " " to @CR or
  • @LF & " " to @LF

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Strange.  When I use:

 

$myCities = $oWord.CleanString($myCities)

I display the string in a messagebox  before and after the cleanstring function, and afte the function is run the string is empty (or all spaces, not sure which)

Share this post


Link to post
Share on other sites

How do you create the list of cities? I assume by an AutoIt RichEdit Control.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Correct.  The user enters the list into a Multi-line AutoIT RichEdit Control.

Share this post


Link to post
Share on other sites

Can't you just grab the hex value of the bullets and use StringReplace to remove them?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Well, there are no bullets until the contents of the edit control get put into Word.  I am already doing a Find/Replace function to to replace a non- bulleted value that already exists in my Word template.  

Share this post


Link to post
Share on other sites

I was going to replace the unicode character with a backspace, but  Word won't show me the Unicode value for the character, even after changing to a Unicode font. Super frustrating.  I'm using Word 2013.  I tried hitting ALT+X, and that doesn't work........I found another article showing to highlight the character and click the show unicode button on the toolbar, but apparently that doesn't exist in Word 2013. 

Share this post


Link to post
Share on other sites

As you get the text from your GUI use AutoIt to display the text in hex. 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Finally got it sorted out.  The solution was to use the "@CR" instead of "@CRLF" when building the string.  Thanks you for the assistance.  I think I am good to go now

Share this post


Link to post
Share on other sites

Glad you got it sorted out :) 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now