DigDeep

Word and Excel format in Edit Menu

8 posts in this topic

#1 ·  Posted (edited)

I have an edit menu which is working fine getting the Text format. But my goal is to input the Word and Excel format in the Edit Menu.

Is there a way I can get word and Excel text / tables format inside the Edit menu?

Edited by DigDeep

Share this post


Link to post
Share on other sites



What do you mean by "edit menu"? Do you mean the context menu of an edit control (The right click menu)?

Do you have some code you've already tried?

Share this post


Link to post
Share on other sites

@InunoTaishou

Here is the test code. If I provide .txt in the $Path it works fine. But I also want to be able to get Word and Excel format in the Edit.

 

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>

#Region
$Form = GUICreate("Email Communications", 1900, 979, -1, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_TABSTOP))
$Add = GUICtrlCreateButton("Add", 1624, 240, 179, 41)
$Edit = GUICtrlCreateEdit("", 16, 296, 1785, 673)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)

GUISetState(@SW_SHOW)
#EndRegion


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

            Case $Add
                GUICtrlSetData($Edit, Path())
    EndSwitch
WEnd

Func Path()
    $Path = "Providing word / Excel file path"
    Return FileRead($Path)
EndFunc   ;==>Path

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

If I'm understanding correctly you want to get the excel/word document and display it in the edit control?

You won't be able to retain the text properties (color, attributes, indenting, etc) in a regular Edit control but this will copy just the text and return it as a valid string (Note: No error checking has been added and the word/excel program will flash on the screen, I don't know any way to not flash on the screen)

#include <Word.au3>
#include <Excel.au3>
#include <Array.au3>

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>

#Region
$Form = GUICreate("Email Communications", 1900, 979, -1, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_TABSTOP))
$Add = GUICtrlCreateButton("Add", 1624, 240, 179, 41)
$Edit = GUICtrlCreateEdit("", 16, 296, 1785, 673)
GUICtrlSetState(-1, $GUI_DROPACCEPTED)

GUISetState(@SW_SHOW)
#EndRegion


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

            Case $Add
                GUICtrlSetData($Edit, Path())
    EndSwitch
WEnd

Func Path()
    Local $Path = FileOpenDialog("Select File", @UserProfileDir & "\Documents\", "All (*.*)")

    If (StringInStr($Path, ".doc") or StringInStr($Path, ".docx")) Then
        Return _FileReadWordDoc($Path)
    ElseIf (StringInStr($Path, ".xls") or StringInStr($Path, ".xlsx")) Then
        Return _FileReadExcelBook($Path)
    Else
        Return FileRead($Path)
    EndIf
EndFunc   ;==>Path

Func _FileReadWordDoc($sPath)
    ; Create a word object
    Local $oWord = _Word_Create()
    $oWord.Visible = False
    Local $oWordDoc = _Word_DocOpen($oWord, $sPath, 0, 0)
    Local $sClipRet = ClipGet()
    $oWordDoc.Range.Copy()
    Local $sData = ClipGet()
    ClipPut($sClipRet)

    _Word_DocClose($oWordDoc)
    _Word_Quit($oWord)

    Return $sData
EndFunc

Func _FileReadExcelBook($sPath)
    ; Create an excel object
    Local $oExcel = _Excel_Open()
    $oExcel.Visible = False
    Local $oExcelApp = _Excel_BookOpen($oExcel, $sPath)
    Local $aExcel = _Excel_RangeRead($oExcelApp)

    _Excel_Close($oExcel)

    Return _ArrayToString($aExcel, @TAB)
EndFunc

 

Edited by InunoTaishou

Share this post


Link to post
Share on other sites

Thank you InunoTaishou

But the way it displays both Word and Excel is in simple Text format which will not work out for me. It looks like in GUICTRLEDIT mode we cannot embed any other format other than simple text.

The reason I wanted to use Edit mode here so I can make any changes after adding files and then looking for further actions.

Is there any other way to use in place of Edit mode that can display the files in the same format they are...

 

Share this post


Link to post
Share on other sites

The regular Edit control cannot support multi-word formatting, the _GUICtrlRichEdit_ control does. This is something small I made that can, sort of, mimic word, to give you an idea of what RichEdit can do.

I, personally, don't know of a way to Stream from a non .rtf file (_GUICtrlRichEdit_StreamFromFile($sFile) will let you set the RichEdit from a .rtf file keeping the text properties) to a RichEdit retaining the text properties. Someone out there may have made it, I don't know of any UDF for AutoIt to do this.

You can, however, paste formatted text into a RichEdit control keeping the text properties. So the _FileReadWordDoc could just return and you can ctrl+v into the rich edit (in the script), keeping all of the text formatting.

I'm heading out, I'll check back later.

Share this post


Link to post
Share on other sites

This would get you started. The only other way I could think to do it would be to extract all the xml files (Since word and excel files are just .zip files renamed) and parse the document yourself.

#include <Word.au3>
#include <Excel.au3>
#include <Array.au3>
#include <GuiRichEdit.au3>

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>

#Region
$Form = GUICreate("Email Communications", 1900, 979, -1, -1, BitOR($GUI_SS_DEFAULT_GUI, $WS_MAXIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_TABSTOP))
$Add = GUICtrlCreateButton("Add", 1624, 240, 179, 41)
$hRichEdit = _GUICtrlRichEdit_Create($Form, "", 16, 296, 1786, 673, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))

