Excel Get current user name VBA help needed for script - vba

I have a Filemaker Pro runtime DB that exports reports to Excel then opens in PDF, there is no problems running this on my machine but.....when a customer loads it onto there machine the VBA code (Current Username) needs to change. One big hurdle for me is to be able to distribute the Excel forms with my solution and have Excel know the different User name. So below is part of my VBA, I need to be able to change the SEAQ(username) to the Current Username.....export to Location is documents, I don't need to change that part.
ActiveWorkbook.UpdateLink Name:= _
"C:\Users\seaq\Desktop\LAB 17025\Forms\Particle Distribution\PD FM Exported.xlsx" _
, Type:=xlExcelLinks
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\seaq\Desktop\LAB 17025\Forms\Particle Distribution\Particle Dist Customer Report.pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True

E.g.
ActiveWorkbook.UpdateLink Name:= _
"C:\Users\" & Environ("Username") & _
"\Desktop\LAB 17025\Forms\Particle Distribution\PD FM Exported.xlsx", _
Type:=xlExcelLinks

Related

Why does creating a PDF from Word using VBA give a bigger file size than doing it manually?

I have a Word macro in VBA which does various things to a document and then creates a pdf of it. The trouble is that the pdf it creates is about twice the size of the pdf that I create if I do it manually.
The really bizarre thing is that I recorded a macro while I did it manually to see what the VBA code would be from doing it manually. And then when I run the exact code that I recorded, the file size doubles.
Here is the code that I recorded, which creates a PDF double the size of the one that got created while I recorded it.
ActiveDocument.ExportAsFixedFormat OutputFileName:= _
"[filename].pdf", _
ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
wdExportOptimizeForOnScreen, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=False, KeepIRM:=False, _
CreateBookmarks:=wdExportCreateHeadingBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=False, UseISO19005_1:=False
Any ideas how I can change the code to give a smaller file size?
I'm using Word 2013, and have Adobe Acrobat Standard DC 2015 installed.

Excel VBA: Paste clipboard data (external source) with the appropriate cell data type

I am copying table data (tab delimited) many times a day from external sources (other applications such as sqlClients: Heidi, MS SQL Management Studio, inhouse tools, ...) and pasting them in Excel sheets for extended analysis.
I've been struggling to make a macro that pastes specific columns in the correct format instead of the General type which alters how data is displayed.
At first I tried setting up specific columns to the appropriate dataType before using paste special to paste values only:
ActiveSheet.Range("B:D,X:X").NumberFormat = "#"
ActiveSheet.Range("A1").PasteSpecial _
Paste:=xlPasteValues _
, Operation:=xlNone _
, SkipBlanks:=False _
, Transpose:=False
Sadly it gives Run Time Error '1004' PasteSpecial method of Range class failed which I couldn't solve.
Next I tried TextToColumns follows:
ActiveSheet.Range("A1").Select
ActiveSheet.Paste
For Each cell In Selection.Cells
If cell <> "" Then
cell.TextToColumns _
Destination:=cell _
, DataType:=xlDelimited _
, ConsecutiveDelimiter:=False _
, Space:=False _
, TextQualifier:=xlTextQualifierNone _
, Tab:=True _
, semicolon:=False _
, comma:=False _
, other:=False _
, FieldInfo:=Array(Array(0, 2), Array(1, 2), Array(3, 2), Array(10, 2), Array(15, 2))
End If
Next
Not only it doesn't work as expected (what is wrong with it ?), but it doesn't seem efficient as It pastes the clipboard content before looping over every cell. My tables can be really huge and thus this will take a lot of time!
So, I was wondering if there is a better/elegant way to paste the clipboard data from an external source into the appropriate cell data type. If not, helping fixing the above leads would be nice as well.
Thank you in advance <3
Edit.1:
Now that I have some correct code working thanks to #jivko's comment:
ActiveSheet.Range("A1").Select
ActiveSheet.PasteSpecial Format:="Unicode Text", Link:=False, DisplayAsIcon:=False
Since I'm pasting code from different sources with different number of columns, I was wondering how could I know how many columns are there in the clipboard before pasting so I can apply the appropriate format beforehand ?
An idea would be to Paste > Count the columns > Apply the correct Format > Paste again. It doesn't sounds perfect but it should get the job done.
If you have any better solution please feel free to share <3
I use a Macro to paste my data, and I've inserted this code near the start. It prevents double quotes (at the start of an entry) from being a problem.
'This section prevents any special characters from interfering with the paste by specifying TextQualifier:=xlNone:
Range("A1").Select
ActiveCell.FormulaR1C1 = "abc"
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlNone, ConsecutiveDelimiter:=False, Tab:=True, Semicolon _
:=False, Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, _
1), TrailingMinusNumbers:=True
Range("A1").Select
Selection.ClearContents
'End Section

