:D snasui.com ยินดีต้อนรับ :D
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย :thup: สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ :arrow: ระบุ Version ของ Excel
:!: โปรดทราบ :!:
  1. กรุณาอ่านกฎการใช้บอร์ด (Forum rules) ในตำแหน่งด้านบนของแต่ละบอร์ด
  2. การสมัครสมาชิก การ Login การกู้คืนรหัสผ่าน
    1. สมัครสมาชิกดูขั้นตอนได้ที่ :arrow: สมัครสมาชิก
    2. Login เข้าระบบโดยคลิกปุ่ม Login ตรงมุมขวาบนของหน้านี้ :roll:
    3. การ Login ผ่าน Facebook ดูวิธีที่ :arrow: Login ผ่าน Facebook
    4. ลืมรหัสผ่านสามารถรับรหัสใหม่ได้ที่ :arrow: Reset รหัสผ่าน
  3. มีปัญหาการใช้งาน แจ้งผู้ดูแลระบบได้ที่ :arrow: ติดต่อผู้ดูแลระบบ
  4. กำหนดการตั้งค่าส่วนตัว เช่นตั้งค่าภาษาเป็นไทยหรืออังกฤษดูได้ที่ :arrow: ตั้งค่าส่วนตัว
  5. การตั้งและตอบกระทู้ดูได้ที่ :arrow: วิธีการตั้งและตอบกระทู้
  6. การจัดรูปแบบตัวอักษรด้วย bbcode ในช่องแสดงความคิดเห็นดูได้ที่ :arrow: จัดรูปแบบตัวอักษร
  7. กำหนดขนาดตัวอักษรใน Browser ดูได้ที่ :arrow: กำหนดขนาดตัวอักษรใน Browser

save รูปพร้อมกับใส่ชื่อ

ฟอรัมถาม-ตอบปัญหาการใช้งานสูตรและฟังก์ชัน Excel
Forum rules
  1. ไม่อนุญาตให้ใช้ภาษาแชทในการถามและตอบปัญหา ไม่ใช้คำว่า "คับ" หรือ "อ่ะครับ" แทนคำว่า "ครับ" ไม่ใช้คำว่า "เด๋ว" แทนคำว่า "เดี๋ยว" เป็นต้น เนื่องจากเมื่อแปลเป็นภาษาต่างประเทศแล้วจะให้ความหมายผิดไปจากที่ควรจะเป็น
  2. ห้ามถามโดยระบุชื่อผู้ตอบและต้องตั้งชื่อกระทู้ให้สื่อถึงปัญหาที่จะถาม ไม่ตั้งชื่อว่า ช่วยด้วยครับ, มีปัญหามาปรึกษาครับ เป็นต้น
  3. กรุณาอธิบายปัญหาและระบุคำตอบที่ต้องการมาในกระทู้ด้วยเสมอถึงแม้จะอธิบายไว้ในไฟล์แนบแล้วก็ตาม ทั้งนี้เพื่ออำนวยความสะดวกแก่เพื่อนสมาชิกในการค้นหาข้อมูล
  4. กรุณาแนบไฟล์ตัวอย่างพร้อมแสดงคำตอบที่ถูกต้องมาในไฟล์ด้วยเพื่อให้ง่ายต่อการทำความเข้าใจและสะดวกต่อการตอบคำถาม (ขนาดไฟล์ไม่เกิน 500Kb ขนาดภาพไม่เกิน 800*600 Pixel) ไม่แนบเป็น Link มาจากแหล่งอื่นที่อาจจะถูกลบทิ้งไปโดยต้นทางในภายหลัง นอกจากนี้ไม่ควรแนบไฟล์ที่มีข้อมูลสำคัญอันก่อให้เกิดความเสียหายกับตนเองและผู้อื่น
  5. กรณีเป็นคำถามเกี่ยวกับ Programming เช่น VBA, VB.Net, C#, SQL ฯลฯ ต้องลองเขียนมาเองก่อนเสมอ ถามเฉพาะที่ติดปัญหา ระบุ Module, Procedure ที่ติดปัญหาให้ชัดเจน กรุณาโพสต์ Code ให้แสดงเป็น Code คือเปิดด้วย [code] และปิดด้วย [/code] ตัวอย่างเช่น [code]dim r as range[/code] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)
  6. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
User avatar
mr.zatan
Bronze
Bronze
Posts: 305
Joined: Sun Sep 05, 2010 8:31 am
Excel Ver: 2016
Contact:

save รูปพร้อมกับใส่ชื่อ

#1

Post by mr.zatan »

ต้องการ Save รูป พร้อมกับใส่ชื่อครับ
- ติดปัญหาคือชื่อไม่ตรงกับรูป

VBA

Code: Select all

