麻辣家族討論版版's Archiver

zz0660 發表於 2021-11-3 10:41

關於EXCEL VBA 外部視窗 貼上問題請益

有做一個EXCEL VBA,檔案如下。
[attach]34336[/attach]

我有開啟兩個視窗,一個是上面提供的EXCEL檔案,裡面有一個按鈕,按下可以複製然後縮小EXCEL視窗。
[attach]34337[/attach]

另一個視窗是記事本,目前想要讓EXCEL內AA工作表的數據,貼在記事本上,類似CTRL+V的感覺,請問該如何以VBA呈現?

samwang 發表於 2021-11-3 12:29

[i=s] 本帖最後由 samwang 於 2021-11-3 12:32 編輯 [/i]

[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=117567&ptid=23465]1#[/url] [i]zz0660[/i] [/b]

另外新增文字檔,然後將資料寫入,謝謝。
Sub test()
Dim fs As Object, a As Object, R As Range
Application.ScreenUpdating = False
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("D:\" & 1 & ".txt")
For Each R In [A2:A32]
    a.WriteLine (R)
Next
a.Close
Application.ScreenUpdating = True
End Sub

zz0660 發表於 2021-11-3 14:11

[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=117568&ptid=23465]2#[/url] [i]samwang[/i] [/b]

samwang大大,有沒有別的方法呢?  不要另外新增文字檔,然後將資料寫入的方式。

像是自動幫我們按 CTRL+V 這樣?

samwang 發表於 2021-11-3 15:36

[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=117569&ptid=23465]3#[/url] [i]zz0660[/i] [/b]

有沒有別的方法呢?  不要另外新增文字檔,然後將資料寫入的方式。
[color=Blue]>>Set a = fs.CreateTextFile("D:\" &[color=Red] 1[/color] & ".txt")[/color]
[color=Blue]直接將 1 改為您目前的文字檔的名稱,這樣就可以了
至於您提的需求,直接貼到另一個視窗的文字檔,後學沒有想到解法,謝謝[/color]

samwang 發表於 2021-11-3 16:12

[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=117569&ptid=23465]3#[/url] [i]zz0660[/i] [/b]


另一個想法,讓使用者選取檔案(可同時選擇多個),請測試看看,謝謝。

Sub test1()
Dim fs, f, R As Range, fc%, x%
Application.ScreenUpdating = False
Set fs = CreateObject("Scripting.FileSystemObject")
With Application.FileDialog(msoFileDialogOpen)
    .InitialFileName = "D:\"
    .AllowMultiSelect = True
    .Show
    fc = .SelectedItems.Count
    If fc = 0 Then Exit Sub
    For x = 1 To fc
        FPath = .SelectedItems(x)
        Set f = fs.OpenTextFile(FPath, 2) '8:保留原資料;2:清除原資料
        For Each R In [A2:A32]: f.WriteLine (R): Next
        f.Close
    Next
End With
Set fs = Nothing: Set f = Nothing
Application.ScreenUpdating = True
End Sub

quickfixer 發表於 2021-11-3 16:55

[i=s] 本帖最後由 quickfixer 於 2021-11-3 17:00 編輯 [/i]

[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=117569&ptid=23465]3#[/url] [i]zz0660[/i] [/b]

這裡抄來的,[url=https://www.mobile01.com/topicdetail.php?f=511&t=4737630&p=57]原文網址[/url]
你run看看,像這樣嗎?秒數自己調整

    Sub Copy()

Dim file_name As String

Range("A2:A32").Copy
ActiveWindow.WindowState = xlMinimized

Delay (1) '等1秒

file_name = "C:\Windows\system32\notepad.exe" '記事本開新文件
Shell file_name, vbNormalFocus
AppActivate "未命名 - 記事本" '記事本視窗的標題
Delay (3) '等3秒

SendKeys "{ENTER}", True 'ctrl+v之前要先隨便輸入個東西
SendKeys "{BACKSPACE}", True
Delay (0.3) '等0.3秒

SendKeys "^V", True


End Sub

Sub Delay(setdelay As Single)

Dim StartTime As Double, NowTime As Double
StartTime = Timer * 100
setdelay = setdelay * 100
Do
NowTime = Timer * 100
DoEvents
Loop Until NowTime - StartTime > setdelay


End Sub

quickfixer 發表於 2021-11-3 17:13

[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=117580&ptid=23465]6#[/url] [i]quickfixer[/i] [/b]


    補充一下開舊檔的方法,像是c:\test.txt檔
file_name = "C:\Windows\system32\notepad.exe" & " " & "c:\test.txt" '執行檔檔名+舊檔路徑檔名
Shell file_name, vbNormalFocus
AppActivate "test - 記事本" '標題要改
注意,標題不一定長這樣,要看你的畫面為主

zz0660 發表於 2021-11-4 14:00

[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=117574&ptid=23465]5#[/url] [i]samwang[/i] [/b]

感謝 samwang & quickfixer 大大的熱心協助處理問題。
目前本人已試出VBA想要呈現的方式,如下↓

Sub Copy()

    [A2:A32].Copy
  
    ActiveWindow.WindowState = xlMinimized
   
    [color=Red]AppActivate "未命名 - 記事本"

    SendKeys "^V", True[/color]

End Sub

紅字為追加的部份,這個就是綜合兩位大大提供的想法來改,再次感謝!

頁: [1]

麻辣家族討論版版為 麻辣學園 網站成員  由 昱得資訊工作室 © Since 1993 所提供