Trying to save and print a single sheet in Excel using VBA

I have been looking into an ongoing problem for my manager and I am stumped, He is trying to get it so when the end user clicks a macro assigned button on one of the Excel sheets, it saves a single sheet as PDF and prints the sheet too.
I have got the point shown by the image provided at the very bottom and the code runs up until the 'ActiveSheet.ExportAsFixedFormat' line. Does anyone have any suggestions on what I should use in order to get the explained?
I would really appreciate it.
Here's the code that I have till now:
Public Sub SavePrint()
ChDir "Path-to-the-file"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"Path-to-the-file\ACT Form.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
Regards, Kieran
Note1: ChDir needs to be used appropriately; after that you changed the path, you don't need to include it in the Filename and Excel automatically save the file in the current directory.
Note2: You need PDF add-in to use ExportAsFuxedFormat. To make sure that you have it navigate to
Excel > File > Options > Add-Ins > (at the bottom) Manage: COM Add-ins
Then find Adobe PDF or any other application that you use for managing PDFs (e.g. NitroPDF or BlueBeam) and check the box. If you don't have Adobe PDF then you need to install it on your machine and activate it in excel (i.e. checking the box) prior to running this macro.
Public Sub SavePrint()
ChDir "C:\"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"ACT Form.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
This will save the PDF file in C:\ folder.
Read this post to learn more about ChDir.
Read this to get an insight about ExportAsFixedFormat.
You can find out how to add Adobe PDF to the excel here.

(VBA) Convert all excel tabs to each separate protected pdf file

How to add password into the pdf files which generate from excel?
I have such part of following code:
Worksheets(i).ExportAsFixedFormat Type:=xlTypePDF, filename:=OutputPath2, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
I tried to add
Worksheets(i).Protect ("My Password")
and alternately
ActiveWorkbook.Protect "password",True,True
but neither worked.
Does anyone know how to make it work?
Many many thanks!
The answer is no ... Excel itself will not allow you to password protect the exported PDF file. You have to use an external tool to do so.
See the following for discussion and for alternatives explored: VBA to save a sheet to a password protected PDF and see https://social.msdn.microsoft.com/Forums/office/en-US/a26cae16-f039-4bb9-9b15-8016b7d0965a/conerting-excel-into-password-protected-pdf-using-vba?forum=exceldev

Excell VBA export some worksheets to PDF with ExportAsFixedFormat in a specified order

I am trying to create a PDF report in Excel which consists of a TitleSheet and a ReportSheet. When I select the two sheets I need and use ExportAsFixedFormat to generate the PDF I can't seem to control the order which the sheets are added to the document. As of now, the TitleSheet ends up at the bottom of the ReportSheet... not very helpful.
Here's my relevant code and some comments for things I tried that didn't work.
Dim FilePath As String
FilePath = Application.GetSaveAsFilename(ReportSheet.Name & ".PDF", "PDF(*.pdf),*.pdf*", 1, "Save As PDF File")
If FilePath = "False" Then Exit Sub
TitleSheet.Select 'so it's the first in the DPF report, no worky
Dim PrintSheets(1) As Variant
PrintSheets(0) = TitleSheet.Name 'changed index order, no worky
PrintSheets(1) = ReportSheet.Name
Sheets(PrintSheets).Select 'select both sheets so they both print
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
FilePath, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True
ReportSheet.Activate 'select this sheet so it's visible at the end
I looked for a few other answers (Exporting multiple pages to PDF in a specific order) but no cigar yet. I have different column widths between the TitlePage and the ReportPage so I don't want to go through the headache of merging those. I also can't use a 3rd party PDF creator because a mojority of users don't have admin rights on their machines to install any additional software.
After much searching and testing I found out that ExportAsFixedFormat exports sheets based on the tab/index order. Which means I need to index ReportSheet after TitleSheet with this line of code:
ReportSheet.Move after:=TitleSheet
Viola! Special thanks to Ken Puls: (http://www.excelguru.ca/forums/showthread.php?234-Reorder-pages-when-exporting-as-PDF&highlight=ExportAsFixedFormat)