Sub SaveImages()

    'the location to save all the images
    Const destFolder$ = "C:\Users\user\Desktop\New folder\"

    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("image")

    Dim ppt As Object, ps As Variant, slide As Variant

    Set ppt = CreateObject("PowerPoint.application")
    Set ps = ppt.presentations.Add
    Set slide = ps.slides.Add(1, 1)

    Dim shp As Shape, shpName$
    For Each shp In ws.Shapes
        shpName = destFolder & shp.TopLeftCell.Offset(1, 1) & ".jpg"
        shp.Copy
        With slide
            .Shapes.Paste
            .Shapes(.Shapes.Count).Export shpName, 2
            .Shapes(.Shapes.Count).Delete
        End With
    Next shp

    With ps
        .Saved = True
        .Close
    End With
    ppt.Quit
    Set ppt = Nothing

End Sub
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 31255
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: save รูปพร้อมกับใส่ชื่อ

#2

Post by snasui »

:D ในเครื่องผมไม่สามารถ Debug ได้เพราะ Code จะจบแค่ .Shapes.Paste ไม่มีการ Run ต่อ

ลอง Debug ตรง .Shapes(.Shapes.Count) โดยสังเกตว่าเป็น Shape อะไร ตรงกับภาพที่นำมาวางหรือไม่ครับ
User avatar
mr.zatan
Bronze
Bronze
Posts: 305
Joined: Sun Sep 05, 2010 8:31 am
Excel Ver: 2016
Contact:

Re: save รูปพร้อมกับใส่ชื่อ

#3

Post by mr.zatan »

:oops:
รัน VBA นานๆแล้ว solve system error &H80070057(-2147024809)

- Excel 2019
- รูปเยอะมากๆ จำเป็นต้อง รันนานๆ ครับ

** มีวิธีแก้ไหมครับ

Code: Select all

Sub Export_Pictures()


Dim picturename As String, mypath As String
Dim PicWidth As Double, PicHeight As Long
Dim init_PicWidth As Long, init_PicHeight As Double
Dim shp As Shape
Dim tempChart As Chart
Dim chartName As String
Dim sheetname As String
Dim sh As Worksheet

mypath = "C:\Users\safe\Desktop\New folder\"

sheetname = ActiveSheet.Name

Set tempChart = Charts.Add


tempChart.Location xlLocationAsObject, Name:=sheetname


chartName = Mid(ActiveChart.Name, Len(sheetname) + 2)

For Each shp In ActiveSheet.Shapes



picturename = Replace(shp.TopLeftCell.Offset(0, 1).Value, "/", "-")

picturename = Application.WorksheetFunction.Substitute(Application.WorksheetFunction.Substitute(picturename, _
Chr(10), " "), "  ", " ")

init_PicWidth = shp.Width
init_PicHeight = shp.Height



If shp.Name <> "Chart 1" And shp.HasChart = False Then
If Dir(flowerpath & picturename & ".jpg") = "" Then



shp.ScaleHeight 1#, True, msoScaleFromTopLeft


PicHeight = shp.Height
PicWidth = shp.Width

With ActiveSheet.Shapes(chartName)

.Width = shp.Width
.Height = shp.Height
End With


shp.Copy

ActiveChart.Paste


ActiveChart.Export Filename:=mypath & picturename & ".jpg", FilterName:="jpg"

shp.Width = init_PicWidth
shp.Height = init_PicHeight

ActiveChart.Shapes(1).Delete

End If

End If

Next


ActiveChart.ChartArea.Clear

End Sub


Image
User avatar
snasui
Site Admin
Site Admin
Posts: 31255
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: save รูปพร้อมกับใส่ชื่อ

#4

Post by snasui »

:D เปิดหน้าต่าง Immediate Window และเพิ่ม Debug.print shp.name ก่อนบรรทัด shp.copy เพื่อจะดูว่า Code มา Error ด้วย Shape ตัวไหน เผื่อพอจะได้ไล่หาสาเหตุจาก Shape ตัวนั้นได้ครับ
User avatar
mr.zatan
Bronze
Bronze
Posts: 305
Joined: Sun Sep 05, 2010 8:31 am
Excel Ver: 2016
Contact:

Re: save รูปพร้อมกับใส่ชื่อ

#5

Post by mr.zatan »

ขึ้นมาแบบนี้ครับ

Code: Select all

