0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

VBAでバーコードを画像に出力するプログラムをChatGPTに聞いてみた

Posted at

背景

Pythonのライブラリーで出力したバーコードで読み込みができずにVBAで出力したバーコードのみしか対応できないため、VBAでバーコードを画像に出力するプログラムを作成しかありませんでした。
このコードでは、SaveClipboardAsPNGサブルーチンがクリップボードから取得した画像をPNGファイルとして保存します。ActiveWindow.SelectedSheets.CopyPictureを使用してスクリーンショットを取得し、それをクリップボードにコピーしています。

fileName を適切なパスとファイル名に変更してください。これにより、指定されたパスにPNG形式のバーコード画像が保存されます。

Sub GenerateAndSaveBarcodePNG()
    Dim barcodeValue As String
    Dim barcodeFont As String
    Dim fontSize As Integer
    Dim fileName As String

    ' バーコードに変換する値
    barcodeValue = "123456789"

    ' 使用するバーコードフォント
    barcodeFont = "Free 3 of 9"

    ' フォントサイズ
    fontSize = 16

    ' 新しいシートを作成
    Sheets.Add

    ' バーコードをセルに設定
    Range("A1").Value = barcodeValue

    ' セルにバーコードフォントを適用
    Range("A1").Font.Name = barcodeFont
    Range("A1").Font.Size = fontSize

    ' シートをアクティブにする
    Sheets(1).Activate

    ' シートをスクリーンショットとして保存
    fileName = "C:\Path\To\Save\BarcodeImage.png"
    ActiveWindow.SelectedSheets.CopyPicture Appearance:=xlScreen, Format:=xlPicture
    SaveClipboardAsPNG fileName

    ' シートを削除
    Application.DisplayAlerts = False
    ActiveWindow.SelectedSheets.Delete
    Application.DisplayAlerts = True
End Sub

Sub SaveClipboardAsPNG(filePath As String)
    ' クリップボードの内容を取得
    Dim img As Object
    Set img = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")

    ' クリップボードの内容をPNGファイルとして保存
    With img
        .SaveAsFile filePath, 2 ' 2 はPNGフォーマットを指定
    End With
End Sub
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?