Jump to content

[Resolved] Issue Pasting into Word..


hogfan
 Share

Recommended Posts

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
Link to comment
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

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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
Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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. 

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Glad you got it sorted out :) 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...