Picture 1
Picture 2
Picture 3
Picture 8
Picture 9
Picture 11
Picture 1
Picture 2
Picture 3
Picture 5
Picture 6
Picture 8
Picture 9
Picture 11
Picture 1
Picture 2
Picture 3
Picture 5
Picture 6
Picture 8
Picture 9
Picture 11
Picture 12
Picture 13
Picture 13
Picture 14
Picture 16
Picture 17
Picture 18
Picture 22
Picture 24
Picture 25
Picture 27
Picture 28
Picture 29
Picture 30
Picture 31
Picture 32
Picture 33
Picture 34
Picture 35
Picture 36
Picture 37
Picture 38
Picture 39
Picture 40
Picture 41
Picture 42
Picture 44
Picture 45
Picture 46
Picture 47
Picture 48
Picture 49
Picture 50
Picture 51
Picture 52
Picture 53
Picture 54
Picture 5
Picture 6
Picture 8
Picture 9
Picture 11
Picture 12
Picture 13
Picture 14
Picture 16
Picture 17
Picture 18
Picture 19
Picture 20
Picture 21
Picture 22
Picture 23
Picture 2
Picture 3
Picture 5
Picture 6
Picture 8
Picture 9
Picture 11
Picture 12
Picture 13
Picture 14
Picture 16
Picture 17
Picture 18
Picture 19
Picture 20
Picture 21
Picture 22
Picture 24
Picture 25
Picture 26
Picture 8
Picture 9
Picture 1
Picture 2
Picture 5
Picture 12
Picture 17
Picture 25
Picture 27
Picture 29
Picture 30
Picture 39
Picture 35
Picture 1
Picture 5
Picture 6
Picture 8
Picture 12
Picture 1
Picture 2
Picture 3
Picture 1
Picture 1
Picture 3
Picture 6
Picture 8
Picture 12
Picture 19
Picture 22
Picture 29
Picture 1
Picture 2
Picture 3
Picture 5
Picture 6
Picture 8
Picture 9
Picture 11
Picture 12
Picture 13
Picture 14
Picture 16
Picture 17
Picture 18
Picture 19
Picture 20
Picture 21
Picture 22
Picture 23
Picture 24
Picture 25
Picture 26
Picture 27
Picture 28
Picture 29
Picture 30
Picture 31
Picture 32
Picture 33
Picture 34
Picture 2
Picture 3
Picture 5
Picture 1
Picture 2
Picture 3
Picture 5
Picture 6
Picture 8
Picture 9
Picture 11
Picture 12
Picture 13
Picture 14
Picture 16
Picture 17
Picture 18
Picture 19
Picture 20
Picture 21
Picture 22
Picture 23
Picture 24
Picture 26
Picture 27
Picture 28
Picture 29
Picture 30
Picture 31
Picture 32
Picture 33
Picture 34
Picture 35
Picture 1
Picture 3
Picture 2
Picture 1
Picture 2
Picture 3
Picture 5
Picture 6
Picture 2
Picture 1
Picture 1

Last edited by mr.zatan on Sat Feb 01, 2020 9:47 am, edited 2 times in total.
User avatar
snasui
Site Admin
Site Admin
Posts: 31255
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: save รูปพร้อมกับใส่ชื่อ

#6

Post by snasui »

:D บรรทัดสุดท้ายคือรายการใด แสดงว่ารายการนั้นก่อให้เกิดปัญหา ลองเพิ่ม Debug.Pirnt ของชื่อชีตมาแสดงควบคู่ไปด้วยจะได้ทราบว่าของชีตไหนครับ
User avatar
mr.zatan
Bronze
Bronze
Posts: 305
Joined: Sun Sep 05, 2010 8:31 am
Excel Ver: 2016
Contact:

Re: save รูปพร้อมกับใส่ชื่อ

#7

Post by mr.zatan »

รึว่าเป็นเพราะมี Shape ตรงไหนสักแห่งที่ไม่ใช่ picture

ผมลองเพิ่ม Shape ลงไปที่ไม่ใช่ picture ก็จะเกิด solve system error &H80070057(-2147024809)
User avatar
snasui
Site Admin
Site Admin
Posts: 31255
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: save รูปพร้อมกับใส่ชื่อ

#8

Post by snasui »

:D ก็อาจจะเป็นไปได้เช่นกัน หากจะแก้ปัญหานี้ก็ต้องดักว่าจะต้องเป็น Picture จึงจะคัดลอกไปวางครับ
User avatar
mr.zatan
Bronze
Bronze
Posts: 305
Joined: Sun Sep 05, 2010 8:31 am
Excel Ver: 2016
Contact:

Re: save รูปพร้อมกับใส่ชื่อ

#9

Post by mr.zatan »

รบกวนแก้ Code ดักเอาแต่ Picture ให้ด้วยครับ
User avatar
snasui
Site Admin
Site Admin
Posts: 31255
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: save รูปพร้อมกับใส่ชื่อ

#10

Post by snasui »

:D ลองเขียนมาเองดูก่อนครับ

ดูตัวอย่างโพสต์นี้มีการดักว่าชื่อขึ้นต้นด้วย "Pict" แล้วลบทิ้ง viewtopic.php?t=324
Post Reply