GUICtrlSetState(-1, $GUI_DROPACCEPTED)

GUISetState(@SW_SHOW)
#EndRegion


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            _GUICtrlRichEdit_Destroy($hRichEdit)
            Exit

        Case $Add
            Path()
    EndSwitch
WEnd

Func Path()
    Local $Path = FileOpenDialog("Select File", @UserProfileDir & "\Documents\", "All (*.*)")
    Local $sClip = ClipGet()

    If (StringInStr($Path, ".doc") or StringInStr($Path, ".docx")) Then
        If (_FileReadWordDoc($Path)) Then
            ControlFocus($Form, "", $hRichEdit)
            send("^{v}")
            ClipPut($sClip)
        EndIf
    ElseIf (StringInStr($Path, ".xls") or StringInStr($Path, ".xlsx")) Then
        If (_FileReadExcelBook($Path)) Then
            ControlFocus($Form, "", $hRichEdit)
            send("^{v}")
            ClipPut($sClip)
        EndIf
    Else
        Return _GUICtrlRichEdit_SetText($hRichEdit, FileRead($path))
    EndIf
EndFunc   ;==>Path

Func _FileReadWordDoc($sPath)
    ; Create a word object
    Local $oWord = _Word_Create()

    If (@Error) Then
        _ReadError("Creating word object failed with error code " & @Error)
        Return False
    EndIf

    $oWord.Visible = False
    Local $oWordDoc = _Word_DocOpen($oWord, $sPath, 0, 0)

    If (@Error) Then
        _ReadError("Opening " & StringRight($sPath, StringInStr($sPath, '\', 0, 1, -1)) & " failed with error code " & @Error)
        _Word_Quit($oWord)
        Return False
    EndIf

    $oWordDoc.Range.Copy()

    _Word_DocClose($oWordDoc)
    _Word_Quit($oWord)

    Return True
EndFunc

Func _FileReadExcelBook($sPath)
    ; Create an excel object
    Local $oExcel = _Excel_Open()

    If (@Error) Then
        _ReadError("Creating excel object failed with error code " & @Error)
        Return False
    EndIf

    $oExcel.Visible = False
    Local $oExcelApp = _Excel_BookOpen($oExcel, $sPath)

    If (@Error) Then
        _ReadError("Opening " & StringRight($sPath, StringInStr($sPath, '\', 0, 1, -1)) & " failed with error code " & @Error)
        _Excel_Close($oExcel)
        Return False
    EndIf

    ; Local $aExcel = _Excel_RangeRead($oExcelApp)

    $oExcel.Range("A1:C10").Copy()

    _Excel_BookClose($oExcelApp)
    _Excel_Close($oExcel)

    Return True
EndFunc

Func _ReadError($sErrorMsg)
    _GUICtrlRichEdit_SetText($hRichEdit, $sErrorMsg)
EndFunc

 

Share this post


Link to post
Share on other sites

Is there any other way to use in place of Edit mode that can display the files in the same format they are...


 

The only way I can think of is to embed the object

#include <GUIConstants.au3>
#include <WindowsConstants.au3>

$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")

$sPath = StringRegExpReplace(@Autoitexe, '^(.+\\)[^\\]+$', "$1") & "Examples\Helpfile\Extras"
$FileName=$sPath & "\_Excel4.xls"
If not FileExists($FileName) then
  Msgbox (0,"Test", "file error")
  Exit
Endif

$oExcelDoc = ObjGet($FileName)  
If not IsObj($oExcelDoc) then Exit Msgbox (0,"Test", "error")
       
$parent = GUICreate ( "Embedded ActiveX Test", 640, 580)
GUISetState() 
$child = GUICreate ( "child", 400, 300, 30, 90, BitOr($WS_CHILD, $WS_CLIPCHILDREN), -1, $parent)
GUISetState()

GUISwitch($child)
$GUI_ActiveX = GUICtrlCreateObj($oExcelDoc, 0, 50, 400, 250 )
   With $oExcelDoc
        .CommandBars("Full Screen").Visible = False
        .CommandBars("Standard").Enabled = False
        .CommandBars("Formatting").Enabled = False
        .CommandBars("Worksheet Menu Bar").Enabled = True  ;False 
        .Application.DisplayFormulaBar = False
    EndWith

While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE 
                $oExcelDoc.Close 
                ExitLoop
         EndSelect
Wend


Func MyErrFunc()
  $HexNumber=hex($oMyError.number,8)
  Msgbox(0,"AutoItCOM Test","We intercepted a COM Error !"       & @CRLF  & @CRLF & _
             "err.description is: "    & @TAB & $oMyError.description    & @CRLF & _
             "err.windescription:"     & @TAB & $oMyError.windescription & @CRLF & _
             "err.number is: "         & @TAB & $HexNumber              & @CRLF & _
             "err.lastdllerror is: "   & @TAB & $oMyError.lastdllerror   & @CRLF & _
             "err.scriptline is: "     & @TAB & $oMyError.scriptline     & @CRLF & _
             "err.source is: "         & @TAB & $oMyError.source         & @CRLF & _
             "err.helpfile is: "       & @TAB & $oMyError.helpfile       & @CRLF & _
             "err.helpcontext is: "    & @TAB & $oMyError.helpcontext )
  SetError(1) 
Endfunc

 

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