web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Insert file name from ...
Power Automate
Unanswered

Insert file name from the specific Excel cell when creating PDF

(0) ShareShare
ReportReport
Posted on by

Hi,
I am new here. If I am posting this to the wrong forum, please let me know.

 

I need to create 200 pcs invoice of a PDF file from one Excel worksheet.

All invoices have the same billing amount, but the billing client's name differs. The total client numbers are about 200.

 

I did like this;
1) I created an invoice worksheet and a client name list individually. (I have 2 Excel files)
2) I selected the "Read from Excel worksheet" and "Write to Excel worksheet" actions and inserted the client name to A9 cell in the invoice Excel worksheet.
3) "Print document" - the default printer is "Microsoft Print to PDF"

 

When I run this flow, the PDF saving dialogue window appears.

I want to insert the client name, which is in the A9 cell in my invoice Excel worksheet, in the file name field and save the file as a PDF.

 

I want to know the next step I need to do.
Thanks in advance.

poqweautomate.jpg
I have the same question (0)
  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    Use the Populate text field in window action under the UI actions group and capture the file name field. Send in the same value you used to type in the client name into the Excel sheet.

    Just make sure your client names do not have any characters that are not allowed in Windows file names. Use Replace text if you need to remove any of those special characters.

    -------------------------------------------------------------------------

    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

     

  • kinuasa Profile Picture
    799 Most Valuable Professional on at

    こんにちは。スクリーンショットが日本語だったので日本語で回答させていただきます。

     

    Microsoft Print to PDFプリンターによる印刷処理を使用してExcelファイルをPDF化したいとのことですが、「印刷結果を名前を付けて保存」ダイアログの「ファイル名」入力欄に文字列を入力する際は「ウィンドウ内のテキスト フィールドに入力する」アクションが使用できるかと思います。

     

    「UI要素の追加」から「ファイル名」入力欄を追加(Ctrl + クリック)し、入力するテキストとしてPDFファイル名を指定します。このとき保存先フォルダーのパスを含めることで指定した場所にファイルを保存できます。 @Agnius  氏も指摘されているように、ファイル名として指定できない文字が含まれていないか事前に確認しておいた方が良いでしょう。

     

    01_UI要素追加.jpg

     

    02_PDF保存先指定.jpg

     

    同様に「保存」ボタンもUI要素として追加することで、「ウィンドウ内のボタンを押す」アクションを使ってクリックできます。

     

    03_UI要素追加(2).jpg

     

    印刷処理部分のフローをまとめると下図のようになります。

     

    04_PDF保存フロー.jpg

     

    一例ですがフロー全体としては下図のようになるかと思います。

    約200個のファイルを作成するとのことですので、実際にはこれに繰り返し処理(ループ)が加わります。

    また、下図フローではPDF作成(印刷処理)時に同名のファイルがすでに存在する場合の処理について考慮していませんので、例えば保存先を新規作成したフォルダーを指定するなり、何らかの対応をしておいた方が無難です。

     

    05_フロー全体.jpg

     

    以下は私の方で作成した上記フローのコードになります。
    コピーしてデザイナー画面で貼り付けることができますが、貼り付けたフローはそのままでは動作しない可能性がありますので、あくまでも参考例としていただけますと幸いです。

     

    SET PDFFolderPath TO $'''C:\\Test\\PDF\\請求書'''
    /# ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
    顧客名取得
    ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★#/
    Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: $'''C:\\Test\\Excel\\顧客リスト.xlsx''' Visible: True ReadOnly: False Instance=> ExcelInstance
    Excel.SetActiveWorksheet.ActivateWorksheetByName Instance: ExcelInstance Name: $'''顧客リスト'''
    Excel.ReadFromExcel.ReadCell Instance: ExcelInstance StartColumn: $'''A''' StartRow: 2 ReadAsText: False CellValue=> ExcelData
    Excel.CloseExcel.Close Instance: ExcelInstance
    /# ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
    請求書の顧客名入力後ブックを保存して閉じる
    ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★#/
    Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: $'''C:\\Test\\Excel\\請求書.xlsx''' Visible: True ReadOnly: False Instance=> ExcelInstance
    Excel.SetActiveWorksheet.ActivateWorksheetByName Instance: ExcelInstance Name: $'''請求書'''
    Excel.WriteToExcel.WriteCell Instance: ExcelInstance Value: ExcelData Column: $'''A''' Row: 2
    Excel.CloseExcel.CloseAndSave Instance: ExcelInstance
    /# ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
    請求書シートを印刷してPDF化
    ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★#/
    Workstation.SetDefaultPrinter PrinterName: $'''Microsoft Print to PDF'''
    Workstation.PrintDocument DocumentPath: $'''C:\\Test\\Excel\\請求書.xlsx'''
    UIAutomation.PopulateTextField TextField: appmask['WinExcel']['EdtFileName'] Text: $'''%PDFFolderPath%\\%ExcelData%.pdf''' Mode: UIAutomation.PopulateTextMode.Replace ClickType: UIAutomation.PopulateMouseClickType.SingleClick
    UIAutomation.PressButton Button: appmask['WinExcel']['BtnSave']
    
    # [ControlRepository][PowerAutomateDesktop]
    
    {
     "ControlRepositorySymbols": [
     {
     "IgnoreImagesOnSerialization": false,
     "Repository": "{\r\n\"Screens\": [\r\n{\r\n\"Controls\": [\r\n{\r\n\"AutomationProtocol\": \"uia3\",\r\n\"ScreenShot\": null,\r\n\"ElementTypeName\": \"Edit\",\r\n\"InstanceId\": null,\r\n\"Name\": \"EdtFileName\",\r\n\"SelectorCount\": 1,\r\n\"Selectors\": [\r\n{\r\n\"CustomSelector\": \" > window[Process=\\\"EXCEL\\\"] > window[Class=\\\"#32770\\\"] > pane[Class=\\\"DUIViewWndClassName\\\"] > pane[Class=\\\"DirectUIHWND\\\"] > pane[Class=\\\"FloatNotifySink\\\"] > combobox[Class=\\\"ComboBox\\\"][Name=\\\"ファイル名:\\\"] > edit[Class=\\\"Edit\\\"]\",\r\n\"Elements\": [],\r\n\"Ignore\": false,\r\n\"IsCustom\": true,\r\n\"IsWindowsInstance\": false,\r\n\"Name\": \"DefaultSelector\",\r\n\"Properties\": []\r\n}\r\n],\r\n\"Tag\": null,\r\n\"ScreenshotPath\": null\r\n},\r\n{\r\n\"AutomationProtocol\": \"uia3\",\r\n\"ScreenShot\": null,\r\n\"ElementTypeName\": \"Button\",\r\n\"InstanceId\": null,\r\n\"Name\": \"BtnSave\",\r\n\"SelectorCount\": 1,\r\n\"Selectors\": [\r\n{\r\n\"CustomSelector\": \" > window[Class=\\\"bosa_sdm_XL9\\\"][Process=\\\"EXCEL\\\"] > window[Class=\\\"#32770\\\"] > button[Class=\\\"Button\\\"][Name=\\\"保存(S)\\\"]\",\r\n\"Elements\": [],\r\n\"Ignore\": false,\r\n\"IsCustom\": true,\r\n\"IsWindowsInstance\": false,\r\n\"Name\": \"DefaultSelector\",\r\n\"Properties\": []\r\n}\r\n],\r\n\"Tag\": null,\r\n\"ScreenshotPath\": null\r\n}\r\n],\r\n\"ScreenShot\": null,\r\n\"ElementTypeName\": \"Window\",\r\n\"InstanceId\": null,\r\n\"Name\": \"WinExcel\",\r\n\"SelectorCount\": 1,\r\n\"Selectors\": [\r\n{\r\n\"CustomSelector\": \":desktop > window[Process=\\\"EXCEL\\\"]\",\r\n\"Elements\": [],\r\n\"Ignore\": false,\r\n\"IsCustom\": true,\r\n\"IsWindowsInstance\": false,\r\n\"Name\": \"DefaultSelector\",\r\n\"Properties\": []\r\n}\r\n],\r\n\"Tag\": null,\r\n\"ScreenshotPath\": null\r\n}\r\n],\r\n\"Version\": 1\r\n}",
     "ImportMetadata": {
     "DisplayName": "LocalComputer",
     "ConnectionString": "",
     "Type": "Local",
     "DesktopType": "local"
     },
     "Name": "appmask"
     }
     ],
     "ImageRepositorySymbol": {
     "Repository": "{\r\n \"Folders\": [],\r\n \"Images\": [],\r\n \"Version\": 1\r\n}",
     "ImportMetadata": {},
     "Name": "imgrepo"
     },
     "ConnectionReferences": []
    }

     

  • kinuasa Profile Picture
    799 Most Valuable Professional on at

    別案としてPDFに変換する部分だけを下図のようなスクリプトに任せる方法もあります。

     

    06_VBSCode.jpg

     

    スクリプトの処理が入ることである程度のプログラミングの知識が必要になってしまいますし、フローのメンテナンス性が低くなってしまうかもしれないのですが、ダイアログの操作を行わずに済む分安定性は高くなります。

     

    こちらも参考までにコピー&ペーストで実行できるフローのコードを記載しておきます。
    なお動作確認は Windows 10 + Power Automate for desktop バージョン 2.36.140.23262 + Excel for Microsoft 365 MSO (バージョン 2310 ビルド 16.0.16921.20000) 64 ビット 環境で行っています。

     

    SET CustomerListFilePath TO $'''C:\\Test\\Excel\\顧客リスト.xlsx'''
    SET InvoiceFilePath TO $'''C:\\Test\\Excel\\請求書.xlsx'''
    SET PDFFolderPath TO $'''C:\\Test\\PDF\\請求書'''
    /# ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
    顧客名取得
    ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★#/
    Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: CustomerListFilePath Visible: True ReadOnly: False Instance=> ExcelInstance
    Excel.SetActiveWorksheet.ActivateWorksheetByName Instance: ExcelInstance Name: $'''顧客リスト'''
    Excel.ReadFromExcel.ReadCell Instance: ExcelInstance StartColumn: $'''A''' StartRow: 2 ReadAsText: False CellValue=> ExcelData
    Excel.CloseExcel.Close Instance: ExcelInstance
    /# ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
    請求書の顧客名入力後PDF変換
    ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★#/
    Excel.LaunchExcel.LaunchAndOpenUnderExistingProcess Path: InvoiceFilePath Visible: True ReadOnly: False Instance=> ExcelInstance
    Excel.SetActiveWorksheet.ActivateWorksheetByName Instance: ExcelInstance Name: $'''請求書'''
    Excel.WriteToExcel.WriteCell Instance: ExcelInstance Value: ExcelData Column: $'''A''' Row: 2
    Scripting.RunVBScript.RunVBScript VBScriptCode: $'''Const xlTypePDF = 0
    Const xlQualityStandard = 0
    GetObject(\"%InvoiceFilePath%\").ActiveSheet.ExportAsFixedFormat xlTypePDF, \"%PDFFolderPath%\\%ExcelData%.pdf\", xlQualityStandard, False, False, , , False''' ScriptOutput=> VBScriptOutput
    Excel.CloseExcel.Close Instance: ExcelInstance

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 241 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 179

#3
Haque Profile Picture

Haque 159

Last 30 days Overall leaderboard